Android Geofencing with Google Maps

A geofence is a virtual perimeter of interest that can be set up to fire notifications when it is entered or exited, or both. For example, a geofencing app can alert us that our kid has left a previously specified area, or send us a coupon (e.g. the “Present this SMS an get 20% off” offer type) when we happen to walk or drive in the proximity of a movie theater. Now, with the new Location APIs, Google’s location algorithm has been rewritten to be more accurate and use significantly less battery life. There is just enough documentation  plus sample code and a downloadable sample app (GeofenceDetection) to help us get started creating geofencing apps. Prerequisites are:
 
 

  1. Download  Google Play Services (via Android’s SDK Manager) and set it up as a library
  2. Get a Google Maps v2 API key, and maybe run the sample app. This short Quick Start guide might help
  3. Download (again via SDK Manager)  the Support Library to cater to older Android versions.

For practical purposes, let’s just start where the sample geofencing app (GeofenceDetection) stops, and introduce a few enhancements to make the app semi-decent and show a sample of possibilities with the new Location API.

1. Zoom and Camera Position

First, a little taste of Google Maps API v2. Let’s choose zoom level and camera angle:

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
//...
// inside class, for a given lat/lon
CameraPosition INIT =
new CameraPosition.Builder()
.target(new LatLng(lat, lon))
.zoom( 17.5F )
.bearing( 300F) // orientation
.tilt( 50F) // viewing angle
.build();
 // use GooggleMap mMap to move camera into position
 mMap.animateCamera( CameraUpdateFactory.newCameraPosition(INIT) );

The code above has a zoom level allowing the viewing of buildings in 3D. Google Maps v2 uses OpenGL for Embedded Systems (OpenGL ES v2) to render 2D and 3D computer graphics.

2. Options menu

Even if we are not big fans of an Options menu, it might be adequate in this case, since we would not want to clutter the map with too much “touch” functionality (we will  have plenty of that shortly).

We can toggle between “normal” and satellite view:

/**
 * Toggle View Satellite-Normal
 */
 public static void toggleView(){
 mMap.setMapType( mMap.getMapType() ==
              GoogleMap.MAP_TYPE_NORMAL ?
              GoogleMap.MAP_TYPE_SATELLITE :
              GoogleMap.MAP_TYPE_NORMAL);
 }

We can also provide a “Flight Mode”, where we let the camera scroll away. Not tremendously useful, but kind of cool nonetheless:

import com.google.android.gms.maps.GoogleMap.CancelableCallback;
//...
private static CancelableCallback callback = new CancelableCallback() {
@Override
 public void onFinish() {
   scroll();
 }
 @Override
 public void onCancel() {}
};

public static void scroll() {
   // we don't want to scroll too fast since
  // loading new areas in map takes time
   mMap.animateCamera( CameraUpdateFactory.scrollBy(10, -10),
                       callback ); // 10 pix
}

3. Geocoding/Reverse Geocoding

The sample is here to demonstrate features and makes heavy use of latitude/longitude coordinates. But we need to provide a more user-friendly way to interface with locations on the map, like a street address. We can use geocoding/reverse geocoding  to transform a  street address to coordinates and vice-versa using Android’s Geocoder.

4. Adding geofences

OK, now on to geofences. Thanks to geocoding, we can request an actual physical address from the user instead of coordinates. We will  just change that address to a latitude/longitude pair internally to process user input. Notice how we use transparent UIs as much as possible to enhance what some might call the user experience. Notice also that we provide a spinner so that the user can choose between predefined values. That saves the user some typing and it saves us from validating coordinates values each time.

Still, if we want to be even more user-friendly, we can give our users the possibility to pre-fill the address field by long-pressing a point on the map. We will then use reverse geocoding to translate the coordinates to a physical address for display (screen below on the right):

Processing long-presses is pretty straightforward:

import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
//...
public class MainActivity extends FragmentActivity
                          implements OnMapLongClickListener  {
  //...
  mMap.setOnMapLongClickListener(this);
  //...
  @Override
  public void onMapLongClick(LatLng point) {
     // reverse geocode point
  }
}

Adding /Removing geofences is pretty much covered in the sample app (by the GeofenceRequester and GeofenceRemover classes). The thing to remember is that the process of adding/removing fences is as follows :

  1. A connection to Google’s Location Services is requested by our app.
  2. Once/if the connection is available, the request to add/remove a fence is done using  a PendingIntent.
  3. If a similar request made by our app is still underway, the operation fails.
  4. Although the method we call (e.g. addGeofences()) returns at once, we won’t know if the request was successful until Location Services calls back into our app (e.g. OnAddGeofencesResultListener‘s onAddGeofencesResult() ) with a success status code.
  5. Finally, the preceding method will use a Broadcast Intent to notify other components of our app of  success/failure.

Needless to say, we need to code defensively at almost every step of the way. Now, once a geofence is added, we can add a marker (the default or a customized one) and choose between different shapes (circle, polygon, etc.) to delimit the geofence. For instance we can write this code to add the default marker and circle the fence within a specified radius:

import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.MarkerOptions;
//...
public static void addMarkerForFence(SimpleGeofence fence){
if(fence == null){
    // display en error message and return
   return;
}
mMap.addMarker( new MarkerOptions()
  .position( new LatLng(fence.getLatitude(), fence.getLongitude()) )
  .title("Fence " + fence.getId())
  .snippet("Radius: " + fence.getRadius()) ).showInfoWindow();

//Instantiates a new CircleOptions object +  center/radius
CircleOptions circleOptions = new CircleOptions()
  .center( new LatLng(fence.getLatitude(), fence.getLongitude()) )
  .radius( fence.getRadius() )
  .fillColor(0x40ff0000)
  .strokeColor(Color.TRANSPARENT)
  .strokeWidth(2);

// Get back the mutable Circle
Circle circle = mMap.addCircle(circleOptions);
// more operations on the circle...

}

Here are the resulting screens, including the one we get once we “Touch to Edit” the Info Window:

The sample app has all we need to fire notifications once the circled area above is entered or exited. Notice how we set up the marker’s Info window to allow editing the geofence radius, or removing the geofence altogether. To implement a clickable custom Info window, all we need is to create our own InfoWindowAdapter and OnInfoWindowClickListener.

As for the notifications themselves, this is how they look like in the sample app:

We can of course change a notification’s appearance and functionality, and… that would be the subject of another article.  Hopefully, this one gave a glimpse of what is possible with the new Location API.  Have fun with Android geofences.
 

Reference: Android Geofencing with Google Maps from our JCG partner Tony Sicilian at the Tony’s Blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Android Geofencing with Google Maps"

  1. Zoram says:

    It would be great if you could give this tutorial as a sample app :D

  2. Kaushik Sureja says:

    Hello..

    Great Work ,

    Thanks for tutorial i want sample example of this code if possible then give us code zip file…

Leave a Reply


one × = 5



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close