简体   繁体   English

Google Maps Javascript API v3返回不同的JSON吗?

[英]Google Maps Javascript API v3 returning different JSON?

I'm aware of the JSON output format as given by Google 我知道Google提供的JSON输出格式

However, I'm sometimes getting a slightly different format . 但是,有时我会得到稍微不同的格式 This is from the results variable in the callback function of gecoder.geocode({'address': address}, callback) 这来自gecoder.geocode({'address': address}, callback)的回调函数中的results变量

"location": {
          "Za": 37.3492097,
          "$a": -122.03260190000003
        },

... ...

"viewport": {
          "aa": {
            "b": 37.329901,
            "f": 37.37543489999999
          },
          "ba": {
            "b": -122.06526500000001,
            "f": -121.99577999999997
          }
        }

Notice the keys under location, bounds and viewport - they are different from what's standard. 请注意位置,边界和视口下的键-它们与标准键不同。 This is a problem for me because I need to send this JSON over an AJAX call and to my server and parse it and my parser can't take "$a" as a valid key name. 这对我来说是个问题,因为我需要通过 AJAX调用 将该JSON发送到我的服务器并进行解析,并且解析器无法将“ $ a”作为有效的键名。

Is this normal or am I missing something? 这是正常现象还是我缺少了什么?

EDIT: I'm on a cloud hosted platform (salesforce.com). 编辑:我在一个云托管平台(salesforce.com)上。 We're using the Javascript API v3. 我们正在使用Javascript API v3。 I was able to replicate this issue from a local HTML file. 我能够从本地HTML文件复制此问题。 I'm now thinking it might be because I'm behind a corporate proxy. 我现在想这可能是因为我落后于公司代理。 Anyone faced this before? 有人遇到过吗?

If you are using only documented methods and getting a non-documented response, it's a bug. 如果您仅使用已记录的方法并获得未记录的响应,那是一个错误。 File an issue in the Issues List -- you will need to include a demonstrator address, if there is one which produces a wrong response consistently. 在“ 问题列表”中提交问题 -如果有一个演示者地址,如果该地址始终产生错误的响应,则需要提供该演示者地址。 If there isn't a consistently-wrong address, then give as much detail as possible. 如果没有一致的地址,请提供尽可能多的细节。 Does it happen with requests repeated too close together, for example? 例如,重复的请求过于紧密地会发生吗?

Make sure that you also test using a different browser client and maybe even a different machine as well. 确保您还使用其他浏览器客户端甚至可能是其他计算机进行测试。 There could be an oddity in your current browser or computer which is causing the API to misbehave. 您当前的浏览器或计算机上可能存在异常,导致API行为异常。

I suggest commenting this answer with the issue number for future reference. 我建议用问题编号对此答案进行评论,以备将来参考。

As pointed out in a comment on the bug report and on the API documentation, the response object in the callback is not pure JSON. 正如在错误报告和API文档的注释中所指出的那样,回调中的response对象不是纯JSON。 In order to get a JSON object, I needed to create one myself like below: 为了获取JSON对象,我需要自己创建一个如下所示的对象:

//assume status is ok

var bestResult = getBestResultFromJSON(results);

var bounds = {'northeast' : {'lat' : 0, 'lng' : 0}, 'southwest' : {'lat' : 0, 'lng' : 0}};
var location = {'lat' : 0, 'lng' : 0};
var viewport = {'northeast' : {'lat' : 0, 'lng' : 0}, 'southwest' : {'lat' : 0, 'lng' : 0}};
// bounds not always available (https://developers.google.com/maps/documentation/javascript/reference#GeocoderGeometry)
if(bestResult.geometry.hasOwnProperty(bounds)) {
    bounds.southwest.lat = bestResult.geometry.bounds.getSouthWest().lat();
    bounds.southwest.lng = bestResult.geometry.bounds.getSouthWest().lng();
    bounds.northeast.lat = bestResult.geometry.bounds.getNorthEast().lat();
    bounds.northeast.lng = bestResult.geometry.bounds.getNorthEast().lng();
}
else {
    bounds = null;
}                       

viewport.southwest.lat = bestResult.geometry.viewport.getSouthWest().lat();
viewport.southwest.lng = bestResult.geometry.viewport.getSouthWest().lng();
viewport.northeast.lat = bestResult.geometry.viewport.getNorthEast().lat();
viewport.northeast.lng = bestResult.geometry.viewport.getNorthEast().lng();

location.lat = bestResult.geometry.location.lng();
location.lng = bestResult.geometry.location.lng();

var jsonResults = {'results' : [{'address_components' : bestResult.address_components,
              'formatted_address': bestResult.formatted_address,
              'geometry' : {'bounds' : bounds, 'location' : location, 'location_type' : bestResult.location_type, 'viewport' : viewport},
              'types' : bestResult.types}], 'status' : status};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM