获得两个经度纬度点之间的驾驶距离 [英] get driving distance between two longitude latitude points

查看:135
本文介绍了获得两个经度纬度点之间的驾驶距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过rails,geocoder或其他有效方式在两个经度纬度点之间获得驾驶距离
是否有?

解决方案

Google地图API v3文档指定来源或目的地(或路标)可以是地址,也可以是 google.maps.LatLng 。要获取两个经度/纬度点之间的驾驶距离,请在请求 as google.maps.LatLng objects。



相关问题:



< a href =http://jsfiddle.net/geocodezip/nzokcse9/1/ =nofollow noreferrer>概念证明小提琴



code snippet (基于文档中的示例):
$ b

 函数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(地图); //纽约,纽约州,美国(40.7127837,-74.0059413)var start = new google.maps.LatLng(40.7127837,-74.0059413); //美国马里兰州巴尔的摩(39.2903848,-76.6121893)var end = new google.maps.LatLng(39.2903848,-76.6121893); calculateAndDisplayRoute(start,end,directionsService,directionsDisplay);}函数calculateAndDisplayRoute(start,end,directionsService,directionsDisplay){directionsService.route({origin:start,destination:end,travelMode:google.maps.TravelMode.DRIVING},function响应,状态){if(status === google.maps.DirectionsStatus.OK){var totaldistance = 0; var route = response.routes [0]; //显示总距离信息for(var i = 0; i < route.legs.length; i ++){totaldistance = totaldistance + route.legs [i] .distance.value;} document.getElementById('distance')。innerHTML + =< p>总距离为+( totalDistance / 1000).toFixed(2)+km 

; directionsDisplay.setDirections(response);} else {window.alert('Directions requests failed to'+'status);}});} google。 maps.event.addDomListener(window,load,initMap);

  html,body,#map {height:100%;宽度:100%; margin:0px; < script src =https://   


I am trying to get driving distance between two longitude latitude points on rails, geocoder or any other efficient way.. Is there?

解决方案

The Google Maps API v3 documentation specifies that either the origin or the destination (or waypoints) can be either an address or a google.maps.LatLng. To get the driving distance between two longitude/latitude points, pass them in the request as google.maps.LatLng objects.

related question: total distance and time for all waypoints in google maps v3

proof of concept fiddle

code snippet (based off 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>

这篇关于获得两个经度纬度点之间的驾驶距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆