OGeek|极客世界-中国程序员成长平台

标题: ios - 如何制作带有居中注释的可拖动 map [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:42
标题: ios - 如何制作带有居中注释的可拖动 map

我正在尝试制作可拖动的注释。但不是让注释可拖动,我希望 mapView 在注释周围移动。所以注释将一直停留在 mapView 的中心,但是当用户完成拖动 map 并单击保存时,我可以获得注释坐标。 我有什么办法可以做到这一点? 假设我已经在屏幕上有注释。

我的 ViewDidLoad

    mapView.delegate = self

    let anno = MKPointAnnotation()
    anno.coordinate = CLLocationCoordinate2D(latitude: 41.417110, longitude: -92.914788)

    mapView.addAnnotation(anno)

我对注释的看法

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    guard annotation is MKPointAnnotation else { return nil }
    let identifier = "marker"
    var view: MKAnnotationView
    if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        as? MKMarkerAnnotationView {
        dequeuedView.annotation = annotation
        view = dequeuedView
    } else {
        view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        view.isDraggable = true
        view.canShowCallout = false
    }
    return view
}



Best Answer-推荐答案


在 map 顶部添加标记图像,将其添加为 subview 。然后,您通常可以在用户停止滚动时使用 mapView.center Coordinate 获取中心坐标。

let markerImageView = UIImageView(image: UIImage(named: "Orange"))
markerImageView.center = mapView.center
mapView.addSubview(markerImageView)

关于ios - 如何制作带有居中注释的可拖动 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52847490/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4