tomazj
tomazj

Reputation: 313

Google maps - directions service api ZERO_RESULTS

I have a question regarding directions service API. Is there a hybrid travel mode setting which would allow me to draw a route for a bike, even if there isn't a bike track on that particular street or it's against street rules (is it even possible). I would like to draw a segment which will go from A to B exactly through selected road, and not around if that road doesn't actually allow driving in that direction. It doesn't have to be correct in regard to driving rules, it must be just drawn over the street. I tried bicycle mode instead of driving mode, but there isn't any difference.

I hope my post makes any sense

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

Example of call:

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);
    }
  });
}

Upvotes: 1

Views: 2748

Answers (1)

geocodezip
geocodezip

Reputation: 161404

You could use TravelMode.WALKING, that tends to give results for one way roads and other routes that won't work for TravelMode.DRIVING. The code in your question doesn't reproduce the picture you posted, but using TravelMode.WALKING returns a route (where TravelMode.BICYCLING gives ZERO_RESULTS for the posted code)

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

code snippet:

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>

Upvotes: 1

Related Questions