3
$\begingroup$

I am trying to systematically obtain the distance between two given locations using the Google Maps API Distance Matrix facility: developers.google.com/maps/documentation/distancematrix/

From the example they give in the web:

I'd use:

URLFetch["http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Vancouver+BC&mode=bicycling&language=fr-FR&sensor=false"]

and then parse the xml data. Now if I want to do this for many cities, I would build a string with URL changing the appropriate origins and destinations and other parameters. However, it looks pretty cumbersome to me and I feel that there should be a more efficient way to do it. Perhaps using the parameters option in URLFetch.

So, there is the question, using the Google Maps API, given two locations is an more efficient (faster) way to obtain its distance?

Thanks in advance!

$\endgroup$
5
  • $\begingroup$ Please take a look at this post. $\endgroup$
    – Rod
    May 28, 2013 at 17:20
  • $\begingroup$ Possible duplicate IMO... $\endgroup$
    – Rod
    May 28, 2013 at 17:25
  • $\begingroup$ Thanks a lot! but as I mentioned, building a URL and then importing was the current state in which I find myself. What I was wondering is wether is there a way to do it through URLFetch and so, URLFetchAsynchronous, in order to do it for large datasets of cities, in the order of thousands. Currently is very slow.The coordinate gathering step is not an issue since my data is in google-friendly postcodes. Thanks! $\endgroup$
    – Bufarrull
    May 28, 2013 at 17:35
  • $\begingroup$ Have you seen the Usage limits and requirements? I'm afraid you won't be able to do it for a large dataset of cities... $\endgroup$
    – Rod
    May 28, 2013 at 17:54
  • $\begingroup$ It's alright, the free license allows for 2500 per day. But again, that is not the point. The point is how can I use efficiently URLFetch and its option Parameters, to do this job. Thanks again. $\endgroup$
    – Bufarrull
    May 28, 2013 at 18:37

1 Answer 1

3
$\begingroup$

One question is about efficiency ("faster"): The bottleneck to make it "faster" will be the client/server link, not Mathematica. Perhaps there's a more efficient way to use the API, but that is not a Mathematica question.

Another questionis about how to use the "Parameters" option: Replace the "=" by a Rule (->) and make the pieces strings:

URLFetch["http://maps.googleapis.com/maps/api/distancematrix/xml", 
 "Parameters" -> {"origins" -> "Vancouver+BC|Seattle", 
   "destinations" -> "San+Francisco|Vancouver+BC", 
   "mode" -> "bicycling", "language" -> "fr-FR", "sensor" -> "false"}]

Otherwise, I think you would be better off studying the answers to the same problem. For instance, the "JSON" Rule format seems much easier to deal with in Mathematica -- that is, it would be more efficient for the programmer.

$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.