【问题标题】:Pan to new mapView Annotation Location - not zoom平移到新的 mapView 注释位置 - 不缩放
【发布时间】:2018-04-11 19:56:26
【问题描述】:

当使用 mapView.addAnnotation 向我的 mapView 添加注释时,mapView.setRegion 正确地设置动画并显示地图的中心和跨度。

但是当添加一个新注释然后调用mapView.setRegion 时,视图再次从非常宽开始,然后动画/放大到新的中心和跨度。

我想知道是否有办法从前一个区域平移到新区域(中心和跨度),而不是开始缩小,然后再一直放大。

mapView.setRegion 每次都从这里开始缩放:

我定义了 2 个自定义类: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")
    }
}

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

然后在 ViewController 中,当用户按下按钮时,在指定的纬度/经度处添加一个标记

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

【问题讨论】:

    标签: mkmapview swift4 mkannotation mkannotationview


    【解决方案1】:

    试试下面的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多