Making POIApp Location Aware

Exclusive offer: get 50% off this eBook here
Xamarin Mobile Application Development for Android

Xamarin Mobile Application Development for Android — Save 50%

Learn to develop full featured Android apps using your existing C# skills with Xamarin.Android with this book and ebook

$20.99    $10.50
by Mark Reynolds | January 2014 | Open Source

The article by Mark Reynolds, the author of Xamarin Mobile Application Development for Android, deals with one of the most interesting aspects for mobile development is interacting with device capabilities such as motion sensors, cameras, and location sensors. While these capabilities are new and fun to many developers, they can also bring a great deal of value to the users of our mobile apps.

(For more resources related to this topic, see here.)

Location services

While working with location services on the Android platform, you will primarily work with an instance of LocationManager. The process is fairly straightforward as follows:

  1. Obtain a reference to an instance of LocationManager.
  2. Use the instance of LocationManager to request location change notifications, either ongoing or a single notification.
  3. Process OnLocationChange() callbacks.

Android devices generally provide two different means for determining a location: GPS and Network. When requesting location change notifications, you must specify the provider you wish to receive updates from. The Android platform defines a set of string constants for the following providers:

Provider Name

Description

GPS_PROVIDER (gps)

This provider determines a location using satellites. Depending on conditions, this provider may take a while to return a location fix. This requires the ACCESS_FINE_LOCATION permission.

NETWORK_PROVIDER (network)

This provider determines a location based on the availability of a cell tower and Wi-Fi access points. Its results are retrieved by means of a network lookup.

PASSIVE_PROVIDER (passive)

This provider can be used to passively receive location updates when other applications or services request them without actually having to request for the locations yourself. It requires the ACCESS_FINE_ LOCATION permission, although if the GPS is not enabled, this provider might only return coarse fixes.

You will notice specific permissions in the provider descriptions that must be set on an app to be used.

Setting app permissions

App permissions are specified in the AndroidManifest.xml file. To set the appropriate permissions, perform the following steps:

  1. Double-click on Properties/AndroidManifest.xml in the Solution pad. The file will be opened in the manifest editor. There are two tabs at the bottom of the screen, Application and Source, which can be used to toggle between viewing a form for editing the file or the raw XML as follows:

  2. In the Required permissions list, check AccessCoarseLocation, AccessFineLocation, and Internet. Select File | Save.

  3. Switch to the Source View to view the XML as follows:

Configuring the emulator

To use an emulator for development, this article will require the emulator to be configured with Google APIs so that the address lookup and navigation to map app works.

To install and configure Google APIs, perform the following steps:

  1. From the main menu, select Tools | Open Android SDK Manager.
  2. Select the platform version you are using, check Google APIs, and click on Install 1 package…, as seen in the following screenshot:

  3. After the installation is complete, close the Android SDK Manager and from the main menu, select Tools | Open Android Emulator Manager.
  4. Select the emulator you want to configure and click on Edit.
  5. For Target, select the Google APIs entry for the API level you want to work with.

  6. Click on OK to save.

Obtaining an instance of LocationManager

The LocationManager class is a system service that provides access to the location and bearing of a device, if the device supports these services. You do not explicitly create an instance of LocationManager; instead, you request an instance from a Context object using the GetSystemService() method. In most cases, the Context object is a subtype of Activity. The following code depicts declaring a reference of a LocationManager class and requesting an instance:

LocationManager _locMgr; . . . _locMgr = GetSystemService (Context.LocationService) as LocationManager;

Requesting location change notifications

The LocationManager class provides a series of overloaded methods that can be used to request location update notifications. If you simply need a single update, you can call RequestSingleUpdate(); to receive ongoing updates, call RequestLocationUpdate().

Prior to requesting location updates, you must identify the location provider that should be used. In our case, we simply want to use the most accurate provider available at the time. This can be accomplished by specifying the criteria for the desired provider using an instance of Android.Location.Criteria. The following code example shows how to specify the minimum criteria:

Criteria criteria = new Criteria(); criteria.Accuracy = Accuracy.NoRequirement; criteria.PowerRequirement = Power.NoRequirement;

Now that we have the criteria, we are ready to request updates as follows:

_locMgr.RequestSingleUpdate (criteria, this, null);

Summary

In this article, we stepped through integrating POIApp with location services and the Google map app. We dealt with the various options that developers have to make their apps location aware and walks the reader through adding logic to determine a device's location and the address of a location, and displaying a location within the map app.

Resources for Article:


Further resources on this subject:


Xamarin Mobile Application Development for Android Learn to develop full featured Android apps using your existing C# skills with Xamarin.Android with this book and ebook
Published: January 2014
eBook Price: $20.99
Book Price: $34.99
See more
Select your format and quantity:

About the Author :


Mark Reynolds

Mark Reynolds is a software enthusiast who has worked in the industry for nearly 30 years. He began his career with Electronic Data Systems, building and supporting systems for the manufacturing sector. Over the years, he has worked with companies ranging in size from startups to Fortune 500 across a diverse set of industries including manufacturing, entertainment, financial services, government, and telecom. In 1993, Mark started a consulting practice focused on delivering training and mentoring services in the areas of software architecture, design, and implementation. With the rise of mobile computing, Mark has returned to what he loves the most, designing, developing, and delivering software solutions, now focusing in the mobile computing space. He continues his private consulting practice based in Allen, TX, where he also resides with his wife and son.

Mark works as an independent consultant through his own private consulting practice (RSEG) based in Allen, TX, a community located north of Dallas. You can find out more about the services he offers from his website, rseg.net.

Books From Packt


Augmented Reality for Android Application Development
Augmented Reality for Android Application Development

Android Application Programming with OpenCV
Android Application Programming with OpenCV

Android Application Security Essentials
Android Application Security Essentials

Android Development Tools for Eclipse
Android Development Tools for Eclipse

Android Native Development Kit Cookbook
Android Native Development Kit Cookbook

Android Security Cookbook
Android Security Cookbook

Android NDK Game Development Cookbook
Android NDK Game Development Cookbook

Android 4: New Features for Application Development
Android 4: New Features for Application Development


Your rating: None Average: 1.8 (4 votes)

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
B
y
G
4
u
7
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software