gpt4 book ai didi

javascript - 获取格式整齐的地址 : Latlng -> Address Google Maps

转载 作者:行者123 更新时间:2023-11-29 05:41:10 27 4
gpt4 key购买 nike

我正在制作一个页面,该页面利用 Google map 地理编码器对象来查找用户输入的地址。我能够找到纬度/经度点,但我想格式化地址(清理它)以便我可以将纬度、经度和地址存储在 MySQL 数据库的 3 个单独字段中。这是我当前的代码:

也可以在安全范围内随意批评当前代码,我是 Gmaps API 的新手,对 js 不是很熟练

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic Map</title>


<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAKgJa4e_5XNFRJJF7MsI1eRQN1RsnicNCcG4bidjiZE76b9vSTBTCbiFNb-LGPKHeiBuV_2yfnWNSTA" type="text/javascript">
</script>



</head>

<body>


Input address Here: <input type="text" name="locate_address" id="locate_address" />
<input type="button" name="locate_address" value="Locate Address" onclick="locateAddress()" />
<div>
<p id="lat">Lat: </p>
<p id="lng">Lng: </p>
<p id="address"></p>
</div>

<br />
<div id="map" style="width: 300px; height:300px"></div>
<script type="text/javascript">

function locateAddress() {
var address = document.getElementById('locate_address').value;
geocoder = new GClientGeocoder();

geocoder.getLatLng (
address,
function (point) {
if (!point) {
alert(address + " not found!");
}
else {
map.setCenter(point, 13);
var marker = new GMarker (point);
map.addOverlay (marker);
marker.openInfoWindowHtml(address);
document.getElementById('address').innerHTML = address;
document.getElementById('lat').innerHTML += point.y;
document.getElementById('lng').innerHTML += point.x;
}
})

}



var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(39.1700149, -86.5148133), 13);

map.setMapType(G_NORMAL_MAP);



</script>







</body>
</html>

最佳答案

如果你真的想使用 v2 API

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic Map</title>


<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAKgJa4e_5XNFRJJF7MsI1eRQN1RsnicNCcG4bidjiZE76b9vSTBTCbiFNb-LGPKHeiBuV_2yfnWNSTA" type="text/javascript">
</script>



</head>

<body>


Input address Here: <input type="text" name="locate_address" id="locate_address" />
<input type="button" name="locate_address" value="Locate Address" onclick="locateAddress()" />
<div>
<p id="lat">Lat: </p>
<p id="lng">Lng: </p>
<p id="address"></p>
</div>

<br />
<div id="map" style="width: 300px; height:300px"></div>
<script type="text/javascript">

function locateAddress() {
var address = document.getElementById('locate_address').value;
geocoder = new GClientGeocoder();


geocoder.getLocations(address, getFormattedAddress)

}

function getFormattedAddress(response){
console.log(response)
if (!response || response.Status.code != 200) {
alert("\"" + address + "\" not found");
} else {
//get itemised address
street = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
city = response.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName
locality = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
postcode = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
country = response.Placemark[0].AddressDetails.Country.CountryName;
alert(street + " " + locality + " " +postcode + " " +country + " "+city)


place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(place.address + '<br>' +
'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
}
}

var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(39.1700149, -86.5148133), 13);

map.setMapType(G_NORMAL_MAP);



</script>







</body>
</html>

v3 见 http://code.google.com/apis/maps/documentation/javascript/services.html#GeocodingResponses

关于javascript - 获取格式整齐的地址 : Latlng -> Address Google Maps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6754523/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com