Articles in this section
Category / Section

How to drag and drop a marker in the .NET MAUI Maps (SfMaps)?

4 mins read

In the Syncfusion® .NET MAUI Maps control, you can enable drag-and-drop functionality for a marker in SfMaps through the following steps.

 

Step 1: Add the SfMaps control to your layout, initialize the MapTileLayer to render the tiles returned from the Web Map Tile Services, and set the URL to the UrlTemplate property as shown in the following code sample.

 

XAML:

<map:SfMaps>
    <map:SfMaps.Layer>
        <map:MapTileLayer x:Name="layer"
                          UrlTemplate="https://tile.openstreetmap.org/{z}/{x}/{y}.png">
        </map:MapTileLayer>
    </map:SfMaps.Layer>
</map:SfMaps>

 

Step 2: Add the MapMarker to the MapTileLayer’s MapMarkerCollection as shown in the following code sample.

 

XAML:

<map:MapTileLayer.Markers>
    <map:MapMarkerCollection>
        <map:MapMarker Latitude="28.7041"
                       Longitude="77.1025"
                       IconWidth="15"
                       IconHeight="15"/>
        <map:MapMarker Latitude="13.239945"
                       Longitude="19.391806"
                       IconWidth="15"
                       IconHeight="15"/>
    </map:MapMarkerCollection>
</map:MapTileLayer.Markers>

 

Step 3: In the MapTileLayer.Panning event, set the EnablePanning property of the MapZoomPanBehavior to False. Retrieve the corresponding touch position and pass it to the GetLatLngFromPoint() method to convert the pixel point to a Latitude and Longitude value. Assign the converted value to the selected Marker’s Latitude and Longitude properties to update its position.

 

C#:

private void Layer_Panning(object sender, PanningEventArgs e)
{
    var position = layer.GetLatLngFromPoint(e.Position);
    if (layer.ZoomPanBehavior == null)
    {
        layer.ZoomPanBehavior = new MapZoomPanBehavior()
        {
            EnablePanning = false
        };
    }
 
    foreach (var marker in layer.Markers)
    {
        var halfWidth = marker.IconWidth / 2;
        var halfHeight = marker.IconHeight / 2;
        if (position.Latitude > (marker.Latitude - (halfWidth / layer.ZoomPanBehavior.ZoomLevel)) &&
            position.Longitude > (marker.Longitude - (halfHeight / layer.ZoomPanBehavior.ZoomLevel)) &&
            position.Latitude < (marker.Latitude + (halfWidth / layer.ZoomPanBehavior.ZoomLevel)) &&
            position.Longitude < (marker.Longitude + (halfHeight / layer.ZoomPanBehavior.ZoomLevel)))
        {
            marker.Latitude = position.Latitude;
            marker.Longitude = position.Longitude;
            break;
        }
    }
}

 

Download the complete sample on GitHub.


Output:

 

Marker drag and drop in Syncfusion .NET MAUI Maps

 

 

Conclusion

I hope you enjoyed learning how to drag and drop a marker in the .NET MAUI Maps (SfMaps).

Refer to our .NET MAUI Maps feature tour page to learn about its other groundbreaking feature representations. You can explore our .NET MAUI Maps documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Maps and other .NET MAUI components.

Please let us know in the following comments section if you have any queries or require clarification. Contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!


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