计算两点之间的行驶距离

时间:2016-07-14 09:54:01

标签: javascript google-maps dictionary latitude-longitude

我想测量两个地方之间的道路距离,条件是地点的纬度和经度。

我的基于JavaScript / Java的自定义应用程序将由离线用户使用。

我认为在这种情况下无法使用Google地图API。我需要免费服务。 arcgis提供这个设施??

任何帮助或指南都将受到赞赏..

1 个答案:

答案 0 :(得分:1)

我认为这对你有用,假设你有两点的经纬度:

var rad = function(x) {
  return x * Math.PI / 180;
};

var getDistance = function(p1, p2) {
  var R = 6371; // Earth’s mean radius in meter
  var dLat = rad(p2.lat() - p1.lat());
  var dLong = rad(p2.lng() - p1.lng());
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
    Math.sin(dLong / 2) * Math.sin(dLong / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  return parseInt(d); // returns the distance in km
};

相关问题