首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获得两个经纬度点之间的驾驶距离

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

Stack Overflow用户
提问于 2013-01-03 01:04:39
回答 3查看 3.5K关注 0票数 0

我试图获得驾驶距离之间的两个经纬度点的铁路,大地编码器或任何其他有效的方式。在那里吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-03 14:23:37

Google v3文档指定源或目的地(或路径点)既可以是地址,也可以是google.maps.LatLng。要获得两个经纬度点之间的驱动距离,在请求中将它们作为google.maps.LatLng对象传递。

相关问题:谷歌地图中所有路径点的总距离和时间v3

概念小提琴的证明

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

代码语言:javascript
复制
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);
代码语言:javascript
复制
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
代码语言:javascript
复制
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="distance"></div>
<div id="map"></div>

票数 2
EN

Stack Overflow用户

发布于 2013-06-13 09:54:48

代码语言:javascript
复制
    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;
票数 1
EN

Stack Overflow用户

发布于 2017-11-02 19:06:47

如果您不想依赖(精巧的) Google或绑定到API限制,我刚刚发现了另一个选项:OpenStreetMap的osrm-项目

在我的例子中,我需要从近20万所房子的最近的开车距离到一个特定的兴趣点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14131708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档