【问题标题】:Parsing Google Maps API for Latitude and Longitude解析 Google Maps API 的纬度和经度
【发布时间】:2013-03-01 00:21:02
【问题描述】:

我正在尝试使用 Google 地图 API 获取地址的纬度/经度。我无法解析 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"
}

【问题讨论】:

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


    【解决方案1】:

    geometrylocation 不是数组,试试

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

    【讨论】:

    • 谢谢穆萨。你成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多