**News Alert:**Experts Exchange Confirmed as Safe in Cloudbleed Leak Read More

Solved

Posted on 2014-04-07

Hi,

I am having trouble getting an accurate distance between two lat/lon coordinates. This site, give the correct result which is 2 meters.

http://boulter.com/gps/distance/?from=53.1330940+-1.1843660&to=53.1331060+-1.1843260&units=k

Here is the code I am using :

This return 2.98 meters as do a lot of other online versions of this calculation. the correct distance is actually 2 meters. Am I missing something?

My coords are :

FROM

53.1330940 -1.1843660

TO

53.1331060 -1.1843260

ANy help appreciated!

Thanks

I am having trouble getting an accurate distance between two lat/lon coordinates. This site, give the correct result which is 2 meters.

http://boulter.com/gps/distance/?from=53.1330940+-1.1843660&to=53.1331060+-1.1843260&units=k

Here is the code I am using :

```
function distance(lat1, lon1, lat2, lon2, 'K') {
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var radlon1 = Math.PI * lon1/180
var radlon2 = Math.PI * lon2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
return dist
}
```

This return 2.98 meters as do a lot of other online versions of this calculation. the correct distance is actually 2 meters. Am I missing something?

My coords are :

FROM

53.1330940 -1.1843660

TO

53.1331060 -1.1843260

ANy help appreciated!

Thanks

2 Comments

http://jsbin.com/vumikayi/1/edit

```
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::: :::
//::: This routine calculates the distance between two points (given the :::
//::: latitude/longitude of those points). It is being used to calculate :::
//::: the distance between two locations using GeoDataSource (TM) prodducts :::
//::: :::
//::: Definitions: :::
//::: South latitudes are negative, east longitudes are positive :::
//::: :::
//::: Passed to function: :::
//::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :::
//::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :::
//::: unit = the unit you desire for results :::
//::: where: 'M' is statute miles :::
//::: 'K' is kilometers (default) :::
//::: 'N' is nautical miles :::
//::: :::
//::: Worldwide cities and other features databases with latitude longitude :::
//::: are available at http://www.geodatasource.com :::
//::: :::
//::: For enquiries, please contact sales@geodatasource.com :::
//::: :::
//::: Official Web site: http://www.geodatasource.com :::
//::: :::
//::: GeoDataSource.com (C) All Rights Reserved 2014 :::
//::: :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function distance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var radlon1 = Math.PI * lon1/180
var radlon2 = Math.PI * lon2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
return dist
}
/*
53.1330940 -1.1843660
TO
53.1331060 -1.1843260
*/
var lat1=53.1330940;
var lon1=-1.1843660;
var lat2=53.1331060;
var lon2=-1.1843260;
var kilometers=distance(lat1, lon1, lat2, lon2, 'k');
meters = kilometers*1000
alert(meters);
```

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Title | # Comments | Views | Activity |
---|---|---|---|

Jquery GMAP3 Hierarchy | 1 | 28 | |

Web forms being filled out by bots | 4 | 37 | |

insert a falling animation of heart in php | 4 | 24 | |

Jquery Check against database on select from auto fill not working in all browsers | 5 | 32 |

Join the community of 500,000 technology professionals and ask your questions.