0
我想從google反向地理編碼api獲取城市名稱,我的代碼顯示位置的完整地址,但我只需要城市。波紋管是我的代碼從google反向地理編碼api獲取城市名稱的最佳方式php
<?php
session_start();
if(!empty($_POST['latitude']) && !empty($_POST['longitude'])){
//Send request and receive json data by latitude and longitude
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($_POST['latitude']).','.trim($_POST['longitude']).'&sensor=false';
$json = @file_get_contents($url);
$data = json_decode($json);
$status = $data->status;
if($status=="OK"){
//Get address from json data
//$location = $data->results[0]->formatted_address;
$location = $data->results[0]->address_components;
}else{
$location = '';
}
//Print city
$_SESSION['getgeoloc']=$location[6]->long_name;
echo $location;
}
?>
我想下面的代碼,以顯示城市,但有些時候它顯示郵政編碼,有時城市名
$_SESSION['getgeoloc']=$location[6]->long_name;
有什麼最好的方法?
我認爲這是因爲每個位置都有不同類型的數據,並形成不同的數組。不是嗎? –
是的,那麼如何從中獲得發揮城市名稱 –
這是一個有點困難的任務,因爲你必須做出一些正則表達式類的東西 –