How to make annotation like

I have an app with a map and a lot of annotations done with a custom image and my problem is when I drag the map or zoom in on the annotation another point (lat, lon) is displayed.

So I was wondering, because I see this error in other applications, is there a way to fix it (be like map annotations that always show lat and lon permissions).

This is the code I am using to annotate:

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

    if annotation is MKUserLocation {
        return nil
    }


    var reuseId = ""

    if annotation is FBAnnotationCluster {

        reuseId = "Cluster"
        var clusterView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId, configuration: FBAnnotationClusterViewConfiguration.default())
        return clusterView
    }else{

        reuseId = "pin"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if  annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            annotationView!.image = UIImage(named: "locat.png")


        }
        else {
            annotationView!.annotation = annotation

        }

        let cpa = annotation as! FBAnnotation

        annotationView!.image = nil

        let image = UIImage(named:cpa.nameImage)


        annotationView!.image = image


        return annotationView
    }




}

      

If you have a question about what I'm asking, or anything else, please feel free to leave a comment.

Thanks in advance!

EDIT

I am uploading a video showing my problem

EDIT2

And here is a video from the cards that show that their annotation does not lose its lat and lon.

+3


source to share


2 answers


Your problem is MKAnnotationView

. centerOffset

, you need to configure this property infunc mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

As defined in MKAnnotationView documentation

centerOffset Property The offset (in points) at which the view is displayed.

var centerOffset: CGPoint

Discussion By default, the center point of the annotation view is placed at the coordinate point of the linked annotation. You can use this property to reposition the annotation as needed. These x and y offset values ​​are measured in points. Positive offset values ​​move the view of the annotation down and to the right, while negative values ​​move up and left.



Something like that

var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if  annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            annotationView!.image = UIImage(named: "locat.png")
            //this is an example you must to get in account the form of your current view and what you want, I think your values are CGPoint(x: 0.5, y: 1)
            annotationView?.centerOffset = CGPoint(x: 0, y: -((UIImage(named: "locat.png")?.size.height)!/2))

        }
        else {
            annotationView!.annotation = annotation

        }

      

Hope this helps you

+1


source


You don't lose lat long, but your lat long is in the center of your annotation image, you need to change the anchorPoint.

annotationView?.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)

      



in

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

      

+1


source







All Articles