简体   繁体   中英

MKMapView Touch events

On my mapview when the user scrolls around and stops i want to send my web service the lat lng of the centre of the map. My problem is i want to fire the event after the user stops moving around the map. what current happens is the events are fired straight away. I'm currently using the touch events

TouchesBegin TouchesCancelled TouchesEnded

This is what i've seen when running on a simulator

TouchesBegin is always called when user touches the screen TouchesCancelled is called if i move the map. But its called immediately after the touches begin and Not when the user stops moving the map TouchesEnd is called when the user just touches the map and is called immediately after the touches begin.

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

I think the preferred way of doing this is by creating your own map delegate that responds to the map view events. This way, you can monitor changes to the map view by the user interacting with it as well as code programmatically changing the region

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM