1

I have set of images that need to be set as left icon in leftcalloutaccessoryview in MKAnnotation. I already done it using if else statement. My problem is, instead of using 'if else' statement, it will be great if I can code it in 'for' statement using loop of array. Can anybody help me on this?How can I code the 'leftcalloutaccessoryview' using for loop array using set of images and coordinate that have in my set of arrays? I have been face this code for a month :(...thank you guys

    //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    }
eynamacho
  • 45
  • 4
  • Your question is very similar to this one: http://stackoverflow.com/questions/25631410/swift-different-images-for-annotation/25635210#25635210. Only difference is that you want to use a different image for the callout instead of the annotation view. –  Mar 10 '15 at 11:14
  • yup Anna...very similar...only that I want to implement it in 'for' statement loop (images,coordinate declared in set of array)...imagine if I have 30 places of interest, i don't think it is appropriate for me to declare it one by one in the code...do you have any other solutions Anna? – eynamacho Mar 10 '15 at 23:56
  • The point is that you cannot access the leftCalloutAccessoryView property while you are creating the annotations. The linked answer shows how you can pass data into the viewForAnnotation delegate method via the annotation objects themselves. So instead of using the built-in MKPointAnnotation class, create a custom class with the property you need and use that property in viewForAnnotation. –  Mar 11 '15 at 00:57

1 Answers1

0

Wondering if this helps:

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

So just add all the 30 POI title to poiSubTitle and image path to imageName without changing the for loops...

Zaid M. Said
  • 314
  • 1
  • 15
  • I assume you're suggesting to put this in viewForAnnotation. Although this may work, it would be inefficient (the viewForAnnotation can be called multiple times for the same annotation), unnecessarily forces you to declare and use class-level variables and continues to tie the logic to the annotation's title. A more object-oriented approach is to create a well-defined custom class for the annotations which contain all the required properties and pass those objects around rather than constantly iterating through arrays looking for things. –  Mar 11 '15 at 10:59
  • Yes, I just simplified the code based on what asked above. The better way is to do a function as you suggested Anna. – Zaid M. Said Mar 11 '15 at 15:21