【问题标题】:Calculating distance between two points in Google Maps for Ionic 2在 Google Maps for Ionic 2 中计算两点之间的距离
【发布时间】:2017-09-03 10:56:47
【问题描述】:

我在开始使用这个新功能时遇到了麻烦,我必须在我正在处理的应用程序上实现该功能。所以应用程序是这样工作的;用户通过按下按钮在地图上输入一个点并获得源的 latlng。另一个模式打开,然后他们必须为目的地做同样的事情。我想要做的是计算两点之间的距离并在地图上显示路线。我一直在尝试找到这方面的教程,但没有运气。任何人都知道如何做到这一点或有任何工作示例回购,我可以看看它?会有很大帮助。谢谢!

【问题讨论】:

标签: javascript google-maps typescript ionic2 google-maps-markers


【解决方案1】:

你见过吗:

https://stackoverflow.com/a/27943/52160

 function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
    var R = 6371; // Radius of the earth in km
    var dLat = deg2rad(lat2-lat1);  // deg2rad below
    var dLon = deg2rad(lon2-lon1); 
    var a =
        Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
        Math.sin(dLon/2) * Math.sin(dLon/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var d = R * c; // Distance in km
        return d;
    }

    function deg2rad(deg) {
      return deg * (Math.PI/180)
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2016-09-23
    • 2014-10-21
    • 2012-03-25
    • 1970-01-01
    相关资源
    最近更新 更多