kyl*_*lex 44 google-maps-api-3
如果用户输入地址,我想转换为等效的LatLng.
我已经阅读了文档,我想我可以使用Geocoder类来做到这一点,但无法弄清楚如何实现它.
谢谢你的帮助!
Ama*_*iel 98
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 );
}
} );
}
Run Code Online (Sandbox Code Playgroud)
Avi*_*nav 28
我不认为location.LatLng是有效的,但这有效:
results[0].geometry.location.lat(), results[0].geometry.location.lng()
Run Code Online (Sandbox Code Playgroud)
在探索Get Lat Lon源代码时找到它.
far*_*ace 19
如果需要在后端执行此操作,可以使用以下URL结构:
https://maps.googleapis.com/maps/api/geocode/json?address=STREET_ADDRESS
Run Code Online (Sandbox Code Playgroud)
使用curl示例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);
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅其他文档
文档提供了示例输出,可帮助您获取自己的API密钥,以便能够向Google Maps Geocoding API发出请求.