VISHMAY
VISHMAY

Reputation: 709

Google Map distance-matrix api Serverside multiple address by latlong

In https://www.npmjs.com/package/google-distance-matrix example we can add multiple destincation as latlong pair,

The server side code is like

string Test = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Lat1,long1&destinations=Lat2,long2&sensor=false"

Now the question is how to use it in multiple origin destination mode Can we something like

string Test = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Lat1,long1,lat2,long2&destinations=Lat3,long3,Lat4,long4&sensor=false"

Example

http://maps.googleapis.com/maps/api/distancematrix/json?origins=23.020284,72.4396566&destinations=22.3039702,70.7671403&sensor=false http://maps.googleapis.com/maps/api/distancematrix/json?origins=23.027100,72.508000&destinations=22.999500,72.600300&sensor=false

Combo of this two

http://maps.googleapis.com/maps/api/distancematrix/json?origins=23.020284,72.4396566,23.027100,72.508000&destinations=22.3039702,70.7671403,22.999500,72.600300&sensor=false

Upvotes: 4

Views: 7956

Answers (4)

eriel marimon
eriel marimon

Reputation: 1380

Another cool way to get directions for multiple coordinates is to encode all origins and destinations into polylines (Google's Encoded Polyline Algorithm).

Google Docs: Encoded Polyline

Alternatively, you can supply an encoded set of coordinates using the Encoded Polyline Algorithm. This is particularly useful if you have a large number of origin points, because the URL is significantly shorter when using an encoded polyline. - Encoded polylines must be prefixed with enc: and followed by a colon :. For example: origins=enc:gfo}EtohhU: - You can also include multiple encoded polylines, separated by the pipe character |. For example: origins=enc:wc~oAwquwMdlTxiKtqLyiK:|enc:c~vnAamswMvlTor@tjGi}L:|enc:udymA{~bxM:

There are client libraries already built for most common languages. For example klaxit/fast-polylines (github link) gem for ruby.

Usage

require "fast_polylines"

origins = FastPolylines.encode([[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]])
destinations = FastPolylines.encode([[38.6, -120.4], [40.4, -120.2], [43.26, -126.463]])


url = http://maps.googleapis.com/maps/api/distancematrix/json?origins=enc:#{origins}:&destinations=enc:#{destinations}:&sensor=false$key=AIza....OgXQ

*** Note that encoded origins and destinations need the enc: prefix and : suffix. Ex: origins=enc:XXXX:

Upvotes: 0

eriel marimon
eriel marimon

Reputation: 1380

OPs own answer works, just adding one with a clear example. Notice the pipe (|) separation between lat, long pairs. an array of arrays [[a,b], [c,d]] would not work.

http://maps.googleapis.com/maps/api/distancematrix/json?origins=23.020284,72.4396566|23.027100,72.508000&destinations=22.3039702,70.7671403|22.999500,72.600300&sensor=false$key=AIza....OgXQ

Upvotes: 0

Praveen Kumar
Praveen Kumar

Reputation: 1

Found the solution .. use the link in this format :https://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&key=YOUR_API_KEY. this is working fine

Upvotes: -1

VISHMAY
VISHMAY

Reputation: 709

I solved it by using "|" delimiter. Thank you every one

Upvotes: 18

Related Questions