We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add map markers dynamically to the tapped location

Platform: Xamarin.Forms |
Control: SfMaps |
Published Date: June 26, 2019 |
Last Revised Date: June 26, 2019

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

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Aman Kharzoum
Nov 21, 2019

Hi I used this sample, but using Touch Events prevent me from moving around Map without Adding any Markers in both Android and iOS like using Touch events prevent any other Events on Map! Mmm, How could I solve this?

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon