V
V
Vladislav Boychenko2018-04-13 20:44:22
JavaScript
Vladislav Boychenko, 2018-04-13 20:44:22

How to plot a route by given points in Google Maps API v3?

Good evening.
Here is the subject: dev.boichenko.space/lutsk
The essence of the problem: you need to lay a route for certain points.
How I do it: First I tried with "google.maps.Polyline", but it's incredibly stupid, because you can only draw straight lines, and you have to add a new point at each bend in the road. Now I decided to try it through the "directionsService", but it determines how to lay the path between the points, even though I add "waypoints". Is there any way to get Google Maps API v3 to pave my waypoints? For example, as in ordinary Google maps, where you can pave the way in such a way by personally determining the points.
Thank you!
Here is the part of the code responsible for making the path:

var directionsService = new google.maps.DirectionsService;
  var directionsDisplay = new google.maps.DirectionsRenderer;
  directionsDisplay.setOptions({suppressMarkers: true});
  
  directionsDisplay.setMap(map);

  var waypts = [
    {location: {lat: 50.747796, lng: 25.323531}, stopover: true},
    {location: {lat: 50.745726, lng: 25.319925}, stopover: true},
    {location: {lat: 50.743555, lng: 25.325369}, stopover: true},
    {location: {lat: 50.741620, lng: 25.322099}, stopover: true},
    {location: {lat: 50.742370, lng: 25.317764}, stopover: true},
    {location: {lat: 50.740230, lng: 25.317296}, stopover: true},
  ];

  var request = {
    origin: new google.maps.LatLng(50.746957,25.325646), //точка старта
    destination: new google.maps.LatLng(50.741453,25.317290), //точка финиша
    waypoints: waypts, 
    optimizeWaypoints: true, 
    travelMode: google.maps.DirectionsTravelMode.WALKING //режим прокладки маршрута
  };
  directionsService.route(request, function(response, status){
    if (status == google.maps.DirectionsStatus.OK){
      directionsDisplay.setDirections(response);
    }
  });
  directionsDisplay.setMap(map);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2018-04-13
@freeExec

And why didn’t you sign other parameters, mindlessly copy?

optimizeWaypoints (optional) - Indicates that a route using the provided waypoints can be optimized by placing these waypoints in a more efficient order . If this field is set to true, the Directions service will return waypoints in the changed order in the waypoint_order field.

E
Eugene, 2018-04-14
@evgen9586

We read the documentation with a good example
https://developers.google.com/maps/documentation/j...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question