如何从V3谷歌地图API中提取邮政编码 [英] How to extract Postal code from V3 Google maps API

查看:152
本文介绍了如何从V3谷歌地图API中提取邮政编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ latitude = $ output-> 

使用以下代码获取地理编码中的lat-lng。 >结果[0] - >几何性质 - >位置 - > LAT;
$ longitude = $ output-> results [0] - > geometry-> location-> lng;

如何提取邮政编码...

  {
status:OK,
results:[{
types:[street_address]] ,
formatted_address:1600 Amphitheatre Pkwy,Mountain View,CA 94043,USA,
address_components:[{
long_name:1600,
short_name:1600,
types:[street_number]
},{
long_name:Amphitheater Pkwy,
short_name:Amphitheater Pkwy,
types:[route]
},{
long_name:Mountain View,
short_name:Mountain View,
types:[locality,political]
},{
long_name:California,
short_name:CA,
types:[administrative_area_level_1,political]
},{
long_name:美国,
short_name:美国,
类型:[国家,政治]
},{
long_name:94043,
short_name:94043,
types:[postal_code]
}],
geometry:{
location:{
lat:37.4219720,
lng:-122.0841430
},
location_type:ROOFTOP,
viewport:{
southwest:{
lat:37.4188244,
lng:-122.0872906
},
northeast:{
lat:37.4251196,
lng:-122.0809954
}
}
}
}]
}


解决方案

您可以使用以下函数来提取任何地址组件:

  function extractFromAdress(components,type){
for(var i = 0; I< components.length;如果(组件[i] .types [j] ==类型)返回组件[i]($ j
for(var j = 0; j< components [i] .types.length; j ++) .long_name;
return;
}

要提取您打电话的邮政编码:

  extractFromAdress(results [0] .address_components,postal_code); 

但是,您还可以获得其他有趣的信息:

  extractFromAdress(results [0] .address_components,route); 
extractFromAdress(results [0] .address_components,locality);
extractFromAdress(results [0] .address_components,country);

等...


I am using the following to get a the lat-lng from a geocode..

  $latitude = $output->results[0]->geometry->location->lat;
  $longitude = $output->results[0]->geometry->location->lng;

How do I extract the Postal code from ...

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

解决方案

You could use the following function to extract any address component:

function extractFromAdress(components, type){
    for (var i=0; i<components.length; i++)
        for (var j=0; j<components[i].types.length; j++)
            if (components[i].types[j]==type) return components[i].long_name;
    return "";
}

To extract the postal code you call:

extractFromAdress(results[0].address_components, "postal_code");

But you could also get other interesting info like:

extractFromAdress(results[0].address_components, "route");
extractFromAdress(results[0].address_components, "locality");
extractFromAdress(results[0].address_components, "country");

etc...

这篇关于如何从V3谷歌地图API中提取邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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