Gil*_*ino 3 google-maps latitude-longitude
我正在构建一个使用此代码搜索地址的Web应用程序:
http://code.google.com/intl/en/apis/maps/documentation/javascript/examples/places-autocomplete.html
键入那里,纽约,纽约.
因此,当用户使用自动完成选项后将地图加载到DOM中时,用户可以将位置地址保存在数据库中,并且它将可用,因为它是"ex.New York,NY".但是,对于应用程序,我还需要保存纬度和经度.
但我没有想法如何从谷歌API中获取它们.
作为测试应用,我仍在使用谷歌代码.我想我应该创建一些HIDDEN字段,并在用户选择地址时为它们分配纬度和经度.
欢迎任何实施我的问题!!
提前致谢
PS: 因为我是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 );
}
});
}
Run Code Online (Sandbox Code Playgroud)
Pat*_*gle 10
丹尼尔在大多数情况下是正确的,他不正确的是他在这里访问了这个地方:
$('#latitude').val(results[0].geometry.location.Pa)
$('#longitude').val(results[0].geometry.location.Qa)
Run Code Online (Sandbox Code Playgroud)
您应该使用提供的lat()和lng()函数:
$('#latitude').val(results[0].geometry.location.lat())
$('#longitude').val(results[0].geometry.location.lng())
Run Code Online (Sandbox Code Playgroud)
您可以使用Google API从您的地址获取经度和纬度.正如您所说,您应该实现一个隐藏字段,其中应插入结果.然后,您可以将位置与坐标一起保存.
我最近在我的一个项目中实现了这个功能:
function getLatLngFromAddress(city, country){
var address = city +", "+ country;
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$('#latitude').val(results[0].geometry.location.lat());
$('#longitude').val(results[0].geometry.location.lng());
} else {
console.log("Geocode was not successful for the following reason: " + status);
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6612 次 |
| 最近记录: |