Skip to content Skip to sidebar Skip to footer

In Xamarin I Need To Set Marker In The Center Of The Map And Get The Position

I'm trying in Xamarin to add a marker in the center of the map that marker will be fixe and can not be dragged, the map below that marker can be dragged normally. I need to get the

Solution 1:

On iOS, you can use the RegionChanged on a MKMapViewDelegate subclass to get updates as the user scrolls the map. Use the MKMapView.Region to get the center (CLLocationCoordinate2D) of the map's view;

classMyMapDelegate : MKMapViewDelegate
{
    publicoverridevoidRegionChanged(MKMapView mapView, bool animated)
    {
        Console.WriteLine($"{mapView.Region.Center.Latitude} : {mapView.Region.Center.Longitude}");
        // Update your map's MKPointAnnotation's Coordinate to match the mapView.Region.Center
    }
}

On Android, you can use the OnCameraMove on the GoogleMap.IOnCameraMoveListener to get updates as the user scrolls the map. Use GoogleMap.CameraPosition.Target to obtain the Lat/Long of the center of the map's view and update your MarkerOptions's position:

publicclassMapCameraMoveListener : Java.Lang.Object, GoogleMap.IOnCameraMoveListener
{
    readonly GoogleMap googleMap;

    publicMapCameraMoveListener(GoogleMap googleMap) { this.googleMap = googleMap; }

    publicvoidOnCameraMove()
    {
        Log.Debug("SO", $"{googleMap?.CameraPosition.Target.Latitude} : {googleMap?.CameraPosition.Target.Longitude}");
        // Update your map's `MarkerOptions`'s position to match the CameraPosition.Target
    }
}

For Xamarin.Forms look at the samples on how to custom the map via custom renderers to implement those native features shown above:

Post a Comment for "In Xamarin I Need To Set Marker In The Center Of The Map And Get The Position"