0

我想獲得駕駛距離兩條經度緯度點之間的軌道,地理編碼器或任何其他有效的方式。 是否有?獲取兩個經度緯度點之間的駕駛距離

+2

你有做過任何研究嗎? – geocodezip

+0

[Google Maps API v3文檔](https://developers.google.com/maps/documentation/javascript/reference#DirectionsService)指定來源或目的地(或航點)可以是地址,也可以是[ google.maps.LatLng](https://developers.google.com/maps/documentation/javascript/reference#LatLng)。要獲取兩個經度/緯度點之間的駕駛距離,請將它們作爲google.maps.LatLng對象在[請求](https://developers.google.com/maps/documentation/javascript/reference#DirectionsRequest)中傳遞。 – geocodezip

+0

非常感謝你 – user1666543

回答

2

Google Maps API v3 documentation指定來源或目的地(或航點)可以是地址或google.maps.LatLng。要獲取兩個經度/緯度點之間的駕駛距離,請將它們作爲google.maps.LatLng對象傳遞給request

相關的問題:total distance and time for all waypoints in google maps v3

proof of concept fiddle

代碼段(基於關the example in the documentation):

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>

+0

精心製作的迴應請幫助 –

1
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; 
+0

這只是一個直線距離不是駕駛距離 –

0

如果你不想依賴(精心製作的)Google API或綁定API限制,我只是發現了另一個選項:OpenStreetMap的osrm-project

在我的情況下,我需要檢索近20萬個房屋的最近開車距離到某個特定的興趣點。

相關問題