为什么Google GeoCoding有时只会返回邮政编码? [英] Why does Google GeoCoding only sometimes return the postal code?

查看:122
本文介绍了为什么Google GeoCoding有时只会返回邮政编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我只通过我的GeoCoding电话呼叫某个城市并声明无法返回邮政编码。任何想法为什么?



这是我的电话:

  var test ='纽约,纽约'; 

var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':test},function(results,status){

if(status == google.maps.GeocoderStatus.OK){

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

var result = results [0] .address_components;

//console.log(result);

var postal = null;
var city = null;
var state = null;
var country = null;

for(var i = 0; i< result.length; ++ i){
if(result [i] .types [0] ==postal_code){
postal = result [i] .long_name;
}
if(result [i] .types [0] ==administrative_area_level_1 ){
state = result [i] .long_name;
}
if(result [i] .types [0] ==locality){
city = result [ i] .long_name;
}
if(result [i] .types [0] ==country){
country = result [i] .long_name;
}
}

alert(POSTAL:+ postal);
alert(STATE:+ state);
alert(CITY:+ city);
alert(COUNTRY:+ country);



解决方案

如果你真的需要一个邮政编码,你可以将请求发回到反向地理编码器,这应该会给你一个代表该城市的邮政编码:

  if(!postal){
geocoder.geocode({'location':results [0] .geometry.location},function(results,status){
if(status == google .maps.GeocoderStatus.OK){

var result = results [0] .address_components;

for(var i = 0; i< result.length; ++ i ){
if(result [i] .types [0] ==postal_code){
postal = result [i] .long_name;
}
}
alert(POSTAL:+ postal);
}
});
} else alert(POSTAL:+ postal);

示例


If I pass my GeoCoding call only a city and state it fails to return the postal code. Any idea why?

Here is my call:

var test = 'New York, NY';

var geocoder = new google.maps.Geocoder();         
geocoder.geocode({ 'address': test }, function (results, status) {          

if (status == google.maps.GeocoderStatus.OK) {  

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

    var result=results[0].address_components;

    //console.log(result);

    var postal = null;
    var city = null;
    var state = null;
    var country = null;

    for(var i=0;i<result.length;++i){
        if(result[i].types[0]=="postal_code"){
            postal = result[i].long_name;
        }
        if(result[i].types[0]=="administrative_area_level_1"){
            state = result[i].long_name;
        }
        if(result[i].types[0]=="locality"){
            city = result[i].long_name;
        }
        if(result[i].types[0]=="country"){
            country = result[i].long_name;
        }
    }

    alert("POSTAL: " + postal);
    alert("STATE: " + state);
    alert("CITY: " + city);
    alert("COUNTRY: " + country);

}

解决方案

If you really need a zip code, you can throw the request back to the reverse geocoder, that should give you a zip code somewhat representative of the city:

if (!postal) {
  geocoder.geocode({ 'location': results[0].geometry.location }, function (results, status) {          
    if (status == google.maps.GeocoderStatus.OK) {  

      var result=results[0].address_components;

      for(var i=0;i<result.length;++i){
       if(result[i].types[0]=="postal_code"){
          postal = result[i].long_name;
       }
      }
      alert("POSTAL: " + postal);
    }
});
} else alert("POSTAL: " + postal);

example

这篇关于为什么Google GeoCoding有时只会返回邮政编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆