从 JSON 结果中获取 postal_code 值
我正在使用 Google 地图地理编码器。我已经一切正常,但我似乎无法弄清楚如何“遍历”(解析?)JSON 结果。
如何从地理编码器的 JSON 结果中获取邮政编码?
我的尝试是循环遍历“address_components”,测试包含“postal_code”的数组的每个“values”键。
所以这是我到目前为止所写的内容的片段:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address : cAddress }, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
var fAddress = results[0].formatted_address;
var contactLatLng = results[0].geometry.location;
var postalCode = $.each(results[0].address_components,
function(componentIndex, componentValue) {
var typesArray = componentValue.types;
if ($.inArray("postal_code", typesArray)) {
return componentValue.long_name;
}
})
}
}
});
问题具体是 postalCode
[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object]`
显然,我遗漏了一些东西。
作为参考,以下是 Google Maps Geocoder JSON 结果的链接: http://code.google.com/apis/maps/documentation/geocoding/ #JSON
感谢您的帮助! ~阿莫斯
I'm working with the Google Maps Geocoder. I've got everything working fine, but I can't seem to figure out how to 'traverse' (parse?) the JSON results.
How can I get the postal code from the Geocoder's JSON results?
My attempt as been to loop through 'address_components', testing each "values" key for an array containing "postal_code".
So here's a snippet of what I've written so far:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address : cAddress }, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
var fAddress = results[0].formatted_address;
var contactLatLng = results[0].geometry.location;
var postalCode = $.each(results[0].address_components,
function(componentIndex, componentValue) {
var typesArray = componentValue.types;
if ($.inArray("postal_code", typesArray)) {
return componentValue.long_name;
}
})
}
}
});
The problem specifically is postalCode
is
[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object]`
Obviously, there is something I'm missing.
For reference, here is the link to Google Maps Geocoder JSON results:
http://code.google.com/apis/maps/documentation/geocoding/#JSON
Thanks for any help!
~Amos
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另请注意,“返回”不起作用。这是一个异步函数。因此,当您的函数运行时,父函数已经完成。
所以你的函数必须明确地对结果做一些事情。例如...
Also note, "return" doesn't work. It's a asynchronous function. So by the time your function runs, the parent function has finished.
So your function must do something explicitly with the result. for example...
假设这里的
$
是 jQuery 对象,您将返回results[0].address_components
集合,因为您的return componentValue.long_name;
被忽略通过each()
。您正在寻找的是$.map()
,它将返回修改后的集合。Assuming
$
here is jQuery object, you are geting back theresults[0].address_components
collection, since yourreturn componentValue.long_name;
is ignored byeach()
. What you are looking for is$.map()
, which will return modified collection.首先,我要对此表示感谢。刚刚帮我自己摆脱了困境。然而,我确实必须稍微改变一下代码。
我遇到的问题是 jQuery.inArray() 不返回布尔值 - 它要么返回数组中元素的索引,要么返回 -1。我对此感到困惑,如果不更改 if 语句来读取,我就无法让您的代码工作:
当我将其设置为检查 true 或 false 时,该 if 块中的代码将在每次迭代中运行$.each() 循环的一部分,因为 if 语句总是返回 -1 而不是 0 或 false。检查 $.inArray() 方法是否返回 -1 后,代码运行顺利。
再次感谢!
Firstly, let me say thanks for this. Just helped me out of a jam myself. I did, however, have to alter the code a little bit.
The issue I had was that jQuery.inArray() does not return a boolean value - it either returns the element's index in the array or -1. I got confused with this, and I couldn't get your code to work without changing the if statement to read as such:
When I had it set to check for true or false, the code within that if block would run on every single iteration of the $.each() loop, because the if statement would always return -1 instead of 0 or false. After checking if the $.inArray() method returned -1 or not, the code worked swimmingly.
Thanks again!