Hi folks, as part of the Xamarin Month initiative -by my friend Luis Matos– we are going to learn how to combine Geolocation plugin from Xamarin.Essentials and Maps Control to make a user’s location tracker in Xamarin Forms.
πͺ Let’s do it!
Create a new project in Visual Studio (I’m using 2017 version), choose Cross-Platform -> Mobile App (Xamarin.Forms).
Xamarin.Forms uses the native map APIs on each platform, so first thing we need is install the NuGet “Xamarin.Forms.Maps” for use the Map control.
Then we need to add the initialization code in each project, after the Xamarin.Forms.Forms.Init method call:
//For iOS, AppDelegate.cs file, in the FinishedLaunching method. Xamarin.FormsMaps.Init(); //For Android, MainActivity.cs file, in the OnCreate method. Xamarin.FormsMaps.Init(this, bundle);
The next step is the platform configurations, starting with the Android project, we need to generate an API Key in order to use the Google Maps API V2, follow the instructions in the Xamarin docs then go to Properties/AndroidManifest.xml and paste the key into the following tag attribute:
<application android:label="LocateMe.Android"> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR_KEY" /> </application>
Now, right-click in the Android project name, go to Properties> Android Manifest > Required Permissions and check the following permissions:
-
AccessCoarseLocation
AccessFineLocation
AccessLocationExtraCommands
AccessMockLocation
AccessNetworkState
AccessWifiState
Internet
On iOS we need to set the following keys in the info.plist (right click in the file > Open With > XML Editor):
<key>NSLocationAlwaysUsageDescription</key> <string>Can we use your location at all times?</string> <key>NSLocationWhenInUseUsageDescription</key> <string>Can we use your location when your app is being used?</string> <key>NSLocationAlwaysAndWhenInUseUsageDescription</key> <string>Can we use your location at all times?</string>
- iOS 11
NSLocationWhenInUseUsageDescription
β for using location services when the app is in useNSLocationAlwaysAndWhenInUseUsageDescription
β for using location services at all times
- iOS 10 and earlier
NSLocationWhenInUseUsageDescription
β for using location services when the app is in useNSLocationAlwaysUsageDescription
β for using location services at all times
To support iOS 11 and earlier, you can include all three keys: NSLocationWhenInUseUsageDescription
, NSLocationAlwaysAndWhenInUseUsageDescription
, and NSLocationAlwaysUsageDescription
.
Cool! now we can user the Map control in our common .NET Standard library project.
Go to the common project, open the MainPage.xaml to add the Map control by replacing with the following:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:LocateMe" xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps" x:Class="LocateMe.MainPage"> <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <maps:Map WidthRequest="320" HeightRequest="200" x:Name="MyMap" MapType="Street" /> </StackLayout> </ContentPage>
πͺNow we have the map running!
In the next post (or maybe on this post in a later update) we are going to show and track the user’s location by using the Geolocation plugin from Xamarin.Essentials.
Waiting to see part two. Interested in seeing how will you filter out invalid points.
Hi @Tarek, next week I will be posting the part two (where we are going to learn how to use the Geolocation Plugin from Xamarin Essentials).
Thanks for the feedback!
Hi Angel,
Is the part 2 of this article now available? Part 1 was great:)
Ey! Hi, I havenβt have the time to do it πππ … but I got good news for you, my coworker Rendy just posted a nice article showing the tracking part:
https://www.xamboy.com/2019/05/17/exploring-map-tracking-ui-in-xamarin-forms/
Hi Angel ,
I went though Rendy’s post . The application displays our device tracking . Is there any application developed which displays other devices tracking . As in , the customer can track Driver’s route . Thanks a lot
Hi Jeena, I guess you want to track live locations from others users, you will need an external data source to ask for the geopositions, could be a realtime database service like Realm or App Center Data (which uses Azure Cosmos DB).
Hi Angel , Thanks for your response. To be precise my Use Case is : we are a Courier company. So , the customers need to track the live status of the Riders’ delivering their Shipment