Android and iOS with Xamarin Forms
In the previous two posts, I covered how you can prepare your Android and iOS projects to display maps when using Xamarin Forms, this post is going to build on that previous project that you can find in this GitHub repository, and add the necessary permissions and code needed to display the user's location in the map.
The iOS setup
Let's jump right in, in the project that we built in the two previous posts (or that you just forked from my GitHub repo) that displays a map, we will have to set some permissions so our application can access the user's location. Most of this is handled by the operating system when it identifies that the application wants access to the location, it displays a message to the user, asking for that permission, if it is granted, the app can receive the location, if it is not, the app can't.
But while all of that is handled by the OS, there is one thing that the OS requires from us: the message that it will display to the user when requesting permission.
To set this message, you will have to open the Info.plist file and add the "Location When In Use Usage Description", and/or the "Location Always Usage Description". Both I believe are self-explanatory, but remember which ones you add, since that is the kind of location (when in use or always) that you will have to request from the code.
BTW, in the case of Visual Studio 2017 (Windows), you will need to right-click the Info.plist file, select "open with" and select "Generic PList Editor" to see a UI similar to this one, in Visual Studio for Mac, double-clicking on the file will be enough. In any case, you will be able to edit the XML source code, in which case you will have to write a key and a string inside the <dict> tags to end up with something like this:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key> <string>Please let me access your location so we can kidnap you.</string>
The Android setup
For Android we will need something similar, we need to let the OS know that this app will need to access the user's location, so we will set the permissions for the Android Application (Android Manifest on Windows) tab that exists in the Android options window (right-click the Android project and select options on Mac or Properties on Windows).
The permissions that we need to add are:
FYI, you could also set this in a similar way to what we did on iOS, with an XML code. Simply navigate to the Properties folder and open the AndroidManifest.xml file. You will likely see a similar view that lists the required permissions, but if you navigate to the source tab, you will be able to edit the XML file directly, simply add the following inside the <manifest> tag:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Displaying the user location
This is surprisingly easy to achieve, the Map element already contains a property that will display the user's location on the map, simply set the IsShowingUser to true, and you are done:
<maps:Map VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" IsShowingUser="true" MapType="Street"/>
In case you are testing on an Android emulator, you will need to click on its extended controls button (the ellipsis at the bottom of the right gray bar), navigate to the Location tab, optionally change the coordinates and click on send before you are able to see any location in the map.
In the case of the iOS simulator, you can do something similar by navigating to the Debug menu and selecting one of the options under location:
This topic, along with many many others, is covered in greater depth in my 25-hour long "The Complete Xamarin Developer Course: iOS and Android" course, which you can practically steal from me by