Question:
I'm finishing a system in PHP, where the client asked if he has a way to generate a route between companies, and generate the shortest one, for example, he has 50 companies registered in the database, with address and everything else, he wants to select for example 10 of these companies to make deliveries, what he asked me was: Can I select the 10 companies, and the system retrieve the addresses, and it (with the help of google API) calculate the shortest route between these 10 points? I answered "good question" haha.
Jokes aside, I've never worked with the Google Maps API, does anyone have any tips, or help? And the question that does not want to remain silent, is there a way to pass these 10 addresses in the client's example by XML (or another type) for Google Maps to calculate the shortest route and show me that route?
Thank you everyone, as the saying goes, asking doesn't offend!
Answer:
For this you can use the Google Distance Matrix API to get the distance and travel time for an array of origins and destinations. This service does not return detailed route information.
The Distance Matrix API has the following format:
http://maps.googleapis.com/maps/api/distancematrix/output?parameters
The HTTPS protocol is recommended for applications that include sensitive user data, such as their location, in their requests:
https://maps.googleapis.com/maps/api/distancematrix/output?parameters
In both cases, output can be either JSON or XML.
Mandatory parameters
-
origins : One or more addresses and/or textual latitude/longitude values, separated by the pipe character (|), from which the distance and time are calculated. If you enter an address as a string, the service geocodes the string and converts it to a latitude/longitude coordinate to calculate routes. If you enter coordinates, make sure there is no space between latitude and longitude values.
origins=Bobcaygeon+ON|41.43206,-81.38992
-
destinations : One or more addresses and/or textual latitude/longitude values, separated by the pipe character (|), for which the distance and time are calculated. If you enter an address as a string, the service geocodes the string and converts it to a latitude/longitude coordinate to calculate routes. If you enter coordinates, make sure there is no space between latitude and longitude values.
destinations=Darling+Harbour+NSW+Australia|24+Sussex+Drive+Ottawa+ON|Capitola+CA
-
sensor : Indicates whether your app is using a sensor (for example, a GPS locator) to determine the user's location. This value must be true or false .
Optional parameters
- mode : Specifies which mode of transport to use when calculating routes. Valid values are:
- driving (default) : Indicates standard transit routes using the road network.
- walking : Requests walking routes through crosswalks and sidewalks (where available).
- bicycling : Request bike routes on preferred bike lanes and streets ( currently only available in the US and some Canadian cities ).
- language : The language in which results should be returned. See the list of supported domain languages . The list of supported languages is updated frequently; therefore, it is not definitive.
- avoid : Introduces restrictions on the route. Valid values are specified in the Restrictions section of this document. Only one restriction can be specified.
- units : Specifies the system of units to use when expressing distance as text. See the Drive Systems section of this document for more information.
Direction information can be obtained by providing the desired unique origin and destination to the Google Directions API .