How to show the current GPS location in Xamarin.Forms maps?
Solution
To show the current GPS location, you can get the current location of device by using the Geolocation.GetLastKnownLocationAsync() method of Xamarin.Essentials and add the marker pin on maps using the current location values.
The following steps demonstrate how to add the marker on current geolocation:
Step 1: Initialize the SfMaps control and add Imagery layer with OSM type to the layer collection of the control.
<maps:SfMaps x:Name="sfmap"> <maps:SfMaps.Layers> <maps:ImageryLayer LayerType="OSM" x:Name="layer"> </maps:ImageryLayer> </maps:SfMaps.Layers> </maps:SfMaps>
Step 2: Create a custom marker by extending the MapMarker with Image property to show pin image as marker and define the MarkerTemplate to show pin marker on Xamairn.Forms Map.
… <maps:ImageryLayer.MarkerTemplate> <DataTemplate> <Image x:Name="markerImage" HorizontalOptions="Center" Source="{Binding Image}" VerticalOptions="Center" HeightRequest="15" WidthRequest="15" /> </DataTemplate> </maps:ImageryLayer.MarkerTemplate> …
… public class CustomMarker : MapMarker { public ImageSource Image { get; set; } public CustomMarker() { Image = ImageSource.FromResource("MarkerOnGPSLocation.pin.png"); } } …
Step 3: Get the current location from Xamarin.Essentials’s Geolocation.GetLaskKnownLocationAsync() API and add the marker to Markers collection of Imagery layer with obtained latitude and longitude values.
… public MainPage() { InitializeComponent(); this.AddMarkerInCurrentLocation(); } private async void AddMarkerInCurrentLocation() { try { var location = await Geolocation.GetLastKnownLocationAsync(); if (location != null) { CustomMarker marker = new CustomMarker(); marker.Latitude = location.Latitude.ToString(); marker.Longitude = location.Longitude.ToString(); this.layer.Markers = new ObservableCollection<MapMarker> { marker }; } } catch (Exception ex) { // Handle not supported on device exception await Application.Current.MainPage.DisplayAlert("Alert", ex.Message, "Ok"); } } …
Step 4: To display the map with specific distance around current location by specifying the Radius with DistanceType properties of imagery layer.
… this.layer.GeoCoordinates = new Point(location.Latitude, location.Longitude); this.layer.Radius = 5; this.layer.DistanceType = DistanceType.KiloMeter; …
Output
You can find the complete sample in this link.
Conclusion
I hope you enjoyed learning about how to set customized currency symbol in Xamarin.Forms Numeric control.
You can refer to our Xamarin Maps feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!