1

我在这里阅读了一些问答和其他文章,但无法让此脚本显示来自 JSON 返回的相关值(来自此 Google Maps 调用的长且经纬度)。

<?php

// Address for Google to search
$address = 'London,UK';

// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';

$json = file_get_contents($googleCall);
//header("Content-type: application/json");
//echo $json;

echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;
?>   

我确信我在那里 99%,只是看不到错误在哪里。

4

3 回答 3

2

您可以将您解码json为关联数组并使用范围访问所有数据

$address = 'London,UK';
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';

$json = file_get_contents($googleCall);
$array = json_decode($json, true); //note second parameter on true as we need associative array

echo $array['results'][0]['geometry']['location']['lat'] . '<br>';
echo $array['results'][0]['geometry']['location']['lng'];

这将输出

51.5112139
-0.1198244
于 2013-06-09T14:51:38.213 回答
0

使用json_decode这里的文档 http://php.net/manual/en/function.json-decode.php

<?php

// Address for Google to search
$address = 'London,UK';

// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';

$json = file_get_contents($googleCall);

$json = json_decode($json);


echo $json->results[0]->geometry->location->lat;
echo $json->results[0]->geometry->location->lng;
?>   
于 2013-06-09T14:47:10.403 回答
0

试试这个:

<?php

// Address for Google to search
$address = 'London,UK';

// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';

$json = file_get_contents($googleCall);

$json = json_decode($json);
//header("Content-type: application/json");
//echo $json;

echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;
?>   
于 2013-06-09T14:47:44.633 回答