【发布时间】: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