【问题标题】:Fetch Google Maps api in PHP在 PHP 中获取 Google Maps api
【发布时间】:2018-06-08 17:33:07
【问题描述】:

我对 API 从来没有信心。 我设法使用 jQuery 在网页中获取 API,但现在我需要使用 PHP 仅解码以下 json (https://maps.googleapis.com/maps/api/geocode/json?address=via%20pascal%2033/2%2042123%20reggio%20emilia&key=) 的位置 lat 和 lng 并将这两个数据作为变量检索:

{
"results" : [
  {
     "address_components" : [
        {
           "long_name" : "2",
           "short_name" : "2",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Via Blaise Pascal",
           "short_name" : "Via Blaise Pascal",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Reggio Emilia",
           "short_name" : "Reggio Emilia",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Reggio nell'Emilia",
           "short_name" : "Reggio nell'Emilia",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Provincia di Reggio Emilia",
           "short_name" : "RE",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Emilia-Romagna",
           "short_name" : "Emilia-Romagna",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Italia",
           "short_name" : "IT",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "42123",
           "short_name" : "42123",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "Via Blaise Pascal, 2, 42123 Reggio Emilia RE, 
Italia",
     "geometry" : {
        "location" : {
           "lat" : 44.6666003,
           "lng" : 10.5960504
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 44.6679492802915,
              "lng" : 10.5973993802915
           },
           "southwest" : {
              "lat" : 44.6652513197085,
              "lng" : 10.5947014197085
           }
        }
     },
     "partial_match" : true,
     "place_id" : "ChIJWaiGeqUcgEcR9l6qwloEWgo",
     "types" : [ "street_address" ]
  }
],
"status" : "OK"
}

最后,我需要的是能够向用户显示:

"纬度是 $lat,经度是 $lng"

谢谢

【问题讨论】:

  • 我以前从未听说过“API 解码”这个词。如果您问如何在 PHP 中解码 JSON,答案是 json_decode()。
  • 是的,这就是我的意思,我尝试过使用 json 解码,但我无法解决我的问题,我只是在后期解码,所以只需 json_decode($datalink)不适合我。顺便说一句,感谢您的更正。
  • 解码 JSON 是必须的,如果您不使用 json_decode(),则必须找到 third-party JSON decoder 或自己滚动。

标签: php json api decode


【解决方案1】:

如果您只需要最后一个结果点,请尝试此代码。

$myJsonData = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=via%20pascal%2033/2%2042123%20reggio%20emilia&key=");
$myArrayData = json_decode($myJsonData, 1);
foreach($myArrayData['results'] as $myItem){    
$lat = $myItem['geometry']['location']['lat'];
$lng = $myItem['geometry']['location']['lng'];
}
printf("Latitude is %s and longitude is %s", $lat, $lng);

【讨论】:

  • 这简直太棒了!非常感谢。
  • 欢迎您。如果您需要所有结果的 lat/lng,只需将 lat/lng 收集到 foreach 中的数组即可。祝你好运!
猜你喜欢
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多