json – 如何从V3 Google Maps API中提取邮政编码

发布时间:2019-05-15 发布网站:脚本之家
脚本之家收集整理的这篇文章主要介绍了json – 如何从V3 Google Maps API中提取邮政编码脚本之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用以下内容从地理编码中获取纬度.
$latitude = $output->results[0]->geometry->location->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": "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",{
      "long_name": "United States","short_name": "US","types": [ "country",{
      "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++)
        for (var j=0; j<components[i].types.length; j++)
            if (components[i].types[j]==type) return components[i].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");

等等…

总结

以上是脚本之家为你收集整理的json – 如何从V3 Google Maps API中提取邮政编码全部内容,希望文章能够帮你解决json – 如何从V3 Google Maps API中提取邮政编码所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:76874919,请注明来意。
标签: