Using Waypoints in Routes
As noted within the DirectionsRequest, you may also specify waypoints (of type DirectionsWaypoint
) when calculating routes using the Directions service. Waypoints allow you to calculate routes through additional locations, in which case the returned route passes through the given waypoints.
A waypoint
consists of the following fields:
location
(required) specifies the address of the waypoint.stopover
(optional) indicates whether this waypoint is a actual stop on the route (true
) or instead only a preference to route through the indicated location (false
). Stopovers aretrue
by default.
optimizeWaypoints: true
within the DirectionsRequest
to allow the Directions service to optimize the provided route by rearranging the waypoints in a more efficient order. (This optimization is an application of the Travelling Salesman Problem.) All waypoints must be stopovers for the Directions service to optimize their route.
If you instruct the Directions service to optimize the order of its waypoints, their order will be returned in the optimized_waypoints_order
field within the DirectionsResult
object.
The following example calculates cross-country routes across the United States using a variety of start points, end points, and waypoints. (To select multiple waypoints, press Ctrl-Click when selecting items within the list.) Note that we inspect the routes.start_geocode.formatted_address
and routes.end_geocode.formatted_address
to provide us with the text for each route's start and end point.
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: chicago
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var waypts = [];
var checkboxArray = document.getElementById("waypoints");
for (var i = 0; i < checkboxArray.length; i++) {
if (checkboxArray.options[i].selected == true) {
waypts.push({
location:checkboxArray[i].value,
stopover:true
});
}
}
var request = {
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var route = response.routes[0];
var summaryPanel = document.getElementById("directions_panel");
summaryPanel.innerHTML = "";
// For each route, display summary information.
for (var i = 0; i < route.legs.length; i++) {
var routeSegment = i+1;
summaryPanel.innerHTML += "<b>Route Segment: " + routeSegment + "</b><br />";
summaryPanel.innerHTML += route.legs[i].start_geocode.formatted_address + " to ";
summaryPanel.innerHTML += route.legs[i].end_geocode.formatted_address + "<br />";
summaryPanel.innerHTML += route.legs[i].distance.text + "<br /><br />";
}
}
});
}
http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/services.html#Geocoding
http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/examples/directions-waypoints.html
《Google Map – Travel Modes》有1个想法
Hi blogger, i must say you have very interesting articles here.
Your website should go viral. You need initial traffic only.
How to get it? Search for: Mertiso’s tips go viral