【发布时间】:2012-02-27 17:11:09
【问题描述】:
我正在构建一个使用此代码搜索地址的网络应用程序:
http://code.google.com/intl/en/apis/maps/documentation/javascript/examples/places-autocomplete.html
在那里输入,纽约,纽约。
因此,当用户在使用自动完成选项后将地图加载到 DOM 中时,用户可以将位置地址保存在数据库中,并且可以使用“ex. New York, NY”。但是,对于应用程序,我还需要保存纬度和经度。
但我不知道如何从 Google API 中获取它们。
作为一个测试应用,我仍在使用谷歌代码。 我想我应该创建一些隐藏字段,并在用户选择地址时为其分配纬度和经度。
欢迎对我的问题进行任何实施!
提前致谢
附注: 由于我是 StackOverflow 的新手,我无法回答自己。 因此,我正在按照 stackoverflow 的建议编辑帖子,这是基于 Daniel 的回答和 Google Api 中的一些研究的解决方案:
function getLatLng( address )
{
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address' : address }, function( results, status ) {
if ( status == google.maps.GeocoderStatus.OK ) {
var mapLatLng = document.getElementById( 'mapLatLng' ); // mapLatLng is my hidden field, use your own
mapLatLng.value = results[0].geometry.location.lat() + ', ' + results[0].geometry.location.lng();
} else {
alert( "Geocode was not successful for the following reason: " + status );
}
});
}
【问题讨论】:
标签: google-maps latitude-longitude