I'm writing an app in Xamarin.IOS which has a controller that displays a Google Maps MapView, and I would like to add a marker when the user taps and holds over any point in the map

So far I've tried to add the gesture recognizer to the MapView object, but its not working. This is my code for the map controller:

public async override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    View.BackgroundColor = UIColor.White;
    var geolocator = new Geolocator { DesiredAccuracy = 50 };
    location = await geolocator.GetPositionAsync (10000);

    var camera = CameraPosition.FromCamera (latitude: location.Latitude, 
            longitude: location.Longitude, zoom: 6);
    mapView = MapView.FromCamera (RectangleF.Empty, camera);
    mapView.MyLocationEnabled = true;

    var longGesture = new UILongPressGestureRecognizer (LongPress);
    longGesture.MinimumPressDuration = 2.0;
    mapView.AddGestureRecognizer (longGesture);

    mapView.StartRendering ();
    View = mapView;
}

public override void ViewWillDisappear (bool animated)
{   
    mapView.StopRendering ();
    base.ViewWillDisappear (animated);
}

[Export("LongPress")]
void LongPress (UILongPressGestureRecognizer gesture)
{
    Console.WriteLine (gesture.LocationInView(mapView).X + gesture.LocationInView(mapView).Y);
}

the gesture handler is never called!

有帮助吗?

解决方案

It turns out that I don't need to add a gesture to accomplish this. The MapView object exposes an event which I can subscribe:

mapView.LongPress += HandleLongPress;
void HandleLongPress (object sender, GMSCoordEventArgs e)
{
//Here I can add my marker. e contains the location point where the user tapped...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top