Distance between two latitude longitude points

Calculating the distance in meters between two points defined with latitude and longitude.

Point one:

  • Latitude:¬† 52.9925463480
  • Longitude: 6.5634555238

Point two:

  • Latitude: 51.9406140497
  • Longitude: 4.8140287399

PHP version:

<?php

$earthRadius =  6367000; //approximate radius of earth in meters

$firstPointLatitude = 52.9925463480;
$firstPointLongitude = 6.5634555238;

$secondPointLatitude = 51.9406140497;
$secondPointLongitude = 4.8140287399;

/*
Convert these degrees to radians
to work with the formula
*/

$firstPointLatitude = ($firstPointLatitude * pi() ) / 180;
$firstPointLongitude = ($firstPointLongitude * pi() ) / 180;

$secondPointLatitude = ($secondPointLatitude * pi() ) / 180;
$secondPointLongitude = ($secondPointLongitude * pi() ) / 180;

/*
Using the
Haversine formula

http://en.wikipedia.org/wiki/Haversine_formula

calculate the distance
*/

$calcLongitude = $secondPointLongitude - $firstPointLongitude;
$calcLatitude = $secondPointLatitude - $firstPointLatitude;
$stepOne = pow(sin($calcLatitude/2), 2) + cos($firstPointLatitude) * cos($secondPointLatitude) * pow(sin($calcLongitude/2), 2);
$stepTwo = 2 * asin(min(1,sqrt($stepOne)));
$calculatedDistance = $earthRadius * $stepTwo;

print "Distance between these two points: " . $calculatedDistance . "m";

?>
This entry was posted in PHP and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?