Over a million developers have joined DZone.

Android Location with Google Maps - Part 4

DZone's Guide to

Android Location with Google Maps - Part 4

· Java Zone ·
Free Resource

Delivering modern software? Atomist automates your software delivery experience.

In Part 1, we designed a simplified UML class diagram to guide us through our development process. Then we implemented LocatorOverlay and Locator in Part 2 and Part 3 respectively.



 In the last part of this series, we'll tackle the UI, LocatorActivity, and start by including the MapView snippet in our XML layout with the Google Maps API key we mentioned in Part 1:

android:apiKey="insert-your-map-key-here" />
Now, we need an Activity that displays a MapView. Google Maps provides a MapActivity with lifecycle methods that take care of the map service threads and rendering for us:
// package statement and other imports here ...
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

 * A Google map activity with zoom controls
 * displaying the handset's location point
 * with the default provider (i.e. network) accuracy.
public class LocatorActivity extends MapActivity {

	private Locator locator;	

	/** A View from the Google Maps service.*/
	private MapView mapView;

// Lifecycle methods ...............

 /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        MapView mapView = (MapView) findViewById(R.id.mapview);
       //  enable the built-in zoom controls

       // start location services
       locator = new Locator(this, mapView, null);

     /** Register for the updates when Activity is in foreground */
     protected void onResume() {

     /** Stop the updates when Activity is paused 
      *  since tracking location is costly on the device's battery and CPU.  
      * */
      protected void onPause() {

// MapActivity accounting info ...............

     *   Whether or not we are displaying any kind of route information,
     *   such as a set of driving directions.
     * */
     protected boolean isRouteDisplayed() {
         return false;

    protected boolean isLocationDisplayed() {
    	return true;


In the last section above, there are two methods in MapActivity that Google Maps uses for accounting purposes, and needs us to implement in order to be compliant with its terms of use:

  1. isLocationDisplayed()
  2. isRouteDisplayed()
So we need to tell Google Maps if we're implementing any of those.
Last but not least, we need to declare our use of the maps library and ask for the appropriate permissions in our Android Manifest file:
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
  <uses-library android:name="com.google.android.maps" />


And...we're done. It's time to see the results of our efforts. Here's a display at the level of details of zoom 16 set as default previously in our Locator class, from where we can zoom in & out by tapping the screen, since we enabled the built-in zoom controls earlier in code:



And our main feature, i.e the possibility to send our location via SMS to our contacts, by tapping our location point on the map:



Once we choose to send out location, our SMS program (or a list if we have several ones installed) will pop up with our location text, here a a location URL. The second screenshot is the visualization of our location by the SMS recipients once they open the link in their own browser:


We have now implemented a basic Android application using Google Maps, that communicates our location via SMS.


From Tony's Blog.

Start automating your delivery right there on your own laptop, today! Get the open source Atomist Software Delivery Machine.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}