Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Android Location with Google Maps - Part 4

DZone's Guide to

Android Location with Google Maps - Part 4

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

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:

<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
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. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

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

     /** Register for the updates when Activity is in foreground */
     @Override
     protected void onResume() {
         super.onResume();	
         locator.requestUpdates();
     }

     /** Stop the updates when Activity is paused 
      *  since tracking location is costly on the device's battery and CPU.  
      * */
      @Override
      protected void onPause() {
         super.onPause();
         locator.removeUpdates();
     }

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

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

    @Override
    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" />
...
<application...>
  ...
  <uses-library android:name="com.google.android.maps" />
</application>

 

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.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}