添加谷歌地图标记,点击并按住手势

时间:2014-02-28 04:04:34

标签: xamarin.ios xamarin google-maps-sdk-ios

我正在Xamarin.IOS中编写一个应用程序,它有一个显示Google Maps MapView的控制器,我想在用户点击并保持地图中的任何一点时添加一个标记

到目前为止,我已尝试将手势识别器添加到MapView对象,但它无法正常工作。这是我的地图控制器代码:

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);
}

永远不会调用手势处理程序!

1 个答案:

答案 0 :(得分:5)

事实证明,我不需要添加手势来完成此任务。 MapView对象公开了一个我可以订阅的事件:

mapView.LongPress += HandleLongPress;
void HandleLongPress (object sender, GMSCoordEventArgs e)
{
//Here I can add my marker. e contains the location point where the user tapped...
}