得到两个经纬度点之间的行驶距离(get driving distance between two l

2019-07-17 18:32发布

我试图让在轨道上,地理编码器2个经纬度点或任何其他有效的方法之间的行车距离 ..有?

Answer 1:

在谷歌地图API V3文档指定无论是原产地或目的地(或航点)可以是一个地址或google.maps.LatLng 。 为了得到两个经度/纬度点之间的行驶距离,将其传入请求为google.maps.LatLng物件。

相关的问题: 在谷歌地图V3所有航点总距离和时间

概念小提琴证明

代码段 (基于关闭所述文档中的示例 ):

 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 += "<p>total distance is " + (totaldistance / 1000).toFixed(2) + " km</p>"; 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 } 
 <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="distance"></div> <div id="map"></div> 



Answer 2:

    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;


Answer 3:

如果你不想依赖(令人惊讶的精雕细琢)谷歌API或绑定到API限制我刚刚发现了另一种选择:OpenStreetMap的OSRM项目 。

就我而言,我需要获取感兴趣的特定点近20万的房子最近的行驶距离。



文章来源: get driving distance between two longitude latitude points