简体   繁体   中英

Add Annocation in Alamofire closure

I'm trying to add annocations on map which locations and title label's coming from server and I try this so for

Services.MyService(pagingParams: pagingparam1, completed: {ret in
        self.getAdverts = ret
        for elements in self.getAdverts{
              
                let coordinates : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: CLLocationDegrees(elements.LOCATIONS[0])!, longitude: CLLocationDegrees(elements.LOCATIONS[1])!)
                pinCords.coordinate = coordinates
                pinCords.title = String(elements.PRICE!)
                self.map.addAnnotation(pinCords)
           
        }


    })

And in MKMapViewDelegate I try to show annocations :

extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
       if annotation is MKUserLocation == true
    {

        return nil
    }
    
           let av = MKAnnotationView(annotation: annotation, reuseIdentifier: "offercount")

    let annoIcon = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
    annoIcon.contentMode = .scaleAspectFit
   
    annoIcon.image = UIImage(named: "cancel-red")
    
    lbl.text = annotation.title!

    lbl.backgroundColor = UIColor.clear
    lbl.textAlignment = .center
    lbl.textColor = .white
    lbl.alpha = 1
    lbl.numberOfLines = 1
    lbl.adjustsFontSizeToFitWidth = true

    lbl.font = UIFont.systemFont(ofSize: 12)

    lbl.layer.masksToBounds = true
    


    av.backgroundColor = AreaColors.advertColor
    av.canShowCallout = true
    av.frame = CGRect(x: 0, y: 0, width: annoIcon.frame.size.width + lbl.frame.size.width + 4, height: 20)
    av.layer.cornerRadius = av.layer.bounds.height/4
    
    let pinlbl = UILabel(frame: CGRect(x:av.layer.bounds.width/2-10, y:av.layer.bounds.height-10, width:20, height:20))
    pinlbl.layer.masksToBounds = true
    pinlbl.layer.cornerRadius = 20
    pinlbl.backgroundColor = AreaColors.advertColor
    av.addSubview(pinlbl)
    av.addSubview(annoIcon)
    av.addSubview(lbl)


    return av
}

There is a label in green view which image is below在此处输入图片说明

Only 1 annocation.title is appear others don't. But when I click to any annocation I can see the title.

I think the problem is async closure but I really don't fix it. Am I doing something wrong?

It looks like you're reusing your lbl and adding it to each annotation, hence you're only seeing it in the last annotation. You will have to create new UILabel for each annotation and add it as a subview the same way you're doing with pinlbl , as what you have now is removing lbl from the previous annotation and re-adding it to the next one until it stops and persists it in the last annotation on the map.

Just change the following

annoIcon.image = UIImage(named: "cancel-red")
// create new label here
let lbl = UILabel()

// the rest of your code can stay the same

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM