使用离子4中的地理位置计算距离

问题描述 投票:0回答:2

我已经可以获取地图和两点之间的路线,我想计算两点之间的距离(以千米为单位)>

跟踪路线的代码

displayDirection(_origin, destin) {
    var ref = this;
    console.log('DIRECTION');
    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer();

    directionsDisplay.setMap(ref.map)


    directionsService.route({
      origin: _origin,
      destination: destin,
      travelMode: 'DRIVING'
    }, (response, status) => {
      console.log("Response: " + response + " Status: " + status);

      if (status === 'OK') {
        directionsDisplay.setDirections(response);
      } else {
        console.log("Can't set directions due to " + response);
      }
    });
  }

有人可以帮我吗?

我已经可以获取地图和两点之间的路线,我想计算两点之间的距离(以千米为单位,代码可以跟踪路线displayDirection(_origin,destin){var ref ...

ionic-framework geolocation ionic4 ionic-native directions
2个回答
0
投票
如果接受您收到的响应,则将在对象下显示一个路由数组

response.routes


0
投票
获得两点之间的距离

getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { var R = 6371; // Radius of the earth in km var dLat = this.deg2rad(lat2-lat1); // deg2rad below var dLon = this.deg2rad(lon2-lon1); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(this.deg2rad(lat1)) * Math.cos(this.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.toFixed(2); }

© www.soinside.com 2019 - 2024. All rights reserved.