在注释地图swift中使用for循环数组在leftcalloutaccessoryview中设置唯一图像

时间:2015-03-10 05:28:22

标签: arrays loops swift mkannotation

我有一组图像需要在MKAnnotation的leftcalloutaccessoryview中设置为左图标。我已经使用if else语句完成了它。我的问题是,而不是使用' if else'声明,如果我可以将其编码为'声明使用数组循环。任何人都可以帮我这个吗?我如何编写' leftcalloutaccessoryview'使用for循环数组使用我的数组集合中的图像集和坐标?我已经面对这个代码一个月了:( ...谢谢你们

    //Define nearby Place of Interest
    let latArray = [24.469546, 24.469450]
    let longArray = [39.609105, 39.613062]
    let poiTitle = ["An-Nisa 1","An-Nisa 2"]
    let poiSubTitle = ["Gate 1","Gate 2"]
  //  let imageName = ["Jons.png","Locus.png"]

    for var i=0;i<poiSubTitle.count;i++
    {
        var latPoi: CLLocationDegrees = latArray[i]
        var longPoi: CLLocationDegrees = longArray[i]

        var locationPoiAnnot = MKPointAnnotation()

        locationPoiAnnot.title = poiTitle[i]
        locationPoiAnnot.subtitle = poiSubTitle[i]
        var locationPoi : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latPoi,longPoi)
        locationPoiAnnot.coordinate = locationPoi


        self.mapView.addAnnotation(locationPoiAnnot)

    }

}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
   if annotation is MKUserLocation {
              //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true

        let arrowButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        arrowButton.frame.size.width = 44
        arrowButton.frame.size.height = 44
        arrowButton.setImage(UIImage(named: "arrow.jpeg"), forState: .Normal)

        pinView!.rightCalloutAccessoryView = arrowButton

    var imageview = UIImageView(frame: CGRectMake(0, 0, 45, 45))

   if(annotation.subtitle == "I am here!")
        {

            imageview.image=UIImage(named: "Locus.png")

        }
        else if(annotation.subtitle == "Gate 1")
        {
            imageview.image=UIImage(named: "Jons.png")

        }

        else if(annotation.subtitle == "Gate 2")
        {

        imageview.image=UIImage(named: "Katara.png")

        }


        pinView!.leftCalloutAccessoryView = imageview


}
    else {
        pinView!.annotation = annotation
    }
    return pinView    }

1 个答案:

答案 0 :(得分:0)

想知道这是否有帮助:

let poiSubTitle = ["Gate 1","Gate 2"]
let imageName = ["Jons.png","Locus.png"]

for(var i = 0 ; i < imageName.count ; i++)
{
    if(annotation.subtitle == poiSubTitle[i])
    {
        imageview.image = UIImage(named: imageName[i])
    }
}

pinView!.leftCalloutAccessoryView = imageview

所以只需将所有30个POI标题添加到poiSubTitle,将图片路径添加到imageName,而不更改for循环...