2014-02-28 89 views
0

我正在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); 
} 

手勢處理程序永遠不會被調用!

回答

3

事實證明,我不需要添加手勢來完成此操作。 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... 
}