【问题标题】:google api geocoding returns json object different than official docsgoogle api geocoding 返回的 json 对象与官方文档不同
【发布时间】:2014-04-15 18:22:43
【问题描述】:

我已经成功使用 google api 进行地理编码,但是当我最初设置它时,我注意到返回的对象与此处的 google 文档中描述的不太一样:https://developers.google.com/maps/documentation/geocoding/#GeocodingResponses

我使用 console.dir 来检查 chrome 中的对象。

除非我误解了(很可能),否则我可以检索所提供地址的纬度和经度,如下所示:

lat = results[0].geometry.location.lat;
lng = results[0].geometry.location.lng;

除了,那不起作用,当我调查时,我发现 lat 和 lng 分别位于 results[0].geometry.location.dresults[0].geometry.location.e

一切都很好,直到今天它停止工作。调查我发现 lat & lng 正在通过results[0].geometry.location.k & results[0].location.A 返回。

大概这会在不久之后再次中断。

如果我检查 results[0].geometry.location.lat 它包含以下内容:

function (){
\"use strict\";
return this[a]}

有人认识吗?

【问题讨论】:

  • https://maps.googleapis.com/maps/api/geocode/json... 根据文档返回 lat/lng 格式。 geocoder.geocode() 返回 results[0].geometry.location 和属性 Ak,这很可能在未来发生变化。你说geocode()函数输出吗?
  • 是的,抱歉,我使用的是 geocoder.geocode(),虽然我是按照文档设置的。

标签: google-maps-api-3 geocoding


【解决方案1】:

从 Javascript-Geocoder 的 geocode-method 返回的结果已被翻译为与 Javascript-API 一起使用(链接的文档适用于 Geocoding-Webservice,而不是 Maps-Javascript-API)。

geometry.location 不是普通对象,它是google.maps.LatLng

您必须使用geometry.locationlat()lng() 方法来检索经纬度。

Specification of GeocoderGeometry

在您的情况下,这意味着像这样使用它:

lat = results[0].geometry.location.lat();

【讨论】:

  • 谢谢,我不知何故错过了。我稍微编辑了您的答案,以使任何新看这个问题的人受益。
  • 好的,但是在开发过程中,我返回的对象中不存在 lat() 和 lng() 方法!这就是我最初拥有代码的方式,它被破坏了,就像 OP 一样。
猜你喜欢
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 2011-06-18
  • 1970-01-01
  • 2012-11-14
  • 2011-08-28
相关资源
最近更新 更多