My Location
To enable My Location, you can set the MyLocationEnabled
property to true
. Additionally, the IsMyLocationButtonVisible
property controls the visibility of the My Location Button, which focuses the map on the current position.
In order to show My Location, the location permissions have to be granted. To enable location access, depending on the system, you have to perform the following steps:
iOS Setup
Based on your use case, you need to configure the info.plist
file as follows:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location to continue.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location to continue.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location to continue.</string>
Android Setup
Ask for the AccessFineLocation
or AccessCoarseLocation
permissions. The following code is an example of how to do this in your MainActivity.cs
:
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Forms.Forms.Init(this, savedInstanceState);
// Initialize RedCorners.Forms.GoogleMaps
RedCorners.Forms.GoogleMapsSystem.Init(this, savedInstanceState);
LoadApplication(new App());
// Ask for Location Permissions
if (
(ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) != Permission.Granted) ||
(ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessCoarseLocation) != Permission.Granted))
{
ActivityCompat.RequestPermissions(this, new [] {
Manifest.Permission.AccessFineLocation,
Manifest.Permission.AccessCoarseLocation}, 1);
};
}
Configure your AndroidManifest.xml
to use the required permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Read Current Location
To access the coordinates of the current location, you can use the MapLocationSystem
singleton. Once the location is shown on the map, its Latitude
and Longitude
properties contain the most up-to-date location values for the device.
Additionally, you can subscribe to the OnMapLocationChanged
event to get notified of the location changes.
var locationSystem = RedCorners.Forms.MapLocationSystem.Instance;
Console.WriteLine($"{locationSystem.Latitude}, {locationSystem.Longitude}");