【问题标题】:Google Driving Directions To Nearest Road谷歌行车路线到最近的道路
【发布时间】:2016-03-29 09:05:18
【问题描述】:

我希望在两个点之间创建行车路线,其中一个或两个点没有直接通往它的道路,而是靠近它。

例如,如果您使用 DirectionsService 尝试在犹他州摩押和犹他州锡安国家公园之间创建一条行车路线,您将返回 Zero_Results,因为没有通往锡安 CENTER(谷歌返回的纬度、经度)的道路国家公园。如果您在 google.com/maps 上执行相同操作,您会看到一条从摩押到锡安国家公园东入口的车道,然后步行到公园中心(放置大头针的地方)。他们是如何决定开车去锡安国家公园的?

【问题讨论】:

  • 欢迎来到 StackOverflow!对于您的问题,您尝试了哪些方法/算法?这里的人们很乐意提供帮助,但我们不一定要为您进行研究/编码。如果你展示你已经尝试过的研究/方法等,你会得到很好的回应。
  • 我不想要代码。我已经写了很多来尝试解决这个问题。我更想知道如何做到这一点。据我所知,API 中没有办法告诉 DirectionsService 在创建 Drive 时找到最近的道路。这意味着需要定制解决方案。我尝试使用半径合适的附近搜索,但问题是您实际上并不知道哪个端点导致返回 ZERO_RESULTS。我只是想弄清楚是否有我错过的请求选项,或者是否有人对如何解决这个问题有一个好主意。不需要代码。

标签: javascript google-maps google-maps-api-3


【解决方案1】:

如果您reverse geocode 由地理编码器返回的锡安国家公园坐标 (37.2982022,-113.0263005),第一个结果将是道路上最近的位置。

proof of concept fiddle

代码 sn-p:

var geocoder;
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var start = "Moab, UT";
  directionsDisplay.setMap(map);
  geocodeAddress("Zion National Park", start);
}

function geocodeAddress(address, start) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      geocoder.geocode({
        'location': results[0].geometry.location
      }, function(results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
          calculateAndDisplayRoute(start, results[0].geometry.location)
        } else {
          window.alert('Reverse Geocode failed due to: ' + status);
        }
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

function calculateAndDisplayRoute(start, end) {
  directionsService.route({
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.DRIVING
  }, function(response, status) {
    if (status === google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    } else {
      window.alert('Directions request failed due to ' + status);
    }
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2012-09-11
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多