我一直在尝试使用一些ajax来保存我的应用程序中的场所位置,并在堆栈溢出时偶然发现以下代码
function getLatLong(address)
{
var geocoder = new google.maps.Geocoder();
var result = "";
geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
result[lat] = results[0].geometry.location.Pa;
result[lng] = results[0].geometry.location.Qa;
} else {
result = "Unable to find address: " + status;
}
});
return result;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是当我调用函数时它什么也没有返回,当我调试并在chrome中设置断点时,它会在返回结果之前先破坏结果[lat] = results [0] .geometry.location.Pa;
我知道数组应该声明为类型数组,但即使我刚刚返回结果[0] .geometry.location对象也没有被返回
我该怎么做才能返回lat/long的位置,以便我可以存储在我的数据库中?
我发现,在使用Google Maps API时,结果会不时发生变化.通过改变我的意思是响应对象.基本上我是在查询谷歌地图以获取基于用户输入的地点列表(也使用jQueryUI自动完成).我正在尝试正确获取从Google Maps API返回的坐标,但包含精确|近似坐标的属性似乎会不时更改.
例如:在早些时候,我能够得到像item.geocode.geometry.location.Ya和的坐标item.geocode.geometry.location.Za.然后我的搜索表单破了,因为Google将其更改为item.geometry.location.mb和item.geometry.location.nb.
关于如何实现这种正确,稳定的方法的任何建议?
这让我疯了,由于某种原因,API用于返回Lat和Lon,如下所示:(我认为它是这样的):
place.geometry.location.y作为纬度
place.geometry.location.z经度
然后它开始像这样返回:
place.geometry.location.Ya作为纬度
place.geometry.location.Za经度
今天我的APP无法运行,我发现这是因为Google API正在返回Lat和Lon,如下所示:
place.geometry.location.$a作为纬度
place.geometry.location.ab经度
跆拳道?我该怎么办呢?我正在考虑简单地将位置内的第一个变量读取为Latitude,将第二个变量作为Longitude读取,无论其名称如何.但我担心API可能会在下次返回一个完全不同的对象并弄乱我的javascript.
有人经历过吗?我应该做些什么?