TheRedCamaro3.0 3.0
TheRedCamaro3.0 3.0

Reputation: 831

Resizing MKAnnotationView as user zooms in and out

I am trying to resize my custom MKAnnotationView as the user scrolls in and out of the map similar to how the MKMarkerAnnotationView behaves. Currently when the user scrolls in and out of the map my custom annotation stays the same size instead of getting smaller when the user zooms out and larger when the user zooms in. I have tried using this example Outdated example however the code seems to be a bit outdated and when I tried the extension suggested here the behavior is not always as expected the view tends to shift a bit around the map and when the user scrolls in and out very fast the behavior is a bit strange.

Below is my raw implementation of the mapViewDelegate based loosely off of the apple project mentioned here.

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


        if annotation is MKUserLocation{
            return nil
        }else if let cluster = annotation as? MKClusterAnnotation{
            let view = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier, for: cluster) as? MKMarkerAnnotationView

            view?.annotation = cluster
            view?.markerTintColor = UIColor.green


            return view
        }
        else if let marker = annotation as? ReusableAnnotation{
            let view = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier, for: marker)
            view.annotation = marker
            view.clusteringIdentifier = "cluster"

            view.canShowCallout = true
//            view.detailCalloutAccessoryView = UIImageView(image: #imageLiteral(resourceName: "apolloRocket"))

//            view.image = #imageLiteral(resourceName: "apolloRocket").resizeImage(150, opaque: false) // #imageLiteral(resourceName: "apolloRocket")
            let subView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
            subView.backgroundColor = UIColor.red
            view.addSubview(subView)

            view.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
            return view
        }else{
            return nil
        }
    }

Thank you for all of your help in advance.

Upvotes: 0

Views: 232

Answers (0)

Related Questions