首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌驾驶路线到最近的道路

谷歌驾驶路线到最近的道路
EN

Stack Overflow用户
提问于 2016-03-29 01:20:32
回答 1查看 405关注 0票数 0

我希望在两个点之间创建驾驶方向,其中一个或两个点没有直接通往它的道路,但靠近它。

例如,如果您使用DirectionsService尝试创建一条之间的摩押,犹他州和锡安国家公园,锡安国家公园,UT你将返回Zero_Results,因为没有道路到中心(最后,谷歌返回的液化天然气)锡安国家公园。如果你在google.com/map上做同样的事情,你会看到一条从摩押到锡安国家公园东门的车道线,并步行到公园的中心(大头针所在的地方)。他们是怎么决定去锡安国家公园的呢?

EN

回答 1

Stack Overflow用户

发布于 2016-03-29 03:07:37

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

proof of concept fiddle

代码片段:

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

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

https://stackoverflow.com/questions/36267314

复制
相关文章

相似问题

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