【问题标题】:Google Maps API - Retrieving city and state using PHPGoogle Maps API - 使用 PHP 检索城市和州
【发布时间】:2015-08-01 01:26:30
【问题描述】:

我目前正在使用 Google Maps API 从用户的街道、街道号码和邮政编码中获取经度和纬度。

我还想从 Maps API 结果中获取他们的城市和州。虽然 XML 文档的格式很奇怪,但我不确定我应该如何引用它(这与我获取经纬度的方式不同)。

这就是我加载 Maps API 的方式:

$map_address = str_replace($address."+".$zip);
$map_api_url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$map_address."&key=myKey";
$resp_json = file_get_contents($map_api_url);
$resp = json_decode($resp_json, true);

然后我得到了经纬度:

$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];

但我如何获取城市和州?

{
   "results" : [
  {
     "address_components" : [
        {
           "long_name" : "3840",
           "short_name" : "3840",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Fake Street",
           "short_name" : "Fake St",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Wilson Heights",
           "short_name" : "Wilson Heights",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "North York",
           "short_name" : "North York",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "Toronto",
           "short_name" : "Toronto",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Toronto Division",
           "short_name" : "Toronto Division",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Ontario",
           "short_name" : "ON",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Canada",
           "short_name" : "CA",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "M6H",
           "short_name" : "M6H",
           "types" : [ "postal_code_prefix", "postal_code" ]
        }
     ],
     "formatted_address" : "3840 Fake Street, Toronto, ON M6H, Canada",
     "geometry" : {
        "location" : {
           "lat" : 43.5399244,
           "lng" : -78.43486559999999
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 43.5412733802915,
              "lng" : -78.43351661970848
           },
           "southwest" : {
              "lat" : 43.7385754197085,
              "lng" : -79.43621458029151
           }
        }
     },
     "place_id" : "ChIJPVDxjGgyK4gRt3O7zOSmIF4",
     "types" : [ "street_address" ]
  }
],
"status" : "OK"
}

【问题讨论】:

    标签: php google-maps rest google-maps-api-3


    【解决方案1】:

    刚刚通过循环运行代码回答了我自己的问题。如果您有更好的方法,请告诉我!

    foreach($resp['results'][0]['address_components'] as $address_componenets) {
        if($address_componenets["types"][0]=="country") {
            $country = $address_componenets["long_name"];
        }
        if($address_componenets["types"][0]=="administrative_area_level_1") {
            $state = $address_componenets["long_name"];
        }   
        if($address_componenets["types"][0]=="locality") {
            $city = $address_componenets["long_name"];
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-11
      • 2018-07-25
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2010-11-17
      • 2022-09-26
      • 2014-07-11
      相关资源
      最近更新 更多