Kev*_*vin 5 annotations mkmapview swift
我正在开发一个应用程序,该应用程序可以在用户当前位置放置图钉,并根据用户输入的位置来放置图钉。当前位置注释放置得很好,但是当尝试放置用户的输入坐标时,注释未放置。
我已经正确添加了注释,map.addAnnotation(annotation)其中 annotation.coordinate (for New York) is CLLocationCoordinate2D(latitude: 40.713054, longitude: -74.007227999999998)。
我发现该viewForAnnotation方法没有被调用,我认为这就是没有放置注释的原因(打印注释不会产生任何结果)。我有一个请求输入坐标的测试应用程序,它可以放置注释。同样在测试应用程序中,将 viewForPrints 中的注释打印出一个值。
下面我粘贴了一些我认为与该问题相关的代码。如果您需要更多请评论。
import UIKit
import MapKit
import CoreLocation
class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
}
Run Code Online (Sandbox Code Playgroud)
下面的这个函数从另一个类获取值并将坐标转换为 CLLocationCooperative2D 类型。如上所述,注释.坐标(对于纽约)产生CLLocationCoordinate2D(latitude: 40.713054, longitude: -74.007227999999998),因此 add 可以保存坐标。
func add(newLocation location_one:[String:Any]) {
let momentaryLat = (location_one["latitude"] as! NSString).doubleValue
let momentaryLong = (location_one["longitude"] as! NSString).doubleValue
let annotation = MKPointAnnotation()
annotation.title = location_one["title"] as? String
annotation.coordinate = CLLocationCoordinate2D(latitude: momentaryLat as CLLocationDegrees, longitude: momentaryLong as CLLocationDegrees)
map.addAnnotation(annotation) //not sure if this adds the annotation!
self.map.centerCoordinate = annotation.coordinate
}
func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {
return nil
}
let identifier = "pinAnnotation"
var annotationView = map.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
map.showAnnotations(map.annotations, animated: true)
return annotationView
}
Run Code Online (Sandbox Code Playgroud)
第一的,
func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
这是一个委托方法,不用于设置注释,它只是
返回与指定注释对象关联的视图,即为指定注释显示的注释视图,如果要显示标准注释视图,则返回 nil。
如果您没有实现此方法,或者如果您从实现中返回除用户位置注释之外的注释,则地图视图将使用标准图钉注释视图。
在这里查看 -
https://developer.apple.com/reference/mapkit/mkmapviewdelegate/1452045-mapview
该addAnnotation()方法足以添加默认引脚注释。它可能不会被调用,因为您可能已经调用了
func add(newLocation location_one:[String:Any])来自后台队列,调用addAnnotatinon()内部方法。
如果您在后台队列上的其他控制器中调用 add 方法,可能是一些完成处理程序,那么您需要显式执行此操作并在主线程上调用 add 注释方法
DispatchQueue.main.async {
self.map.addAnnotation(annotation) //Yes!! This method adds the annotations
}
Run Code Online (Sandbox Code Playgroud)
出于调试目的,将控制器更改为此,甚至删除委托,然后您也将看到注释。
import UIKit
import MapKit
class ViewController: UIViewController{
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let annotation = MKPointAnnotation()
annotation.title = "Test Location"
annotation.coordinate = CLLocationCoordinate2D(latitude: 40.71304, longitude: -74.0072)
self.map.addAnnotation(annotation)
}
}
Run Code Online (Sandbox Code Playgroud)
所以一定要确保——
- addAnnotation 方法从主线程调用
- viewForAnnoation 方法 - 您可以使用它来提供对注释默认引脚的自定义。