Google API地址组件 [英] Google API Address Components

查看:83
本文介绍了Google API地址组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Google Maps API获取地址组件,但无法正确解析结果。我的代码如下:

  // Ajax调用
$ .ajax({
url:' queryPage.php?',
data:'varObtainGoogleAddress = y&'+ $ b $'varAPILink ='+ encodeURIComponent(varAPILink),
dataType:'json',
success:function (data){
//格式化地址
varFormattedAddress = data.result ['formatted_address']; // Works!
}
});

我想要的是城市,州和邮政编码项目。在这方面的任何帮助表示赞赏。我是一名自学的业余Web开发人员。 :)

解决方案

我写了一个函数。

pre> / **
* geocodeResponse是一个充满地址数据的对象。
*这个函数会为正确的值钓鱼
*
*例如:type ='postal_code'=>
* geocodeResponse.address_components [5] .types [1] ='postal_code'
* geocodeResponse.address_components [5] .long_name ='1000'
*
* type =' route'=>
* geocodeResponse.address_components [1] .types [1] ='route'
* geocodeResponse.address_components [1] .long_name ='Wetstraat'
* /
function addresComponent (var i = 0; i< geocodeResponse.address_components.length; i ++){
for(var j = 0; j< geocodeResponse.address_components [i]如果(shortName){
返回geocodeResponse.address_components [i] .types.length; j ++){
if(geocodeResponse.address_components [i] .types [j] == type) 。简称;
}
else {
return geocodeResponse.address_components [i] .long_name;
}
}
}
}
return'';
}

如何使用的示例:

  ... 
myGeocoder.geocode({'latLng':marker.getPosition()},function(results,status){
if状态== google.maps.GeocoderStatus.OK&& results [1]){
var country = addresComponent('country',results [1],true);
var postal_code = addresComponent( 'postal_code',results [1],true);
}
});
...


I am attempting to obtain the address components using the Google Maps API however am unable to properly parse results. My code is as follows:

    // Ajax Call
$.ajax({
    url: 'queryPage.php?',
    data: 'varObtainGoogleAddress=y&' +
          'varAPILink=' + encodeURIComponent(varAPILink),
    dataType: 'json',
    success: function(data) {   
        // Formatted Address
        varFormattedAddress = data.result['formatted_address'];  // Works!
    }
});

What I would like is the city, state and postal code items. Any help in this regard is appreciated. I am a self taught amateur web developer. :)

解决方案

I wrote a function.

/**
*   geocodeResponse is an object full of address data.  
*   This function will "fish" for the right value
*   
*   example: type = 'postal_code' => 
*   geocodeResponse.address_components[5].types[1] = 'postal_code'
*   geocodeResponse.address_components[5].long_name = '1000'
* 
*   type = 'route' => 
*   geocodeResponse.address_components[1].types[1] = 'route'
*   geocodeResponse.address_components[1].long_name = 'Wetstraat'
*/
function addresComponent(type, geocodeResponse, shortName) {
  for(var i=0; i < geocodeResponse.address_components.length; i++) {
    for (var j=0; j < geocodeResponse.address_components[i].types.length; j++) {
      if (geocodeResponse.address_components[i].types[j] == type) {
        if (shortName) {
          return geocodeResponse.address_components[i].short_name;
        }
        else {
          return geocodeResponse.address_components[i].long_name;
        }
      }
    }
  }
  return '';
}

example of how to use:

...
myGeocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK && results[1]) {
    var country = addresComponent('country', results[1], true);
    var postal_code = addresComponent('postal_code', results[1], true);
  }
});
...

这篇关于Google API地址组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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