Calculate distance between two points in google maps V3

后端 未结 15 1704
醉梦人生
醉梦人生 2020-11-22 02:27

How do you calculate the distance between two markers in Google maps V3? (Similar to the distanceFrom function inV2.)

Thanks..

15条回答
  •  [愿得一人]
    2020-11-22 03:00

    Using PHP, you can calculate the distance using this simple function :

    // to calculate distance between two lat & lon
    
    function calculate_distance($lat1, $lon1, $lat2, $lon2, $unit='N') 
    { 
      $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") {
        return ($miles * 1.609344); 
      } else if ($unit == "N") {
          return ($miles * 0.8684);
        } else {
            return $miles;
          }
    }
    
    // function ends here
    

提交回复
热议问题