Android Location with Google Maps – Part 1

UPDATE: This series is based on the old Google Maps API (v1) and is no longer of any major relevance. For Google Maps v2, check out this article.

This series looks at how to design and implement a location-based Android application using the location API and Google Maps. Part One focuses on gathering the requirements, analyzing the tools at our disposal, and designing the application.

1.  Requirements: What do we want?

Let’s say we want to create an application that displays the phone’s location on a map, plus other cool stuff on top of that, like the possibility to send the location via SMS or email to contacts simply by tapping the screen (for example). In order to do that, we need several things:

  1. The ability to access the phone’s location services
  2. A visual  display of the phone’s location on a map
  3. The capacity to react to changes in location and update the map accordingly
  4. The possibility to overlay items on top of the map, like icons, dialogues or other clickable items

2.   Tools and APIs: What do we have?

Android provides access to the phone’s location services with the LocationManager in the android.location package . The API provides some level of useful information such as:

// Get a reference to the system Location Manager via the app context
 LocationManager locManager = 
      (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

// 1. LIST OF PROVIDERS (Network, GPS, etc..)
 List providers =  locManager.getAllProviders();

// 2. BEST PROVIDER using some Criteria. here, the default
String bestProvider = locManager.getBestProvider(new Criteria(), false);

// 3. LAST KNOWN LOCATION
Location lastKnown = locManager.getLastKnownLocation(bestProvider);
String display = new StringBuilder()
      .append("latitude: ").append(lastKnown.getLatitude())
      .append("\nLongitude: ").append(lastKnown.getLongitude())
      .append("\nAltitude: ") .append(lastKnown.getAltitude())
      .append("\nAccuracy: ").append(lastKnown.getAccuracy())
      .append("\nBearing: ").append(lastKnown.getBearing())
      .append("\nSpeed: ").append(lastKnown.getSpeed())
      .toString();
// show display on screen
//...

And that’s nice…if you like numbers. But to actually show the location on a map, we need an external  library like Google Maps. And to use that, we need to do a couple of things:

Once we do the above (and that’s relatively painless), we can start creating a simple application that displays our location on a map. So that takes care of requirements 1 and 2 above.

The third requirement, i.e. the capacity to update the phone’s location is provided by implementing one or more of the android.location.LocationListener‘s callback methods:

public void onLocationChanged(Location location) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}

Finally, our fourth requirement will be met by extending Google Maps’ Overlay, OverlayItem or ItemizedOverlay. The latter provides us with a list of items we might want to overlay on top of our map. In particular, we will be interested in the following method :

protected boolean onTap(int index){}

…since we would want the user to get some added functionality by tapping his/her location on the map.
That’s it. Now that we have all we need to start designing and coding our application.

3.  Application Design

At first glance, we’ll need three classes with each their own responsibilities:

  1. The UI, i.e. an Android Activity class extending Google Maps MapActivity:  LocatorActivity
  2. A processor class that will take care of all the location-based logic, and in particular react to location updates:  Locator
  3. An Overlay class which will focus on the items to display on top of the map:  LocatorOverlay

We might need more than the above as we dive more deeply into our implementation, but remember, we are agile, so we can always go back & modify our original design.

This is our (simplified) UML Class Diagram:

.

We’ll start tackling the implementation in part 2, starting with the lowest-level class, i.e. LocatorOverlay .

This article is also available at JavaLobby.

, ,

  1. #1 by Jeune Fille on August 15, 2014 - 4:03 am

    I followed this tutorial, it is successful. But when I start the apps, the maps takes long time to find the current location. May I know how to fix it. Thank you for your nice tutorial

  1. CS711 Assignment No 2 Solution Guideline Spring 2018 – theiteducation

Leave a comment