admin管理员组文章数量:1277292
I've looked all over the internet for a simple answer to this, but I can't seem to find one anywhere. I do a simple reverse geocode of a latitude and longitude and I am returned the huge results object. Where do I go from there? Here is what I have so far. Currently zipcode returns "undefined".
geocoder.geocode( {'latLng' : event.latLng}, function(results, status) {
for(var i; i < results.length; i++){
for(var j=0;j < results[i].address_ponents.length; j++){
for(var k=0; k < results[i].address_ponents[j].types.length; k++){
if(results[i].address_ponents[j].types[k] == "postal_code"){
var zipcode = results[i].address_ponents[j].short_name;
}
}
}
}
});
I've looked all over the internet for a simple answer to this, but I can't seem to find one anywhere. I do a simple reverse geocode of a latitude and longitude and I am returned the huge results object. Where do I go from there? Here is what I have so far. Currently zipcode returns "undefined".
geocoder.geocode( {'latLng' : event.latLng}, function(results, status) {
for(var i; i < results.length; i++){
for(var j=0;j < results[i].address_ponents.length; j++){
for(var k=0; k < results[i].address_ponents[j].types.length; k++){
if(results[i].address_ponents[j].types[k] == "postal_code"){
var zipcode = results[i].address_ponents[j].short_name;
}
}
}
}
});
Share
Improve this question
edited Jun 7, 2012 at 6:04
mu is too short
435k71 gold badges858 silver badges818 bronze badges
asked Jun 7, 2012 at 5:58
ColinColin
2,4873 gold badges34 silver badges51 bronze badges
2
- looks as though you should have a "for(var i=0" in the beginning there. – luke_mclachlan Commented Feb 2, 2015 at 22:14
- Did you find any solution for reversing geo code in order to get postal code? – Pooya Panahandeh Commented Mar 14, 2023 at 13:06
4 Answers
Reset to default 10for(var i=0; i < results[0].address_ponents.length; i++)
{
var ponent = results[0].address_ponents[i];
if(ponent.types[0] == "postal_code")
{
console.log(ponent.long_name);
}
}
This is what I got working. Basically it just does a search for postal code in the first address ponent, since that contains all the information. Remember, console.log() is your friend.
Edit many years later
Thanks to lovely ES6, supported in move browsers, this can now be done more easily let's take a look at some easier ways of doing it. First, we could simplify the loop as such
for(let ponent of results[0].address_ponents))
{
if(ponent.types[0] == "postal_code")
{
console.log(ponent.long_name);
}
}
Or we could use the find method
let postalCode = results[0].address_ponents.find(function (ponent) {
return ponent.types[0] == "postal_code";
});
console.log(postalCode.long_name);
Found this neat function that (https://stackoverflow./users/638040/johann) posted as an answer from another stack overflow question.
It will find match the type you ask for and return it.
function extractFromAdress(ponents, type){
for (var i=0; i<ponents.length; i++)
for (var j=0; j<ponents[i].types.length; j++)
if (ponents[i].types[j]==type) return ponents[i].long_name;
return "";
}
Usage:
var postCode = extractFromAdress(results[0].address_ponents, "postal_code");
var country = extractFromAdress(results[0].address_ponents, "country");
Your issue is that the zipcode variable is a local variable to the function. I've got a working example on this jsfiddle
var zipcode = null;
function testFunct (results, status) {
for(var i in results){
var result = results[i];
for(var j in result.address_ponents){
var address_ponent = result.address_ponents[j];
for(var k in address_ponent.types){
var addrtype = address_ponent.types[k];
if(addrtype == "postal_code"){
zipcode = results[i].address_ponents[j].short_name;
console.log(zipcode);
return;
}
}
}
}
}
Your code does the following: If there are for instance two or three answers; it stores the last one in a row in the zipcode variable. Even if it's undefined. You should build in a check to see if the zip code is valid; and than you should return it to your zip code variable. And define the variable outside the function; otherwise it will be stuck in the function.
本文标签: javascriptHow do I get just the postal code from a reverse geocode resultStack Overflow
版权声明:本文标题:javascript - How do I get just the postal code from a reverse geocode result? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741282476a2370087.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论