Over a million developers have joined DZone.

Android Location with Google Maps - Part 1

DZone's Guide to

Android Location with Google Maps - Part 1

· Java Zone ·
Free Resource

"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.

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);

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())
// 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 .

From  Tony's Blog.

"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}