Google Maps Api - Adding Multiple Destinations Not Working (google Directions)
I am having trouble creating/replicating the google maps directions function. I am able to get it working fine when I have a From/To field but as soon as I try to add multiple dest
Solution 1:
This is how I have handled multiple way point directions.
vardirectionsService=newgoogle.maps.DirectionsService();
varrenderOptions= { draggable: true };
vardirectionDisplay=newgoogle.maps.DirectionsRenderer(renderOptions);
//set the directions display service to the map
directionDisplay.setMap(map);
//set the directions display panel//panel is usually just and empty div. //This is where the turn by turn directions appear.
directionDisplay.setPanel(directionsPanel);
//build the waypoints//free api allows a max of 9 total stops including the start and end address//premier allows a total of 25 stops. varitems= ["address 1", "address 2", "address 3"];
varwaypoints= [];
for (vari=0; i < items.length; i++) {
varaddress= items[i];
if (address !== "") {
waypoints.push({
location: address,
stopover: true
});
}
}
//set the starting address and destination addressvaroriginAddress="starting address";
vardestinationAddress="destination address";
//build directions requestvarrequest= {
origin: originAddress,
destination: destinationAddress,
waypoints: waypoints, //an array of waypoints
optimizeWaypoints: true, //set to true if you want google to determine the shortest route or false to use the order specified.
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
//get the route from the directions service
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionDisplay.setDirections(response);
}
else {
//handle error
}
});
Solution 2:
I was searching for how to add multiple destinations to the google map api, this came up in the top search result but it doesn't help much. Finally found the answer in google doc, You can supply one or more locations separated by the pipe character (|), in the form of an address, latitude/longitude coordinates, or a place ID. For example, this request will give you the distance information from Miami to 3 destinations.
http://maps.googleapis.com/maps/api/distancematrix/json?destinations=Washington,DC|New+York,NY|Los+Angeles,CA&origins=Miami,FL&units=imperial
It will return a json like this:
{
"destination_addresses" : [ "Washington, DC, USA", "New York, NY, USA", "Los Angeles, CA, USA" ],
"origin_addresses" : [ "Miami, FL, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1,053 mi",
"value" : 1693921
},
"duration" : {
"text" : "14 hours 56 mins",
"value" : 53781
},
"status" : "OK"
},
{
"distance" : {
"text" : "1,277 mi",
"value" : 2054642
},
"duration" : {
"text" : "18 hours 24 mins",
"value" : 66219
},
"status" : "OK"
},
{
"distance" : {
"text" : "2,733 mi",
"value" : 4397976
},
"duration" : {
"text" : "1 day 14 hours",
"value" : 138230
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
Post a Comment for "Google Maps Api - Adding Multiple Destinations Not Working (google Directions)"