Geolocation takes advantage of device-specific location services to help provide a location for a particular user at any given point in time. There are many common use cases for using geolocation in mobile apps, such as displaying weather data for a user’s location, navigating a user to a destination, or helping find a restaurant nearby. Even if you aren’t building a weather or maps app, there are other useful applications of location services, including location-specific features (like Snapchat’s geofilters featured right) or pre-filling data entry forms (i.e. city, state, zip code) to help improve conversion rates.
Accessing geolocation services from a portable class library or shared project has traditionally been very hard. Each mobile operating system (iOS, Android, and Windows) implements location services differently, so conditional compilation or dependency injection would typically be required. Additionally, each of the providers is implemented very differently and with varying degrees of complexity, which can make it difficult to add even basic functionality to your mobile apps.
Plugins for Xamarin are libraries that expose a single set of APIs for accessing common device functionality across iOS, Android, and Windows. This increases the amount of code that can be shared across multiple platform, making mobile development fast and easy. There are many different plugins, including a Geolocator Plugin for Xamarin and Windows. In this post, I’m going to share with you one of my favorite plugins to help make adding geolocation to your apps a breeze.
Platform Setup
Add the Geolocator Plugin for Xamarin and Windows NuGet to your cross-platform portable class library and each of the platform-specific projects. Geolocation taps into the native location services for both iOS and Android. Because a user’s location is private, we must configure our applications to ask the user if it’s okay if we access their current location.
Android
To allow our application to access location services, we need to enable two Android permissions: ACCESS_COARSE_LOCATION
and ACCESS_FINE_LOCATION
. If you have added the NuGet to the Android project directly, this should be enabled for you by default. Double-check to make sure the following permissions are enabled:
If you are targeting Android Marshmallow (Android M) or above, users will also be prompted automatically for runtime permissions.
iOS
Depending on if you will be always using geolocation (such as a maps app), or just at certain points in a user’s workflow, you will either need to add the key NSLocationWhenInUsageDescription
or NSLocationAlwaysUsageDescription
in your Info.plist, along with a new string entry for the key that describes exactly what you’ll be doing with the user’s location. When the permission is prompted to the user at runtime, the description listed here will display.
Additionally, if you wish to support background updates (iOS9+ only), you must enable the AllowsBackgroundUpdates
property of the Geolocator. The presence of the UIBackgroundModes
key with the location value is also required for background updates.
Windows
The ID_CAP_LOCATION
permission must be enabled.
Detecting a User’s Location
The Geolocator Plugin for Xamarin and Windows boils all of the complexity of location services down to a single asychronous method:
using Plugin.Geolocator; var locator = CrossGeolocator.Current; var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10)); Debug.WriteLine("Position Status: {0}", position.Timestamp); Debug.WriteLine("Position Latitude: {0}", position.Latitude); Debug.WriteLine("Position Longitude: {0}", position.Longitude);
If you are continuously monitoring a user’s location, such as for a maps application, you can also tap into the PositionChanged
event to receive updated coordinates:
locator.PositionChanged += (sender, e) => { var position = e.Position; latitudeLabel.Text = position.Latitude; longitudeLabel.Text = position.Longitude; };
Conclusion
The Geolocator Plugin for Xamarin and Windows is a super easy way to integrate cross-platform location services into your apps. To get started, download the geolocator sample featured above or check out the plugin source on GitHub. Be sure to check out a full listing of Plugins for Xamarin, including some of my favorite plugins, such as settings, connectivity, and sharing.
0 comments