Display maps with "Start activity" and other local mapping App

Durée estimée: 45 minutes

Presentation :

This App will "throw an intent" to draw a map. The Android system will catch ths intent and check if other Apps on the phone can do the job. If yes one will take control and do it. In our cas, this will be the Google maps App (if it is installed on the phone).

while this new "activity" is running, your own App will be hiddent and be back when this new activity has done the job and died.

What we will do :

What we will learn :

  • Get a small peek on the Android activity life cycle, and how different Apps can run each other and take foreground control,

Input Resources :

None : Start with a new project (no canvas necessary).

Development :

Intent APIs and interface to other Apps on your smartphone

With this solution, your App delegates mapping to another App that must be installed on your phone (here the Google maps App). The general idea of an “Intent” request is that an android App throws an intent (i.e. something it wishes to do) to the Android system and waits for some other App to answer ”I can do it”. If there is only one App, it will start, otherwise it will let you choose between App candidates do the job. That’s funny, as if Android Apps can behave like students in class when one calls for help, and others who know how propose to help !

In our case, we will ask for a "VIEWing" action (android.intent.action.VIEW). This with the data URI may be enough for Android to find the good App (as it is the case on my Nexus) but besides action and data URI it is better to say more precisely what service the App wants, or be more explicit.

With Activity Starter, you can indicate 3 infos :

  • Action (android.intent.action.VIEW),
  • Class (android.maps.MapsActivity)
  • and Package (com.google.android.apps.maps).
Activity action, class and package will help Android identify the Apps that can answer the request. Then your requesting App will pass the “Data URI” to the answering App to tell it what to do.
The Data URI must match the API of the answering App. In our case with Google maps, the API and data URIs are described here.
They may be like :
geo:0,0?q=latitude,longitude(label)
We will see details of this API when we use it below, and if you wish to know more you will find :
  • a nice video tutorial by Ralph Morelli within the mobile CSP course (see lesson 10 in unit 3) with this video video to show the App and this video to show how to build it,
  • a nice App on the playstore that may help you practice and find solutions : with App Inventor is the App Inventor ActivityStarter App. It may help you fill in parameters for the Activity starter component (even if you do not completely understand what you are really doing).

Code "Activity starter"display project with Google maps

Foreword :
If you have Apps like Google maps installed on you smartphone, you can start it from your own app with an "ActivityStarter" component (see in the Connectivity drawer).
There are 2 main differences with previous solution :

  1. your own App disappears while the new activity or App is running. This means that your App has completely lost control and can no longer interact until you close map display,
  2. but you have more display options and there is a good documentation for the API here.

Coding :

  1. Create a new project, image003.png
  2. on the design side :
    • Add a display button and set its text to "displayMap".
    • Add a StartActivity component with the following setup :
      Action : android.intent.action.VIEW
      Activity Class : android.maps.MapsActivity
      Activity Package : com.google.android.apps.maps
    This will enable Android to find the application that matches these criteria (e.g. Google maps that has to be on your phone).

  3. On the blocks side :
    • Initialize latitude, longitude and zoom global variables with default values, and add a query variable.
    • Then build the data URI, according to the first case shown in the documentation.
      geo:latitude,longitude?z=zoom

    Test App : Clicking on the button will display the map below on the right.

Go further : Other display options

The google maps Android-Intent API offers many other options (see documentation) :

1. geo:latitude,longitude?z=zoom basic display
2. geo:latitude,longitude?q=query to add search for restaurant, hospital
3. geo:0,0?q=my+street+address to find by address
4. geo:0,0?q=latitude,longitude(label) to add a label
5. google.navigation:q=a+street+address to find directions with different travel modes
6. google.navigation:q=latitude,longitude&mode=d mode = d or w or b for travel option :
drive, walk ,bike
7. google.streetview:cbll=latitude,longitude&cbp=0,bearing,0,zoom,tiltand streetview
8. google.streetview:panoid=id&cbp=0,bearing,0,zoom,tilt

Cases 1 to 7 are illustrated below them with their dataURIs.
image027.png image029.png image031.png image033.png
image025.png image035.png image037.png image039.png

image041.png image043.png image045.png
image047.png image049.png image051.png

The code - below -uses a list of 7 dataURIs. Display switches from one to the next each time the user clicks on the display button.

image053.png