获得两个经度纬度点之间的行驶距离

我试图在轨道,地理编码器或任何其他有效方式上获得两个经度纬度点之间的行驶距离 ..有吗?

Google Maps API v3文档指定原点或目的地(或路标)可以是地址或google.maps.LatLng 。 要获得两个经度/纬度点之间的行驶距离,请将它们作为google.maps.LatLng对象传递给请求 。

相关问题: Google地图v3中所有航点的总距离和时间

概念certificate小提琴

代码段 (基于文档中的示例 ):

function initMap() { var directionsService = new google.maps.DirectionsService; var directionsDisplay = new google.maps.DirectionsRenderer; var map = new google.maps.Map(document.getElementById('map'), { zoom: 7, center: { lat: 41.85, lng: -87.65 } }); directionsDisplay.setMap(map); // New York, NY, USA (40.7127837, -74.0059413) var start = new google.maps.LatLng(40.7127837, -74.0059413); //Baltimore, MD, USA (39.2903848, -76.6121893) var end = new google.maps.LatLng(39.2903848, -76.6121893); calculateAndDisplayRoute(start, end, directionsService, directionsDisplay); } function calculateAndDisplayRoute(start, end, directionsService, directionsDisplay) { directionsService.route({ origin: start, destination: end, travelMode: google.maps.TravelMode.DRIVING }, function(response, status) { if (status === google.maps.DirectionsStatus.OK) { var totaldistance = 0; var route = response.routes[0]; // display total distance information. for (var i = 0; i < route.legs.length; i++) { totaldistance = totaldistance + route.legs[i].distance.value; } document.getElementById('distance').innerHTML += "

total distance is " + (totaldistance / 1000).toFixed(2) + " km

"; directionsDisplay.setDirections(response); } else { window.alert('Directions request failed due to ' + status); } }); } google.maps.event.addDomListener(window, "load", initMap);
 html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px } 
  
  var R = 6371; var dLat = toRad(lat2-lat1); var dLon = toRad(lon2-lon1); var dLat1 = toRad(lat1); var dLat2 = toRad(lat2); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(dLat1) * Math.cos(dLat1) * Math.sin(dLon/2) * Math.sin(dLon/2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; alert(d); function toRad(Value) { /** Converts numeric degrees to radians */ return Value * Math.PI / 180; 

如果您不想依赖(精心设计的)Google API或受限于API限制,我只是发现了另一种选择:OpenStreetMap的osrm项目 。

在我的情况下,我需要检索近200,000个房屋的最近驾驶距离到某个兴趣点。