如果用户输入地址,我想转换为等效的LatLng。使用谷歌地图API v3如何使用给定的地址获得LatLng?
我读过文档,我想我可以使用Geocoder类来做到这一点,但无法弄清楚如何实现它。
感谢您的帮助!
如果用户输入地址,我想转换为等效的LatLng。使用谷歌地图API v3如何使用给定的地址获得LatLng?
我读过文档,我想我可以使用Geocoder类来做到这一点,但无法弄清楚如何实现它。
感谢您的帮助!
上有https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
一个很好的例子要缩短了一点:
geocoder = new google.maps.Geocoder();
function codeAddress() {
//In this case it gets the address from an element on the page, but obviously you could just pass it to the method instead
var address = document.getElementById('address').value;
geocoder.geocode({ 'address' : address }, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
//In this case it creates a marker, but you can get the lat and lng from the location.LatLng
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map : map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
我不认为location.LatLng
工作,但这个工程:
results[0].geometry.location.lat(), results[0].geometry.location.lng()
在探索Get Lat Lon源代码时发现它。
谢谢! – cpcdev 2016-07-15 22:58:47
如果你需要做这在后端,您可以使用下列网址结构:使用curl
https://maps.googleapis.com/maps/api/geocode/json?address=STREET_ADDRESS
示例PHP代码:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://maps.googleapis.com/maps/api/geocode/json?address=' . rawurlencode($address));
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);
curl_close ($curl);
有关详细信息,请参见附加documentation。
该文档提供了示例输出,并将帮助您获得自己的API key,以便能够向Google地图地理编码API发出请求。
你知道这是什么限制吗。我的意思是我可以在一个小时或一天内拨打多少电话? – 2015-08-24 08:17:32
免费的API限制是每24小时2500个请求,每秒5个请求...请参阅https://developers.google.com/maps/documentation/geocoding/intro#Limits – farinspace 2015-08-25 03:07:02
来到这里寻找这个。 PHP用户可能希望使用'$ arr = json_decode($ json,true);'来访问数据。 – 2016-01-20 19:52:34
难道我们不希望所有的文档都和jQuery一样好。我对Google的一些文档感到很困难。 – b729sefc 2014-05-25 15:32:17