簡體   English   中英

解析谷歌地圖API的緯度和經度

[英]Parsing Google Maps API for Latitude and Longitude

我正在嘗試使用Google maps API獲取地址的lat / lng。 我在解析JSON結果時只是將lat和lng值存儲為PHP變量。 有人能幫我解析一下嗎?

這是我到目前為止:

<?php

$jsonObject = file_get_contents(
"http://maps.googleapis.com/maps/api/geocode/json?
address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true");

$object = json_decode($jsonObject);

$lat = $object->results[0]->geometry[0]->location[0]->lat;
$lng = $object->results[0]->geometry[0]->location[0]->lng;

echo "$lat, $lng";

?>

這是JSON提要(lat / lng值大約是代碼的三分之二):

  {
"results" : [
  {
     "address_components" : [
        {
           "long_name" : "1600",
           "short_name" : "1600",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Amphitheatre Parkway",
           "short_name" : "Amphitheatre Pkwy",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Mountain View",
           "short_name" : "Mountain View",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Santa Clara",
           "short_name" : "Santa Clara",
           "types" : [ "administrative_area_level_2", "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" ]
        }
     ],
     "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
     "geometry" : {
        "location" : {
           "lat" : 37.4216410,
           "lng" : -122.08550160
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 37.42298998029150,
              "lng" : -122.0841526197085
           },
           "southwest" : {
              "lat" : 37.42029201970850,
              "lng" : -122.0868505802915
           }
        }
     },
     "types" : [ "street_address" ]
  }
],
"status" : "OK"
}

geometrylocation不是數組,請嘗試

$lat = $object->results[0]->geometry->location->lat;
$lng = $object->results[0]->geometry->location->lng;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM