【问题标题】:How to let user to add custom annotation?如何让用户添加自定义注释?
【发布时间】:2019-02-07 10:14:32
【问题描述】:

我找不到任何文档、视频或 stackoverflow 答案。

这是我的问题。我创建了地图并添加到我的自定义 MKAnnotation 和 MKAnnotationView 中。

我想让用户创建自定义 pin 并通过 CoreData 保存到本地

MyCustomAnnotation 具有相同的属性,即标题、副标题和坐标。

我想出的第一个解决方案是放置一个按钮,该按钮创建一个可拖动的 pin 到用户位置。

但我需要获得更简单、更复杂的解决方案。

private func addPins() {
    let list = PrivateLocations.shared.initLocations()
    for pin in list {
        map.addAnnotation(pin)
    }
}

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    if view.annotation is MKUserLocation { return }
    let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
    let customView = views?[0] as! CustomCalloutView
    customView.delegate = self
    customView.isUserInteractionEnabled = true
    customView.titleLabel.text = view.annotation?.title!
    customView.desc.text = view.annotation?.subtitle!
    customView.center = CGPoint(x: view.bounds.size.width / 2, y: -customView.bounds.size.height*0.52)
    view.addSubview(customView)
    map.setCenter((view.annotation?.coordinate)!, animated: true)
}

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

    if annotation is MKUserLocation {
        return nil
    } else {
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "CustomAnnotationView")
        annotationView.image = UIImage(named: "myImage")
        annotationView.canShowCallout = false
        return annotationView
    }
}

最后这是我的 CustomPin 类:

var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?

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

【问题讨论】:

  • 我不知道为什么用户对我的问题投反对票。你能告诉我这个话题有什么问题吗?是重复的,还是离题的,还是我解释得不好。

标签: swift xcode mapkit mkannotation


【解决方案1】:

我就是这样解决这个问题的,

1) 创建一个 UIView 供用户显示他想要添加注释的位置。

2) 在其中添加平移手势识别器。

func addPanGesture(view: UIView) {
    let pan = UIPanGestureRecognizer(target: self, action: #selector (self.handlePan(sender:)))
    view.addGestureRecognizer(pan)
}

3) 在我的选择器函数中,我调用 pinDropped() 函数

  @objc func handlePan(sender: UIPanGestureRecognizer) {

    let view = sender.view!
    let translation = sender.translation(in: self.mapView)

    switch sender.state {
    case .began, .changed:
        pinImage.center = CGPoint(x: dropPinImage.center.x + translation.x, y: dropPinImage.center.y + translation.y)
        sender.setTranslation(CGPoint.zero, in: view)
        break
    default:
        pinDropped()
        break
    }
}

4) 我在 pinDropped func 中写下将要发生的事情

func pinDropped() {
    DispatchQueue.main.async {
        let pin = CustomPin(self.lastOrigin, "pin")
        self.mapView.addAnnotation(pin)
    }
    self.saveButton.alpha = 1
    pinImage.alpha = 0
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2015-08-13
    • 2011-07-15
    • 1970-01-01
    相关资源
    最近更新 更多