如何为除用户位置以外的所有点设置自定义注释? [英] How to set a custom annotations for all points except for user location?

查看:54
本文介绍了如何为除用户位置以外的所有点设置自定义注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

授权检查之后,要获取用户位置,我将调用此CLLocation委托函数:

After the authorization checks, to obtain the user location, I am calling this CLLocation delegate function:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        guard let latestLocation = locations.first else { return }

        if currentCoordinate == nil {
            zoomToLocation(with: latestLocation.coordinate)
        }
        currentCoordinate = latestLocation.coordinate
    }

然后我放大到用户的位置:

And then I zoom onto the user's location:

func zoomToLocation(with coordinate: CLLocationCoordinate2D) {
        let regionDimension : Double = 1000
        let zoomRegion = MKCoordinateRegion.init(center: coordinate, latitudinalMeters: regionDimension, longitudinalMeters: regionDimension)
        mapView.setRegion(zoomRegion, animated: true)
    }

到目前为止,一切都很好.但是我正在加载一些具有自定义批注的目的地",因此我需要调用此委托函数:

So far so good. But I am loading some "destinations" for which I have custom annotation so I need to call this delegate function:

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

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
        }

        annotationView?.image = UIImage(named: "icon_marker")
        annotationView?.canShowCallout = true
        return annotationView
    }

并且它将用相同的自定义标记替换用户位置的蓝点.这是一个非常奇怪的用户体验.如何从自定义注释中排除我的位置"?

And it replaces the blue dot for the user location with the same custom marker. This is a really weird user experience. How do I exclude "my location" from getting the custom annotation?

推荐答案

问题是您没有检查传入的注释.检查一下!如果它是MKUserLocation,则返回 nil .

The problem is that you are not examining the incoming annotation. Examine it! If it is a MKUserLocation, return nil.

这篇关于如何为除用户位置以外的所有点设置自定义注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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