2011-12-20 92 views
3

在我的網站我想獲得給定的PIN碼或郵政編碼的經度和緯度。 如果我給680721作爲PIN碼或郵政編碼,那麼我想要使用PHP代碼獲取其相應的經度和緯度。 我該怎麼做? 如果這在PHP中是可行的? 我有php代碼來獲取給定地名的經度和緯度。使用PHP獲取緯度和經度從PIN碼

$Url='http://maps.googleapis.com/maps/api/geocode/json?address='.$exp_pjctloc[$i].'&sensor=false'; 
if (!function_exists('curl_init')){ 
    die('Sorry cURL is not installed!'); 
} 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $Url); 
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm"); 
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
$output = curl_exec($ch); 
curl_close($ch); 
$search_data = json_decode($output); 
$lat = $search_data->results[0]->geometry->location->lat; 
$lng = $search_data->results[0]->geometry->location->lng; 

但是我怎樣才能得到它通過使用PIN碼或郵政編碼?

+1

我相信,一些國家(KHM,印度,KHM)使用術語「密碼」指區號或郵政編碼。我很困惑,直到我發現。 – Amadan 2011-12-20 07:39:36

+2

嘗試向它傳遞更多相關數據,可能是因爲地理編碼api很難確定你想要的東西,如果你只是遞交一個數字,嘗試添加國家,省份等......如果你知道它們的話。 – ChrisR 2011-12-20 07:55:41

回答

2

這爲我工作:

 
$zip = 94043; 
$site = file_get_contents('http://geocoder.ca/?postal='.$zip, false, NULL, 1000, 1000); 
$goods = strstr($site, 'GPoint('); // cut off the first part up until 
$end = strpos($goods, ')'); // the ending parenthesis of the coordinate 
$cords = substr($goods, 7, $end - 7); // returns string with only the 
$array = explode(', ',$cords); // convert string into array 
echo "<pre>"; 
print_r($array); // output the array to verify 
14

下面的代碼爲我工作

<?php 
    $zipcode="92604"; 
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false"; 
    $details=file_get_contents($url); 
    $result = json_decode($details,true); 

    $lat=$result['results'][0]['geometry']['location']['lat']; 

    $lng=$result['results'][0]['geometry']['location']['lng']; 

    echo "Latitude :" .$lat; 
    echo '<br>'; 
    echo "Longitude :" .$lng; 
    ?>