Google地图地理编码器:返回邮政编码 [英] Google Maps Geocoder: Return postal_code

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

问题描述

我使用Jquery UI自动完成地址搜索,使用 Google地图地理编码器,当选择地址时,它返回地理编码信息。以下是摘录:

  $('#address-dropdown')。autocomplete({
//使用地理编码器使用谷歌地图API地址输入获取lat + long值API
source:function(request,response){
geocoder.geocode({'address':request.term},function(results,status ){
response($。map(results,function(item){
return {
label:item.formatted_address,
value:item.formatted_address,
location :item.geometry.location,
latitude:item.geometry.location.lat(),
longitude:item.geometry.location.lng()
}
})) ;
})
},

我需要能够返回用法postal_code来检查它们是否在允许的范围内。以下是一个输出示例: http://maps.goo gleapis.com/maps/api/geocode/json?address=1601+Main+St+Eaton+Rapids+MI+48864&sensor=false



我该如何定位postal_code信息?

解析方案

看看我的例子: http://jsfiddle.net/nEKMK/



o
pre $ if $($。 i in o.results [0] .address_components){
if(typeof(o.results [0] .address_components [i])===object&& o.results [0] .address_components [i] .types [0] ==postal_code){
var result = o.results [0] .address_components [i] .long_name;



if(!result){
alert(Error,there is no postal code);
} else {
alert(result);
}


I'm using Jquery UI to autocomplete search for an address using Google Maps Geocoder, and when an address is selected it returns the geocode information. Here is the snippet:

$('#address-dropdown').autocomplete({
  //Use geocoder to fetch lat+long values from an address input using google maps API
  source: function(request, response) {
    geocoder.geocode( {'address': request.term }, function(results, status) {
      response($.map(results, function(item) {
        return {
          label: item.formatted_address,
          value: item.formatted_address,
          location: item.geometry.location,
          latitude: item.geometry.location.lat(),
          longitude: item.geometry.location.lng()
        }
      }));
    })
  },

I need to ability to return the uses "postal_code" to check if they live within an allowable area. Here is an example output: http://maps.googleapis.com/maps/api/geocode/json?address=1601+Main+St+Eaton+Rapids+MI+48864&sensor=false

How could I target the postal_code information?

解决方案

Look at my example: http://jsfiddle.net/nEKMK/

o is your object.

if (o.results[0].address_components) {
    for (var i in o.results[0].address_components) {
        if (typeof(o.results[0].address_components[i]) === "object" && o.results[0].address_components[i].types[0] == "postal_code") {
            var result = o.results[0].address_components[i].long_name;
        }
    }
}
if (!result) {
    alert("Error, there is no postal code");
} else {
   alert(result);
}

这篇关于Google地图地理编码器:返回邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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