PHP 透過 Google Map Geocode取得地址的經緯度

$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]計算二點間距離

#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 &gt; 10000){
      return intval($meters/1000).msg('公里','km');
    }else{
      return $meters.msg('公尺','m');
    }
  } else if ($unit == "N") {
    return ($miles * 0.8684);
  } else {
    return $miles;
  }
}