Articles in this section
Category / Section

How to add map markers dynamically to the tapped location

1 min read

Using SfMaps, you can add markers dynamically to the desired location just by tapping at the required locations. You can achieve this by converting the screen points to the corresponding latitude and longitude coordinates and add markers to the maps.

 

The following code snippet demonstrates how to add markers to a layer.

C#

public void AddMarker()
{
    var layer = this.Layers[0];
    Point longitudeLatitude = layer.GetLatLonFromPoint(new Point(PointX, PointY));  // Getting longitude and latitude values from touch points.
    layer.Markers.Add(new CustomMarker()
    {
        Latitude = longitudeLatitude.Y.ToString(),
        Longitude = longitudeLatitude.X.ToString()
    });
}

 

Markers can be specified as demonstrated in the following code snippet.

XAML

    <maps:ImageryLayer.Markers>
                    <local:CustomMarker Label = "California" Latitude = "37" 
                                    Longitude = "-120" x:Name="marker" />
                </maps:ImageryLayer.Markers>
                
                <maps:ImageryLayer.MarkerTemplate>
                    <DataTemplate>
                        <Image HorizontalOptions="Center" 
                               Source="{Binding Image}"
                               VerticalOptions="Center" HeightRequest="15" WidthRequest="15"/>
                    </DataTemplate>
           </maps:ImageryLayer.MarkerTemplate>

 

Output screenshot

Adding Markers at tapped points using Xamarin SfMaps

 

Sample for adding markers at the tapped points: https://github.com/SyncfusionExamples/How-to-add-map-markers-dynamically-at-the-tapped-location

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied