开发者

Render google maps link as polyline

I want to display a couple of r开发者_Python百科outes on a map, but I would prefer to first draw them with google maps. For example, I got directions from Seattle to San Diego, then moved things a bit, and the link looks like this.

I know that I can use the DirectionsRenderer to draw a polyline connecting Seattle and San Diego like this:

  function renderDirections(result) { 
    var directionsRenderer = new google.maps.DirectionsRenderer;
    directionsRenderer.setMap(gMap);
    directionsRenderer.setDirections(result);
  } 

  var directionsService = new google.maps.DirectionsService;
  function requestDirections(start, end) { 
    directionsService.route({
      origin: start,
      destination: end,
      travelMode: google.maps.DirectionsTravelMode.BICYCLING
    }, function(result) { 
      renderDirections(result);
    });
  } 
  requestDirections('Seattle, WA', 'San Diego, CA');

What I would like to know is if there is a way to pass in the link as the directions request. The link contains waypoints, my modifications to the default route.


It is possible and you are on the right track. It is hard to understand the API. I believe that you have to set the waypoints in the DirectionRequest object of the DirectionsService when you call the route method. I don't think you can pass in a link, but you can create an object or Array of waypoints first.

If you want, you can also specify the optimizeWaypoints boolean. Check out the DirectionsRequest Object.

waypoints Array. Array of intermediate waypoints. Directions will be calculated from the origin to the destination by way of each waypoint in this array. Optional.


Yes, you can use the DirectionsRenderer so long as you pass your start and end points into a DirectionsRequest, and pass that into a DirectionsService object. Once you call .setDirections it'll draw the polyline for you. From the API documentation at.

Set the renderer to use the result from the DirectionsService. Setting a valid set of directions in this manner will display the directions on the renderer's designated map and panel.

If what you were getting at was drawing the polyline yourself (though I don't see why it would be necessary), the individual points in the path can be derived -- DirectionsResult contains an array of DirectionsLegs which contains an array of DirectionsSteps which contains a .path property, which is an array of latlngs. (whew!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜