How to make a map app to return only (driving) distance in miles?

I seen tutorials about showing a map and other things but, I need the ability to input 2 address and it returns only the distance in miles to the location on a driving route.

Is this possible with app inventor?

There are several ways to get the driving distance.

-This tutorial explains how to get the driving distance using App Inventor’s Navigate control HOW TO: Use the Navigate control This provides reasonable driving distance estimation (the basic data is provided by volunteers and is not always precisely accurate).

You can also use other methods

Is this what you want to do?



  • Get Distance between 2 different addresses.
  • @param start_address Address as string Ex. “300 N LaSalles St, Chicago, IL”
  • @param end_address Address as string Ex. “900 N LaSalles St, Chicago, IL”
  • @param return_type Return type as string Ex. “miles” or “kilometers” or “minutes” or “hours”
  • @customfunction

function GOOGLEMAPS(start_address,end_address,return_type) {

var mapObj = Maps.newDirectionFinder();

var directions = mapObj.getDirections();

var getTheLeg = directions[“routes”][0][“legs”][0];

var meters = getTheLeg[“distance”][“value”];


case “miles”:
return meters * 0.000621371;
case “minutes”:
// get duration in seconds
var duration = getTheLeg[“duration”][“value”];
//convert to minutes and return
return duration / 60;
case “hours”:
// get duration in seconds
var duration = getTheLeg[“duration”][“value”];
//convert to hours and return
return duration / 60 / 60;
case “kilometers”:
return meters / 1000;
return “Error: Wrong Unit Type”;


The code above is what I used to get this in Google Sheets. Then I made an app inventor app to place that data in the sheet and then retrieve the value to be displayed on the app.

My goal is to not need google sheets and do it purely from the app.

Everything you have linked doesn’t show me how to return only a variable. It’s a list or a lot of information I don’t need to have returned. The google API would work if I knew how to use it with App inventor.

:cry: almost everything linked to does show what is necessary to return only a value. So you don’t need all the information returned; parse out what you want instead… :thinking:

The google api returns a json file to your request; learn to use json with app inventor. All you need
is a Web component Google’s development api credentials and the ability to parse json. Here is some help

and an example, easily adapted to App Inventor 2 using the Web Url and GetText Blocks to capture the json response webURL

The following non App Inventor example requests the distance matrix data between Washington, DC and New York City, NY, retyrbs the result in JSON format:,DC&destinations=New+York+City,NY&key=YOUR_API_KEY

Try it! You can test this by entering the URL into your web url block you construct (be sure to replace YOUR_API_KEY with your actual API key). The response includes the distance and duration between the specified origins and destinations.

Create your url string using your two destinations using Text and Text join Blocks.


That is relatively straightforward. If you are lazy use the first method mentioned and do

which is a simplification of the Navigate tutorial. It took 10 minutes. Remember, you have to obtain a free api key described here> and add both a Navigate and LocationSensor control (to geocode)

It results in the same distance as the tutorial (note the tutorial truncates rounds kilometers so it is not the same distance units used in the simple example. With the tutorial example, you get a free map :).

Here are some resources to help you learn to use the AI2 tools. A very good way to learn App Inventor is to read the free Inventor’s Manual here in the AI2 free online eBook … the links are at the bottom of the Web page. The book ‘teaches’ users how to program with AI2 blocks.

There is a free programming course here and the aia files for the projects in the book are here:

How to do a lot of basic things with App Inventor are described here: .

Also look here and here for more tutorials.