Here's a guide on how to integrate map and geolocation features into an Android business app. This guide specfically tells you on how to includes overlaying store locations on Google Maps*, using geofence to notify the user when the device enters the store proximities.
In this case study, we will incorporate maps and geolocation functionality into a restaurant business app for Android tablets (Figure 1). The user can access the geolocation functionality from the main menu item “Locations and Geofences” (Figure 2).
For a business app, showing the store locations on maps is very graphical and helpful to the user (Figure 3). The Google Maps Android API provides an easy way to incorporate Google Maps into your Android apps.
Google Maps Android API v2 is part of the Google Play Services APK. To create an Android app which uses the Google Maps Android API v2 requires setting up the development environment by downloading and configuring the Google Play services SDK, obtaining an API key, and adding the required settings in your app’s AndroidManifest.xml file.
First you need to set up Google Play Services SDK by following https://developer.android.com/google/play-services/setup.html.
Then you register your project and obtain an API key from Google Developers Console https://console.developers.google.com/project. You will need to add the API key in your AndroidManifest.xml file.
To use Google Maps Android API v2, some permissions and features are required to be specified as children of the <manifest> element (Code Example 1). They include the necessary permissions for network connection, external storage, and access to the location. Also OpenGL ES version 2 feature is needed for the Google Maps Android API.
Code Example 1. Recommended permissions to specify for an app which uses Google Maps Android API. Include the “ACCESS_MOCK_LOCATION” permission only if you test your app with mock locations **
Also as children of the <application> element, we specify the Google Play Services version and the API key we obtained in <meta-data> elements (Code Example 2).
Code Example 2. Specify Google Play Services Version and API Key **
Adding a Map Fragment
First in your activity layout xml file, add a MapFragment element (Code Example 3).
Code Example 3. Add a MapFragment in the Activity Layout **
In your activity class, you can retrieve the Google Maps MapFragment object and draw the store icons at each store location.
Code Example 4. Draw the Store Icons on Google Maps **
A geofence is a circular area defined by the latitude and longitude coordinates of a point and a radius. An Android app can register geofences with the Android Location Services. The Android app can also specify an expiration duration for a geofence. Whenever a geofence transition happens, for example, when the Android device enters or exits a registered geofence, the Android Location Services will inform the Android app.
In our Restaurant app, we can define a geofence for each store location. When the device enters the proximity of the store, the app sends a notification for example, “You have entered the proximity of your favorite restaurant!” (Figure 4).
Registering and Unregistering Geofences
In Android SDK, Location Services is also part of Google Play services APK under the “Extras” directory.
To request geofence monitoring, first we need to specify the "ACCESS_FINE_LOCATION" permission in the app’s manifest, which we have already done in the previous sections.
We also need to check the availability of the Google Play Services (the
checkGooglePlayServices() method in Code Example 5). After the
locationClient().connect() call and the location client has successfully established a connection, the Location Services will call the
onConnected(Bundle bundle) function, where the location client can request adding or removing geofences.
Code Example 5. Request Geofence Monitoring by Location Services **
The location service requests are usually non-blocking or asynchronous calls. Actually in Code Example 5 in the previous section, we have already implemented one of these functions: after the
locationClient().connect()call and the location client establishes a connection, the Location Services will call the
onConnected(Bundle bundle) function. Code Example 6 lists other location callback functions we need to implement.
Code Example 6. IImplement the Location Service Callbacks **
Finally, we need to implement the IntentService class to handle the geofence transitions (Code Example 7).
Code Example 7. Implement an IntentService Class to Handle Geofence Transitions
In this article, we have discussed how to incorporate mapping and geofencing features into an Android business app. These features support rich geolocation contents and strong location based services and use cases in your apps.
For more such Android resources and tools from Intel, please visit the Intel® Developer Zone