Android Location with Google Maps - Part 1
Join the DZone community and get the full member experience.
Join For Free1. 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:
- the ability to access the phone's location services
- a visual display of the phone's location on a map
- the capacity to react to changes in location and update the map accordingly
- 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:
- download it via the sdk manager
- obtain a maps api key
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:
- the ui, i.e. an android activity class extending google maps mapactivity : locatoractivity
- a processor class that will take care of all the location-based logic, and in particular react to location updates: locator
- 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 .
Opinions expressed by DZone contributors are their own.
Comments