Search code examples
mkmapviewswift4mkannotationmkannotationview

Pan to new mapView Annotation Location - not zoom


When adding an annotation to my mapView using mapView.addAnnotation, mapView.setRegion correctly animates and displays the map's center & span.

But when adding a new annotation and then calling mapView.setRegion, the view again starts from very wide, and then animates/zooms in to the new center & span.

I would like to know if there is a way to PAN from the previous region to the new region (center & span), rather than starting zoomed out, then zooming all the way back in again.

mapView.setRegion starts zoom from way out here each time: Image

I have defined 2 custom classes : MarkAnnotation & MarkAnnotationView

class MarkAnnotation : NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
    }
}

class MarkAnnotationView: MKAnnotationView {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
    super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        guard let markAnnotation = self.annotation as? MarkAnnotation else { return }
        image = UIImage(named: "customAnnotation")
    }
}

and in my MKMapViewDelegate :

func addMarkAnnotation() {
    for mark in marks {
        let markName : String = mark.name
        let markLat : Double = mark.lat
        let markLon : Double = mark.lon
        let markLL = CLLocationCoordinate2DMake(markLat, markLon)
        let markAnnotation = MarkAnnotation(coordinate:markLL, title:markNumStr, subtitle: markName)
        mapView.addAnnotation(markAnnotation)
    }
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = MarkAnnotationView(annotation: annotation, reuseIdentifier: "MarkView")
    annotationView.canShowCallout = true
    return annotationView
}

func centerMapOnLocation(location:CLLocation, span:Double) {
    let mapSpan = MKCoordinateSpanMake(0.2, 0.2)
    let recenterRegion = MKCoordinateRegion(center: location.coordinate, span: mapSpan)
    mapView.setRegion(recenterRegion, animated: true)
}

And then in the ViewController, when user presses a button, a mark is added at a specified lat/lon

func addMarkBtnPressed() {
    // .... (have not shown : conversion of text fields from String to Double, adding Mark to marks array, etc ) ... All this works fine

    addMarkAnnotation()

    let mapCenter = CLLocation(latitude: markLat, longitude: markLon)
    centerMapOnLocation(location: mapCenter, span: span)
}

Solution

  • Try the following code:

    func centerMapOnLocation(location:CLLocation, span:Double) {
        let mapSpan = mapView.region.span
        let recenterRegion = MKCoordinateRegion(center: location.coordinate, span: mapSpan)
        mapView.setRegion(recenterRegion, animated: true)
    }