谷歌地图 - 路线服务api ZERO_RESULTS

时间:2018-06-09 18:29:50

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

我对路线服务API有疑问。是否有混合动力旅行模式设置,这将允许我为自行车绘制路线,即使在该特定街道上没有自行车道或者它违反街道规则(甚至可能)。 我想绘制一段从A到B完全通过选定的道路,而不是那条道路 实际上并不允许朝那个方向开车。在驾驶规则方面,它不必是正确的,它必须只是在街道上绘制。我试过自行车模式而不是驾驶模式,但没有任何区别。

我希望我的帖子有意义

In blue is route made by google, I need route in yellow

电话示例:

function loadRoute0() {
  var request0 = {
    origin: new google.maps.LatLng(46.56300788, 15.62779705),
    destination: new google.maps.LatLng(46.55953332, 15.62616729),
    travelMode: google.maps.TravelMode.BICYCLING
  };

  directionsService.route(request0, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      var renderer = new google.maps.DirectionsRenderer({
        polylineOptions: {
          strokeColor: "#00FF00"
        },
        suppressMarkers: true,
        map: map
      });
      renderer.setDirections(result);
    }
  });
}

1 个答案:

答案 0 :(得分:1)

您可以使用TravelMode.WALKING,这往往会为单向道路和其他不适用于TravelMode.DRIVING的路线提供结果。 您问题中的代码不会重现您发布的图片,但使用TravelMode.WALKING会返回一条路线(TravelMode.BICYCLING为发布的代码提供ZERO_RESULTS

function loadRoute0() {
  var request0 = {
    origin: new google.maps.LatLng(46.56300788, 15.62779705),
    destination: new google.maps.LatLng(46.55953332, 15.62616729),
    travelMode: google.maps.TravelMode.WALKING
  };
  directionsService.route(request0, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      var renderer = new google.maps.DirectionsRenderer({
        polylineOptions: {
          strokeColor: "#00FF00"
        },
        suppressMarkers: true,
        map: map
      });
      renderer.setDirections(result);
    } else
      console.log("status=" + status);
  });
}

screenshot of resulting map

screenshot of resulting map

代码段

var map;

function initialize() {
  map = new google.maps.Map(document.getElementById("map_canvas"));
  directionsService = new google.maps.DirectionsService();
  loadRoute0();

  function loadRoute0() {
    var request0 = {
      origin: new google.maps.LatLng(46.56300788, 15.62779705),
      destination: new google.maps.LatLng(46.55953332, 15.62616729),
      travelMode: google.maps.TravelMode.WALKING
    };
    var markerS = new google.maps.Marker({
      position: request0.origin,
      map: map,
      label: "S"
    });
    var markerE = new google.maps.Marker({
      position: request0.destination,
      map: map,
      label: "E"
    });
    directionsService.route(request0, function(result, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        var renderer = new google.maps.DirectionsRenderer({
          polylineOptions: {
            strokeColor: "#00FF00"
          },
          suppressMarkers: true,
          map: map
        });
        renderer.setDirections(result);
      } else
        console.log("status=" + 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>