如何解决自定义点注释从重叠消失的问题? [英] How to fix custom Point Annotations from disappearing from overlap?

查看:111
本文介绍了如何解决自定义点注释从重叠消失的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过MapKits本地搜索显示自定义点注释.当注释第一次加载到地图上时,所有注释都会显示,但是重叠的注释会消失.并且只有当您放大该区域时,它们才会重新出现.

Attempting to show custom point annotations from MapKits local search. When the annotations first load on the map all of them show, but then the overlapping ones disappear. And they only reappear when you zoom in on the area.

许多堆栈解决方案已向用户view?.displayPriority = .required声明.但是出于某种原因,这一行代码无法正常工作.

Many stack solutions have stated to user view?.displayPriority = .required. But for some reason this line of code doesn't work.

按下按钮时本地搜索功能

Local Search Function on button press

@IBAction func groceryLocalSearch(_ sender: Any) {
    self.localStoresArray.removeAll()
    self.LocalMapKit.removeAnnotations(self.LocalMapKit.annotations)
    currentLocationBtn.isHidden = false
    localRequest.naturalLanguageQuery = "Grocery"
    //localRequest.region = LocalMapKit.region

    self.localSearchBtn.isEnabled = false

    let search = MKLocalSearch(request: localRequest)

    search.start(completionHandler: {(response, error) in

        if error != nil{
            print("Error occured when searching: \(error!.localizedDescription)")
        } else if response!.mapItems.count == 0 {
            print("There were no results in the search")
        } else {
            print("\(response!.mapItems.count) Results found!")
            for item in response!.mapItems {
                //Add each item to a array to access in table view
                self.localStoresArray.append(item)
                let stores = MKPointAnnotation()
                stores.title = item.name
                stores.coordinate = item.placemark.coordinate
                self.LocalMapKit.addAnnotation(stores)
            }
        }
        self.LocalMapKit.showAnnotations(self.LocalMapKit.annotations, animated: true)
        self.localStoresTableView.reloadData()
    })

查看注释功能

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
    guard annotation is MKPointAnnotation else { return nil }

    let identifier = "Annotation"

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.displayPriority = .required
        //annotationView?.canShowCallout = true
    } else {
        annotationView!.annotation = annotation

    }

    return annotationView
}

我想要它,以便当用户进行本地搜索时,它显示所有批注无关紧要,而不必放大.

I want it so that when the user does the local search it shows all annotations not matter if they are close to each other without having to zoom in.

当前地图视图的图像:

推荐答案

您显然没有为地图视图设置delegate,因为这些注释视图不是MKPinAnnotationView,而是默认的MKMarkerAnnotationView.如果要实现MKMapViewDelegate方法,则必须设置地图视图的delegate(在IB或以编程方式).

You apparently haven’t set the delegate for your map view because those annotation views are not MKPinAnnotationView, but rather the default MKMarkerAnnotationView. If you’re going to implement MKMapViewDelegate methods, you have to set the delegate of the map view (either in IB or programmatically).

此消失的原因是,默认的MKMarkerAnnotationView已配置为启用群集,但您尚未注册MKMapViewDefaultClusterAnnotationViewReuseIdentifier.

This disappearing act is because the default MKMarkerAnnotationView is configured to enable clustering but you haven’t registered a MKMapViewDefaultClusterAnnotationViewReuseIdentifier.

因此,如果您确实想要图钉注解视图并且不想聚类,请设置地图视图的delegate,然后您的方法就可以完成您想要的操作.

So, if you really want pin annotation views and you don’t want clustering, set your map view’s delegate and your method should accomplish what you want.

我个人建议您通过将注释视图的配置移到MKPinAnnotationView子类中来减少视图控制器膨胀:

I’d personally suggest you reduce view controller bloat by moving the configuration of your annotation view into a MKPinAnnotationView subclass:

class CustomAnnotationView: MKPinAnnotationView { // or use `MKMarkerAnnotationView` if you want
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        displayPriority = .required
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

然后,如果您的目标是iOS 11及更高版本,则可以在viewDidLoad中注册您的课程,而不必完全实现mapView(_:viewFor:):

Then, if you’re targeting iOS 11 and later, in your viewDidLoad, you can register your class, and you don’t have to implement mapView(_:viewFor:) at all:

mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

或者,如果您想适当地享受群集的话,可以扩展CustomAnnotationView:

Or, if you want to enjoy clustering properly, you can expand your CustomAnnotationView:

class CustomAnnotationView: MKPinAnnotationView { // or use `MKMarkerAnnotationView` if you want
    static let preferredClusteringIdentifier: String? = Bundle.main.bundleIdentifier! + ".CustomAnnotationView"

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        clusteringIdentifier = CustomAnnotationView.preferredClusteringIdentifier
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var annotation: MKAnnotation? {
        didSet {
            clusteringIdentifier = CustomAnnotationView.preferredClusteringIdentifier
        }
    }
}

然后注册您的注释视图和群集注释视图:

And then register both your annotation view and a cluster annotation view:

mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)

然后,您可以轻松完成群集工作.

Then you enjoy clustering with minimal effort.

这篇关于如何解决自定义点注释从重叠消失的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆