谷歌地理API(纬度+经度)


Google Geo API (Latitude + Longitude)

我目前正在尝试从我使用html5获得的GEO中解决我的用户位置,现在我正在尝试从Google API获取格式化地址。

我已经做到了:

function LatLong($Latitude, $Longitude) {

    $Url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$Latitude,$Longitude&sensor=true";

    //send request:
    $Client = curl_init($Url);
    //Set options:
    curl_setopt($Client, CURLOPT_RETURNTRANSFER, 1);
    //query the API and fetch results:
    $Response = curl_exec($Client);
    //decode the response:
    return  json_decode($Response);
}

现在,为了得到完整的结果,我尝试了这个,只是想看看它是否有效:

print_r(LatLong($_COOKIE['latitude'], $_COOKIE['longitude'])->results);

我确实得到了一个结果:

"results" : [
  {
     "address_components" : [
        {
           "long_name" : "4",
           "short_name" : "4",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Neuwerk - Cuxhaven",
           "short_name" : "Neuwerk - Cuxhaven",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Hamburg-Insel Neuwerk",
           "short_name" : "Hamburg-Insel Neuwerk",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "Hamburg",
           "short_name" : "Hamburg",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Hamburg",
           "short_name" : "Hamburg",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Tyskland",
           "short_name" : "DE",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "27499",
           "short_name" : "27499",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "Neuwerk - Cuxhaven 4, 27499 Hamburg, Tyskland",
     "geometry" : {
        "location" : {
           "lat" : 53.91403529999999,
           "lng" : 8.4899886
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 53.91538428029149,
              "lng" : 8.491337580291502
           },
           "southwest" : {
              "lat" : 53.91268631970849,
              "lng" : 8.488639619708497
           }
        }
     },
     "place_id" : "ChIJrT-2jXcZtEcRv91jn7XQhcQ",
     "types" : [ "street_address" ]
  }

我想打印formatted_address我试着这样做:

print_r(LatLong($_COOKIE['latitude'], $_COOKIE['longitude'])->results->formatted_address);

但我什么都没得到,我做错了什么?

如问题中所述,results是一个数组。这意味着您可能希望在获取formatted_address之前访问第一个元素。

试试这个:

print_r(LatLong($_COOKIE['latitude'], $_COOKIE['longitude'])->results[0]->formatted_address);
 $string = file_get_contents('./string.json');
  $json = json_decode($string);

如果你想要物品::

foreach ($json['items'] as $address)
{
    echo "items:". $address['address'] ."'n";
};

无论如何,如果你不确定数组是如何构建的,你可以通过打印它

print_r($json);

将打印:

阵列([items]=>数组([0]=>数组([地址]=>W 7th Ave)

        [1] => Array
            (
                [address] => W 8th St
            )
    )

)

现在您发现$json只包含一个由两个数组组成的数组(项(,然后,如果您循环它,您将获得在示例中打印的数组。如上所述,您需要更深入地循环items数组中的元素并打印它们的地址元素。

以下是完整的脚本:http://pastie.org/2275879