[php]
$address = $_GET["address"];
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo "您的地址:".$address."<br/>";
echo "所在的經度(latitude):".$lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo "所在的緯度(longitude):".$long = $response_a->results[0]->geometry->location->lng;
[/php]
標籤: 經緯度
MySQL求出二經緯度之間的距離(公尺)
假設自己的經緯度為 LAT,LNG
資料庫欄位_LAT,_LNG,_NAME,
SELECT CEIL((((ACOS(SIN((LAT*PI()/180)) * SIN((_LAT*PI()/180))+COS((LAT*PI()/180))
* COS((_LAT*PI()/180)) * COS(((LNG- _LNG)*PI()/180))))*180/PI())*60*1.1515*1.609344)*1000)
AS DISTANCE,_NAME
FROM TABLE_NAME
ORDER BY DISTANCE ASC
算出距離幾公尺
[PHP]計算二點間距離
[php]
#lat1,lon1是第一個點的經緯度
#lat2,lon2是第二個點的經緯度
#unit的話就用"K" (這樣的話傳回值就會是公尺)
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
global $source;
$theta = $lon1 – $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
$meters = intval($miles * 1.609344 * 1000);
if($meters > 10000){
return intval($meters/1000).msg(‘公里’,’km’);
}else{
return $meters.msg(‘公尺’,’m’);
}
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
[/php]