我已经可以获取地图和两点之间的路线,我想计算两点之间的距离(以千米为单位)>
跟踪路线的代码
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 ...
response.routes
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);
}