Determine If Mkmapview Was Dragged/Moved

determine if MKMapView was dragged/moved

Look at the MKMapViewDelegate reference.

Specifically, these methods may be useful:

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

Make sure your map view's delegate property is set so those methods get called.

get MKMapView callbacks as map is being dragged by user?

You need implement MKMapViewDelegate methods for example

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;

or add your panGesture to map

UIPanGestureRecognizer* panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didDragMap:)]; 
panRec.delegate = self;
[self.mapView addGestureRecognizer:panRec];

and need implement pan delegate method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {   
return YES;
}

How to distinguish user dragging map from location change in MKMapView?

There are two distinct methods you can implement in your map view delegate to respond to these two events:

When the user drags the map around (or rotates it, or pinches/taps to zoom in, etc.): mapView:regionDidChangeAnimated:

When the map view is tracking the user's location and updates the view as the user's location changes: mapView:didUpdateUserLocation:

Try implementing both of these methods with some NSLog statements and mess around with the map view to get an idea of when and how frequently these methods are called.

Track MKAnnotation drag as it moves?

Sorry to be late to the party. I had the same need arise and encountered your post. Here is what I did.

First my map view delegate's view for annotation method. The annotation view is made draggable and a gesture recognizer is added. (My DetailAnnotationView is a subclass of MKAnnotationView. What it brings to the party is not pertinent to our focus.)

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = overviewMap.dequeueReusableAnnotationView(withIdentifier: DetailAnnotationView.reuseIdentifier) as? DetailAnnotationView
if annotationView == nil {
annotationView = DetailAnnotationView(annotation: nil)
annotationView!.isDraggable = true
let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureHandler))
recognizer.allowableMovement = CGFloat.infinity
recognizer.delegate = self
annotationView!.addGestureRecognizer(recognizer)
}
annotationView!.updateBounds(self)
return annotationView
}

Next the gesture handler. The detailAnnotation is an MKPointAnnotation. Note that I am using the result of MKMapView.convert to reposition a map. You can, of course, use it as you wish.

@objc func longPressGestureHandler(_ recognizer: UILongPressGestureRecognizer) {
switch recognizer.state {
case .changed:
guard let annotationView = overviewMap.view(for: detailAnnotation) else { fatalError("Cannot get detail annotation's view") }
detailMap.region.center = overviewMap.convert(recognizer.location(in: annotationView), toCoordinateFrom: annotationView)
default: break
}
}

Finally, I allow my gesture recognizer to operate simultaneously with the gesture recognizer that MapKit is using to drag the annotation view.

extension DualMapsManager : UIGestureRecognizerDelegate {

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return gestureRecognizer is UILongPressGestureRecognizer && gestureRecognizer.delegate === self && otherGestureRecognizer is UILongPressGestureRecognizer
}
}

And there you have it. If you ended up polling then you now have an alternative. If you arrived at a different solution then why not post it?

Detecting when MKMapView has stopped moving

Try this delegate:

mapView:regionDidChangeAnimated:

determine if MKMapView was dragged or zoomed

MKMapView is not based on UIScrollView so it doesn't call UIScrollViewDelegate methods. But nonetheless, you can instantiate UIPanGestureRecognizer and UIPinchGestureRecognizer, add them to your map view and work with their action methods.



Related Topics



Leave a reply



Submit