Need Help Calculating Longitude And Latitude Midpoint Using Javascript From Php Values
I am currently working on a project and have run into quite a snag. I am running a mapping application using the google maps API and I need to calculate the midpoint between two po
Solution 1:
in PHP
function midpoint ($lat1, $lng1, $lat2, $lng2) {
$lat1= deg2rad($lat1);
$lng1= deg2rad($lng1);
$lat2= deg2rad($lat2);
$lng2= deg2rad($lng2);
$dlng = $lng2 - $lng1;
$Bx = cos($lat2) * cos($dlng);
$By = cos($lat2) * sin($dlng);
$lat3 = atan2( sin($lat1)+sin($lat2),
sqrt((cos($lat1)+$Bx)*(cos($lat1)+$Bx) + $By*$By ));
$lng3 = $lng1 + atan2($By, (cos($lat1) + $Bx));
$pi = pi();
return ($lat3*180)/$pi .' '. ($lng3*180)/$pi;
}
Solution 2:
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$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);
} elseif ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
This function will help you
Solution 3:
Use Openlayers and it helps to do this. http://www.openlayers.org/
Solution 4:
For a javascript version I converted @vineesh's answer.
functionmidpoint ($lat1, $lng1, $lat2, $lng2) {
$lat1 = $lat1 * 0.017453292519943295;
$lng1 = $lng1 * 0.017453292519943295;
$lat2 = $lat2 * 0.017453292519943295;
$lng2 = $lng2 * 0.017453292519943295;
$dlng = $lng2 - $lng1;
$Bx = Math.cos($lat2) * Math.cos($dlng);
$By = Math.cos($lat2) * Math.sin($dlng);
$lat3 = Math.atan2( Math.sin($lat1)+Math.sin($lat2),
Math.sqrt((Math.cos($lat1)+$Bx)*(Math.cos($lat1)+$Bx) + $By*$By ));
$lng3 = $lng1 + Math.atan2($By, (Math.cos($lat1) + $Bx));
$pi = 3.141592653589793;
$lat = ($lat3*180)/$pi;
$lng = ($lng3*180)/$pi;
return [$lat,$lng];
}
Post a Comment for "Need Help Calculating Longitude And Latitude Midpoint Using Javascript From Php Values"