How to set a custom annotations for all points except for user location?

≯℡__Kan透↙ 提交于 2021-01-29 14:30:53

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/61785563/how-to-set-a-custom-annotations-for-all-points-except-for-user-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!