问题描述
这个想法很简单。我有一个StopData()数组,我创建了一个自定义结构来通过对每个循环执行一个操作来显示mapView的不同位置。我的代码看起来像这样:
var route = [StopData]()
struct StopData {
var addr: String?
var coord: CLLocationCoordinate2D?
var number: Int?
var completed = false
}
func addStopsToMap() {
mapView.removeAnnotations(mapView.annotations)
for stop in route {
let annotation = MKPointAnnotation()
annotation.coordinate = stop.coord!
annotation.title = "\(stop.number!)"
annotation.subtitle = stop.addr
mapView.addAnnotation(annotation)
}
}
func attemptLocationAccess() {
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
return NonClusteringAnnotation(annotation: annotation,reuseIdentifier: "NonClusteringAnnotation")
}
// MARK: - Location manager functions
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
return
}
func locationManager(_ manager: CLLocationManager,didFailWithError error: Error) {
locationManager.requestWhenInUseAuthorization()
}
func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
return
}
manager.requestLocation()
}
我还有一个自定义类,如下所示:
class NonClusteringAnnotation: MKMarkerAnnotationView {
override var annotation: MKAnnotation? {
willSet {
displayPriority = .required
}
}
}
我想完成三件事。
- 显示每个注释图钉并使其不聚集
- 如果完成的变量为false,则显示自定义注释
- 如果完成为true,则显示其他自定义注释
使用此代码,每个引脚都会显示在地图上,并且不会聚集。但是,它还包括当前位置的蓝点,与所有注释的其余部分一样,该点显示为红色图钉。因此,我想知道如何制作多个自定义注释视图,并仅为它们分配某些注释,同时仍保留指示用户当前位置的蓝点?
到目前为止,我尝试添加一个布尔变量(如willUseCustomAnnotation),该变量将触发不同的返回值,如下所示:
func addStopsToMap() {
mapView.removeAnnotations(mapView.annotations)
willUseCustomAnnotation = true
for stop in route {
let annotation = MKPointAnnotation()
annotation.coordinate = stop.coord!
annotation.title = "\(stop.number!)"
annotation.subtitle = stop.addr
mapView.addAnnotation(annotation)
}
willUseCustomAnnotation = false
}
func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if willUseCustomAnnotation {
return NonClusteringAnnotation(annotation: annotation,reuseIdentifier: "NonClusteringAnnotation")
} else {
return nil
}
}
这是行不通的,因为我想它首先将所有注释添加到mapview中,然后一次为其创建视图。
我在论坛上阅读过以更改批注的视图,您必须在viewForAnnotation函数中创建自己的视图,但这仅在您需要单个自定义视图而不是像我想要的多个自定义视图的情况下那样。我是MapKit的新手,所以非常感谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)