this Blog is about android developing and For Android Mobiles.
Friday, June 28, 2013
Android Geofencing with Google Maps
Ageofenceis 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 thenew Location APIs, Google’s location algorithm has been rewritten to be more accurate and use significantly less battery life. There is just enoughdocumentation plus sample code and a downloadable sample app (GeofenceDetection) to help us get started creating geofencing apps. Prerequisites are:
Download (again via SDK Manager) theSupport Libraryto 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:
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 usegeocoding/reverse geocoding to transform a street address to coordinates and vice-versa usingAndroid’sGeocoder.
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 pairinternallyto 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:
Adding /Removing geofences is pretty much covered in the sample app (by theGeofenceRequesterandGeofenceRemoverclasses). The thing to remember is that the process of adding/removing fences is as follows :
A connection to Google’s Location Services is requested by our app.
Once/if the connection is available, the request to add/remove a fence is done using aPendingIntent.
If a similar request made by our app is still underway, the operation fails.
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‘sonAddGeofencesResult() ) with a success status code.
Finally, the preceding method will use a Broadcast Intent to notify other components of our app of success/failure.
Needless to say, we need tocode 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:
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 ownInfoWindowAdapterandOnInfoWindowClickListener.
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.