标记注释重叠并相互交换

时间:2015-06-25 06:50:51

标签: ios swift mkmapview mapkitannotation

我在MKMapView上遇到关于显示多个注释的问题。我的问题是,我有三种类型的注释,如(select_StarFlagsimple_FlagcurrentLocation_Flag)。所有的旗帜都在地图上显示但是它们之间的位置互换,当我滚动地图时,一些旗帜相互重叠。我正在使用以下代码

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 个答案:

没有答案