本文介绍了使用谷歌地图API v3,我如何获得给定地址的LatLng?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果用户输入地址,我想转换为相应的LatLng。
If a user inputs an address, I want to convert to the equivalent LatLng.
我读过文档,我想我可以使用Geocoder类这样做,但无法弄清楚如何实现它。
I've read the documentation, and I think I can use the Geocoder class to do this, but can't figure out how to implement it.
感谢您的帮助!
推荐答案
缩短一点:
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 );
}
} );
}
这篇关于使用谷歌地图API v3,我如何获得给定地址的LatLng?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!