Getting distance and time driving between to places using google maps

312 views
Skip to first unread message

omera...@gmail.com

unread,
Jul 25, 2014, 3:19:12 PM7/25/14
to mitappinv...@googlegroups.com
Hello

Is there any way to input 2 locations into my program and get only the distance and time of driving between these two places using google maps?

Thanks!

SteveJG

unread,
Jul 25, 2014, 3:34:33 PM7/25/14
to
Yes.   The way is documented here:  https://developers.google.com/maps/documentation/distancematrix/

You can make a JSON or XML request.    When you read the request, you will have to parse the output.   The component to use is the Web component.


Be aware:

Use of the Distance Matrix API must relate to the display of information on a Google Map; for example, to determine origin-destination pairs that fall within a specific driving time from one another, before requesting and displaying those destinations on a map. Use of the service in an application that doesn't display a Google map is prohibited.

Regards,
Steve
Message has been deleted

omera...@gmail.com

unread,
Jul 26, 2014, 2:51:19 AM7/26/14
to mitappinv...@googlegroups.com
So there is no other way that i can get distance and time between two places?

omera...@gmail.com

unread,
Jul 26, 2014, 2:53:30 AM7/26/14
to mitappinv...@googlegroups.com
Maybe I can show the map and then to get the distance and time?

If I can do this, can someone help me with the blocks?

SteveJG

unread,
Jul 26, 2014, 10:21:30 AM7/26/14
to
If you do this "Maybe I can show the map and then to get the distance and time?"   in your app (display a Google Map) then it appears you satisfy one of the requirements for using their API.

Blocks to show a map:    Have you done the Where Is my Car tutorial?   That tutorial shows how to use Activity.Starter within AI2 to open a Google Map.    The tutorial is here:http://appinventor.mit.edu/explore/ai2/android-wheres-my-car.html   


Can you calculate distance and time without the Google API?   Yes, however the math can be very complicated.    You can calculate a straight-line distance using the Haversine formula :

Here is a Haversine formula  in Java code:

var rad = function(x) {
  return x * Math.PI / 180;
};

var getDistance = function(p1, p2) {
  var R = 6378137; // Earth’s mean radius in meter
  var dLat = rad(p2.lat() - p1.lat());
  var dLong = rad(p2.lng() - p1.lng());
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
    Math.sin(dLong / 2) * Math.sin(dLong / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  return d; // returns the distance in meter
};

The Java code provides a result in meters or perhaps kilometers that is only reliable over relatively small distances.    The problem is, this algorithm does not calculate the path of a car, it calculates a point to point distance..    You can calculate driving time by calculating the distance and dividing by your average anticipated driving speed.

Google's API is remarkable, to duplicate that performance in code only on an Android device with AI2?     Possible but improbable.

Regards,
Steve

Reply all
Reply to author
Forward
0 new messages