2014-10-01 264 views
0

在我的mapview上,當用戶滾動並停止時,我想將我的web服務發送到地圖中心的緯度。我的問題是我想在用戶停止在地圖上移動之後觸發事件。目前發生的事情是事件馬上被解僱。我目前使用的觸摸事件MKMapView觸摸事件

TouchesBegin TouchesCancelled TouchesEnded

這是我所看到的在一個模擬器上

TouchesBegin總是被調用運行時,當用戶觸摸屏幕時 TouchesCancelled被稱爲如果我移動地圖。但它在觸摸開始後立即調用並且 不當用戶停止移動地圖時 當用戶剛剛觸摸地圖並在觸摸開始後立即調用時,會調用TouchesEnd。

 #region ToucheEvents 

    public override void TouchesBegan(NSSet touches, UIEvent evt) 
    { 
     base.TouchesBegan(touches, evt); 

     Console.WriteLine("Touchs Begin"); 
     _cancelTokenSource.Cancel(); 

     _operationMode = ParkingOperationMode.ParkingNotAvailable; 
     UpdateUi(); 
    } 


    public override void TouchesCancelled(NSSet touches, UIEvent evt) 
    { 
     base.TouchesCancelled(touches, evt); 

     Console.WriteLine("Touchs Cancelled"); 

     MapViewOnRegionChanged(); 
    } 


    public override void TouchesEnded(NSSet touches, UIEvent evt) 
    { 
     base.TouchesEnded(touches, evt); 
     Console.WriteLine("Touched Ended"); 

     _cancelTokenSource.Cancel(); 
    } 

    #endregion 

private void MapViewOnRegionChanged() 
    { 
     _cancelTokenSource = new CancellationTokenSource(); 
     _cancelToken = _cancelTokenSource.Token; 
     _cancelToken.Register(CancelCallBack); 

     if (Utility.ValidateOnlineStatus()) 
     { 
      ViewDef.ShowLoadingAnimation(); 
      Utility.AddNetworkConnection(); 

      Task.Run(() => 
      { 
       Thread.Sleep(1000); 

       if (!_cancelToken.IsCancellationRequested) 
       { 
        _cancelToken.ThrowIfCancellationRequested(); 
        Console.WriteLine("Firing GetLocation"); 

        GetParkingLocation(); 
       } 
       else { 
        Console.WriteLine("Location Cancelled"); 
       } 
      }, _cancelToken); 
     } 
    } 

回答

2

我認爲這樣做的首選方法是創建您自己的地圖代表,以響應地圖視圖事件。通過這種方式,您可以監視用戶與之交互的對地圖視圖的更改以及以編程方式更改區域的代碼

public override void ViewDidLoad() 
{ 
    var customDelegate = new CustomMapViewDelegate(); 
    customDelegate.OnRegionChanged += TheMapView_OnRegionChanged; 

    TheMapView.Delegate = customDelegate; 
} 

public void TheMapView_OnRegionChanged(object sender, MKMapViewChangeEventArgs e) 
{ 
    var latitude = TheMapView.Region.Center.Latitude; 
    var longitude = TheMapView.Region.Center.Longitude; 

    // Map change logic goes here 
} 

public class CustomMapViewDelegate : MKMapViewDelegate 
{ 
    public event EventHandler<MKMapViewChangeEventArgs> OnRegionChanged; 

    public override void RegionChanged(MKMapView mapView, bool animated) 
    { 
     if (OnRegionChanged != null) 
     { 
      OnRegionChanged(mapView, new MKMapViewChangeEventArgs(animated)); 
     } 
    } 
}