A
A
Alex2015-01-21 13:39:22
OpenStreetMap
Alex, 2015-01-21 13:39:22

Route by multiple points on maps (google, osm)?

Good afternoon!
I ran into a problem, I can’t find the following in the documentation:
How to build an optimal route between three or more points.
Those. there is information on OSM or google how to put poi, or how to connect 2 poi by walking type of movement.
But there is no information on how to connect several points.
Google maps:

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

    function initialize() {
      directionsDisplay = new google.maps.DirectionsRenderer();
      var center = new google.maps.LatLng(40.7791666666667,-73.9583333333333);
      var mapOptions = {
        zoom: 13,
        center: center
      }
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      directionsDisplay.setMap(map);
    }

    function getRoute(start, end, poi) {
      var waypts = [];
      for (var i = 0; i < poi.length; i++) {
        //console.log(poi[i])
          waypts.push({
            location:poi[i],
            stopover:true});

      }
      var request = {
        origin: start,
        destination: end,
        waypoints: waypts,
        optimizeWaypoints: true,
        travelMode: google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function(response, status) {
        console.log(response)
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        }
      });
    }

    google.maps.event.addDomListener(window, 'load', initialize);

    var start = '40.7791666666667,-73.9583333333333';
    var end = '40.7752777777778,-73.9769444444444';
    var waypoints = [
      '40.7513888888889,-73.9777777777778',
      '40.7155555555556,-74.0111111111111'
    ];

    getRoute(start, end, waypoints);

Starts cursing:
TypeError: undefined is not an object (evaluating 'directionsDisplay.setDirections')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergeyj, 2015-01-21
@sensus

function initialize() {
      directionsDisplay = new google.maps.DirectionsRenderer();
      var center = new google.maps.LatLng(40.7791666666667,-73.9583333333333);
      var mapOptions = {
        zoom: 13,
        center: center
      }
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      directionsDisplay.setMap(map);

      var start = "40.7791666666667,-73.9583333333333";
      var end = "40.7752777777778,-73.9769444444444";
      var waypoints = [
        "40.7513888888889,-73.9777777777778",
        "40.7155555555556,-74.0111111111111",
      ];

      getRoute(start, end, waypoints);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question