Skip to content

Instantly share code, notes, and snippets.

@kitelife
Created May 10, 2015 13:15
Show Gist options
  • Save kitelife/c78623069e20598ce23d to your computer and use it in GitHub Desktop.
Save kitelife/c78623069e20598ce23d to your computer and use it in GitHub Desktop.
经纬度距离计算
var appFunc = {
getDistance: function (firstLon, firstLat, secondLon, secondLat) {
var radFirstLon = appFunc.rad(firstLon);
var radFirstLat = appFunc.rad(firstLat);
var radSecondLon = appFunc.rad(secondLon);
var radSecondLat = appFunc.rad(secondLat);
var latDiff = radFirstLat - radSecondLat;
var lonDiff = radFirstLon - radSecondLon;
var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(latDiff / 2), 2) + Math.cos(radFirstLat) * Math.cos(radSecondLat) * Math.pow(Math.sin(lonDiff / 2), 2)));
distance = distance * appFunc.EARTH_RADIUS;
distance = Math.round(distance * 10000) / 10000;
return distance;
},
EARTH_RADIUS: 6378.137,
rad: function (du) {
return du * Math.PI / 180.0;
}
};
// 纬度:latitude
// 经度:longitude
var firstLon = 121.413413;
var firstLat = 31.157984;
var secondLon = 121.604824;
var secondLat = 31.181937;
console.log(appFunc.getDistance(firstLon, firstLat, secondLon, secondLat));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment