简体   繁体   English

计算Google Maps for Ionic 2中两点之间的距离

[英]Calculating distance between two points in Google Maps for Ionic 2

I am having trouble starting with this new feature that I have to implement on an application that I am working on. 我开始使用这个我必须在我正在处理的应用程序上实现的新功能时遇到了问题。 So the application works like this; 所以应用程序就是这样的; A user inputs a point on the map by pressing a button and gets a latlng for source. 用户通过按下按钮在地图上输入一个点并获取源的latlng。 Another modal opens and then they have to do the same for destination. 另一个模态打开然后他们必须为目的地做同样的事情。 What I want to do is to calculate the distance between two points and show a route on the map. 我想要做的是计算两点之间的距离,并在地图上显示一条路线。 I have been trying to find tutorials on this but have had no luck. 我一直试图找到这方面的教程,但没有运气。 Anyone knows how to do this or have any working example repo that I can have a look at it? 任何人都知道如何做这个或有任何工作的例子回购,我可以看看它? Would be of great help. 会有很大的帮助。 Thanks! 谢谢!

Have you seen this: 你见过这个吗:

https://stackoverflow.com/a/27943/52160 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)
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM