我想从谷歌地图版本3到json解析数据。我想获得locaityName,AdministrativeAreaName和状态代码等详细信息。我可以知道如何解析这些数据吗?谢谢
答案 0 :(得分:3)
function getGoogleAddressCoordinates()
{
//using the google maps api to get the long and lat coordinates of addresss
//using form post variables
$address =trim($_POST['address']);
$city = trim($_POST['city']);
$state = trim($_POST['state']);
//formats the address add '+' into space
$address = str_replace( ' ', '+' ,$address);
$city = str_replace( ' ', '+', $city);
$address = preg_replace("[^A-Za-z0-9]", '+', $address ); //remove non alpha numeric chars such as extra spaces.
$address_str = $address.',+'. $city.',+'.$state;
$geo_url = "http://maps.google.com/maps/api/geocode/json?address=$address_str&sensor=false";
//echo $geo_url;
//echo file_get_contents($geo_url);
$json_result = json_decode(file_get_contents($geo_url));
$geo_result = $json_result->results[0];
$coordinates = $geo_result->geometry->location;
//see if the it is locating the real address or just apox location, or in most cases a bad address
if($geo_result->types[0] == 'street_address')
$coordinates->valid_address = TRUE;
else
$coordinates->valid_address = FALSE;
return $coordinates;
}
答案 1 :(得分:0)
答案 2 :(得分:0)
PHP有json_decode()将JSON数据流转换为PHP数组或对象。
答案 3 :(得分:0)
使用PHP PECL json库
json_decode()
http://php.net/manual/en/function.json-encode.php
只是一个建议,如果函数没有返回任何意味着JSON中存在语法错误的东西,它就不会引发任何错误或记录。