我试图从此URL获取格式化地址。我在下面尝试过这个编码。但它没有显示任何内容,也没有显示任何错误。如何从json解码结果中获取格式化地址?
while($r = mysql_fetch_assoc($res)) {
$lat = $r['latitude'];
$lng = $r['longitude'];
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
$jsons = @file_get_contents($url);
$data = json_decode($jsons,true);
echo $data->results[0]->formatted_address;
$json[]= array("nod"=>ucwords($r['driver_name']),"info"=>ucwords($r['driver_name'])."<br />Speed:<b>".$r['current_speed']."Km/h</b>","lat"=>$r['latitude'],"lng" => $r['longitude'],"location"=> $data, "did"=>$r['driver_id'],"mobile_number"=>$r['mobile_number'],"current_speed"=>$r['current_speed']." km/h","vehicle_name"=>$r['vehicle_name'],"vehicle_number"=>$r['registration_number']);
}
答案 0 :(得分:1)
试试这个,它应该是$data
而不是$datas
$data=json_decode($jsons);
echo $data->results[0]->formatted_address;
而不是
$data=json_decode($jsons);
$datas->results[0]->formatted_address;
--^
我得到
的输出$lat = 32;
$lng = 40;
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
$jsons = @file_get_contents($url);
$data=json_decode($jsons);
echo $data->results[0]->formatted_address; // output: 75898, Saudi Arabia
答案 1 :(得分:1)
你走了:
<?php
while($r = mysql_fetch_assoc($res)) {
$lat = urlencode(trim($r['latitude']));
$lng = urlencode(trim($r['longitude']));
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$lat.','.$lng.'&sensor=false';
$jsons = @file_get_contents($url);
$data = json_decode($jsons,true);
$address = $data['results'][0]['formatted_address'];
$json [] = array(
"nod" => ucwords($r['driver_name']),
"info" => ucwords($r['driver_name'])."<br />Speed:<b>".$r['current_speed']."Km/h</b>",
"lat" => $r['latitude'],
"lng" => $r['longitude'],
"location" => $address,
"did" => $r['driver_id'],
"mobile_number" => $r['mobile_number'],
"current_speed" => $r['current_speed']." km/h",
"vehicle_name" => $r['vehicle_name'],
"vehicle_number" => $r['registration_number']
);
}
?>