In Xamarin I need to set marker in the center of the map and get the position

最后都变了- 提交于 2021-01-27 19:06:11

问题


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 position of the map that was chosen by the user, I searched a lot for the internet and found nothing that could help me. This feature is widely used in the Uber app.


回答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;

class MyMapDelegate : MKMapViewDelegate
{
    public override void RegionChanged(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:

public class MapCameraMoveListener : Java.Lang.Object, GoogleMap.IOnCameraMoveListener
{
    readonly GoogleMap googleMap;

    public MapCameraMoveListener(GoogleMap googleMap) { this.googleMap = googleMap; }

    public void OnCameraMove()
    {
        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:

  • https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/map/


来源:https://stackoverflow.com/questions/46895114/in-xamarin-i-need-to-set-marker-in-the-center-of-the-map-and-get-the-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!