views:

798

answers:

1

Hi

SELECT postcode, lat, lng,
truncate(
 (degrees(acos
  (sin(radians(`lat`)) 
   * 
   sin( radians('.$latitude.')) 
   + 
   cos(radians(`lat`)) 
   * 
   cos( radians('.$latitude.')) 
   * 
   cos( radians(`lng` - ('.$longitude.'))) 
    ) 
   ) 
   * 69.172), 2) 
   as distance
   FROM `myData`

This query calculates distance (in miles). But when I check distance for same lat and longitude at google maps my result doesnt match. If the distance is around 10 miles then my result is a bit accurate, but over that it gets wrong (for example, my result showed 13 miles and google showed 22 miles for same post code values)

I got this query from http://forums.mysql.com/read.php?23,3868,3868#msg-3868

How can I get it accurate. Any ideas? Thanks for help.

UPDATE

I tried @Blixt code in PHP. Picked up 2 sample postcodes and their lats longs

//B28 9ET
$lat1 = 52.418819;
$long1 = -1.8481053;

//CV5 8BX
$lat2 = 52.4125573;
$long2 = -1.5407743;

$dtr = M_PI / 180;
$latA = $lat1 * $dtr;
$lonA = $long1 * $dtr;
$latB = $lat2 * $dtr;
$lonB = $long2 * $dtr;
$EarthRadius = 3958.76; //miles
echo $distance = $EarthRadius * acos(cos($latA) * cos($latB) * cos($lonB - $lonA) + sin($latA) * sin($latB));

Results:

My app - 12.95 miles

Google - 17.8 miles

Any ideas how to get it right?

+1  A: 

Have a look at this source code. When I tested it against various other measurement services it seemed to get the same results. It's C# but the math should be easy enough to convert.

Here are the relevant pieces:

public const double EarthRadius = 6371.0072; // Kilometers
//public const double EarthRadius = 3958.76; // Miles

/* ... */

const double dtr = Math.PI / 180;
double latA = this.Latitude * dtr;
double lonA = this.Longitude * dtr;
double latB = other.Latitude * dtr;
double lonB = other.Longitude * dtr;

return GpsLocation.EarthRadius * Math.Acos(Math.Cos(latA) * Math.Cos(latB) * Math.Cos(lonB - lonA) + Math.Sin(latA) * Math.Sin(latB));

Note: The Earth is not perfectly spherical, so the constants used may differ. There is no easy way to make the measurements truly exact. See Wikipedia.

Blixt
Thanks for your reply, Blixt. //B28 9ET$lat1 = 52.418819;$long1 = -1.8481053;//CV5 8BX$lat2 = 52.4125573;$long2 = -1.5407743;$dtr = M_PI / 180;$latA = $lat1 * $dtr;$lonA = $long1 * $dtr;$latB = $lat2 * $dtr;$lonB = $long2 * $dtr;$EarthRadius = 3958.76; //milesecho $distance = $EarthRadius * acos(cos($latA) * cos($latB) * cos($lonB - $lonA) + sin($latA) * sin($latB));Result: my app = 12.95~ milesGoogle = 17.8 miles-- :-( --
Wbdvlpr
oh code doesnt show up .. I'll just update my question.
Wbdvlpr
@Blixt - is it possible for you to check the above values in your application? If yes, can you please let me know what values you get?
Wbdvlpr
I have checked and I get the same values as you, and I verified it with a few other services on the internet and they also returned the same values. How are you checking the distance with Google Maps?
Blixt
Now I've even put the two coordinates on the map in Google Maps and used the "My Maps" feature to drag a line between the two points and sure enough, it shows ~13 miles. So the error probably lies in how you measure the distance with Google.
Blixt
hmm .. It is Google Maps get directions.
Wbdvlpr
Oh well then it is not strange! Get directions measures the distance to drive from one place to another (i.e. it follows the roads), this method calculates the point-to-point distance. Measuring the drive distance between two points is far more complex than what you have here.
Blixt
ah I see. Thanks, Blixt. I need to find about that "complex" formula.
Wbdvlpr
Oh by complex I don't mean just complex. I mean immensely difficult, bordering on impossible for an individual. You'd have to have a database of all roads to start with. Just use an API instead. See my comment on your question.
Blixt