简体   繁体   English

在 Alamofire 闭包中添加注解

[英]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 :在 MKMapViewDelegate 中,我尝试显示 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.只有 1 annocation.title出现其他人没有。 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.看起来您正在重用lbl并将其添加到每个注释中,因此您只能在最后一个注释中看到它。 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.您必须为每个注释创建新的UILabel并将其添加为子视图,就像使用pinlbl ,因为您现在拥有的是从前一个注释中删除lbl并将其重新添加到下一个注释,直到它停止并将其保留在地图上的最后一个注释中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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