Flag Annotation overlap and interchange to each other

207 views Asked by At

I am facing a problem about show multiple annotation on MKMapView. My question is that, I have three type annotation like as(select_StarFlag, simple_Flag, currentLocation_Flag). all flag are show on map but there are interchange the position between them, when I scroll the map, some Flag overlap each other. I am using following code

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if (annotation.isKindOfClass(MKUserLocation)) {
        //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
        //return nil so map view draws"Blue Dot" for standard user location
        return nil
    }
if (annotation.isKindOfClass(MKPointAnnotation)){
    let reuseId = "test"
    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as MKAnnotationView!
    if (anView == nil) {

            anView =  MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            anView.canShowCallout = true
            var data = Double(annotation.coordinate.latitude)
            var data1 = Double(annotation.coordinate.longitude)
            var flag = Bool(false)
            for geoData in ysDataBaseGeolocationArray{

                var geolocationlat = Double()
                var geolocationlong = Double()
                geolocationlat = Double(geoData.objectAtIndex(0) as NSNumber)
                geolocationlong = Double(geoData.objectAtIndex(1) as NSNumber)


                if data == geolocationlat && data1 == geolocationlong {
                    println(geolocationlat)
                    println(geolocationlong)
                    flag = true
                    break
                }
            }
           if flag {

            anView.image = UIImage(named:"select_StarFlag")
        }
        else if data == Double(ysLatitude) && data1 == Double(ysLongitude){
            if  ysValueFlag != false{
                anView.image = UIImage(named:"currentLocation_Flag")

            }else {

                anView.image = UIImage(named:"simple_Flag")
            }
        }
        else {

            anView.image = UIImage(named:"simple_Flag")
        }
    }
    else {
        //we are re-using a view, update its annotation reference..
        anView.canShowCallout = true
        anView.annotation = annotation
        }
        return anView
    }
    return nil
}
0

There are 0 answers