微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

viewForAnnotation混淆并迭代自定义pinColor

如何解决viewForAnnotation混淆并迭代自定义pinColor

代码应该 _ _ 被调用viewForAnnotation明确自身。 删除viewForAnnotationaddAnnotation行后的显式调用

viewForAnnotation是委托方法并且地图视图 需要显示注释时 自动调用 。如果没有自动调用它,请确保delegate已设置地图视图的属性self例如)。

(也是真正的问题),该代码假定viewForAnnotation委托方法只会在添加每个注释后立即被调用一次。

并非如此,并且不能保证。地图视图将viewForAnnotation在需要显示注释的任何时候调用,并且可以针对同一注释多次调用,也可以在实际添加注释之后很长时间(例如,在用户平移或缩放地图并使注释进入视图之后)被调用

看到MKAnnotationView是否缓冲其输入队列?了解一些其他详细信息以及指向其他答案(包括示例代码)的相关链接

基本上,您必须使用注解对象本身存储影响注解视图的属性,并从annotation传递到中的参数中检索这些属性viewForAnnotation

我为您的情况建议的是:

我假设您正在使用内置注释类MKPointAnnotation。除了使用MKPointAnnotation哪种不允许您将自定义status属性与注释对象本身存储在一起之外,还可以使用以下两种方法之一:

  • 创建一个既实现MKAnnotation协议又具有status属性自定义类。在创建注释时设置此属性,并从annotation传入的参数中提取其值viewForAnnotation并进行pinColor相应的设置。请参阅链接答案中的示例代码

  • 在实现协议的对象MysupplierData 本身中创建 对象MKAnnotation。因此,如果中的对象MysupplierData是某个类的实例,例如,supplier使supplier该类符合MKAnnotation协议,则可以MysupplierData调用时将对象本身添加到地图视图中addAnnotation

解决方法

目标是针对存储在结构数组中的某些值自定义图钉颜色。

通过这里的一些帮助,我实现了以下viewForAnnotation委托方法,该方法很好地基于结构数据数组的大小在循环中迭代调用此委托方法。因此,如果我要将所有引脚设置为一种颜色(例如,紫色)(这是下面代码中的注释行),则可以使用。

问题是当我放入一个开关以根据数组中的值设置颜色时,它会通过此代码,但不遵守任何大小写值来将其设置为备用颜色,并且所有内容都变为红色针(看似默认值)。我已经打印出状态并进行调试,以知道它正在进入交换机内部并相应地设置pinColor,但是它们似乎不粘。

func mapView(aMapView: MKMapView!,viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

       let theindex = mystructindex  // grab the index from a global to be used below

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"
        var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

        if pinView == nil {
            //println("Pinview was nil")
            pinView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true

            // Preventive if to keep this from being called beyond my arrays index value as the delegate getting called beyond the for loop for some unknown reason

            if (theindex < MySupplierData.count) {

                // Set the pin color based on the status value in MySupplierData structure array
                switch MySupplierData[mystructindex].status  {

                case 0,1:
                    println("Case 0 or 1 - setting to Red")
                    pinView!.pinColor = .Red  // Needs help,show red pin
                case 2:
                    println("Case 2 - Setting to Green")
                    pinView!.pinColor = .Green  // Looking Good 
                case 3:
                    println("Case 3 - Setting to Purple")
                    pinView!.pinColor = .Purple  // Could use a follow-up
                default:
                    println("Case default - Should Never Happen")
                    break;

                }   // end switch
            } // end if

            // pinView!.pinColor = .Purple  // This works fine without the switch and respects any color I set it to.
        }
        else {
            pinView!.annotation = annotation
        }

        return pinView
}

在ViewController的for循环中,我将其称为如下,但对返回值不做任何事情。

        // previous to this I setup some Titles and Subtitle which work fine
        self.theMapView.addAnnotation(myAnnotation)
        // Call to my mapview   
        mapView(theMapView,viewForAnnotation: myAnnotation)

我对返回的Pinview不执行任何操作-认为我不需要这样做,但是在使用切换代码时,所有的引脚在此时都被绘制为红色。从根本上说,我在这里一定错过了一些东西。


7-8-14更新以解决Anna的大力帮助/辅导中的修订代码问题。TKS!

几乎可以使用,地图中的所有图钉都有正确的颜色,但即时显示之外的图钉有时是错误的。在此处发布所有涉及的代码,因为它可能对其他人有所帮助,因为这似乎是关于如何在Google
Maps中进行自定义工作的非常普遍的问题。

建议使用一个自定义类将其他变量保存在自定义批注中-在这种情况下,状态值来自我的数据结构MySupplierData。

class CustomMapPinAnnotation : NSObject,MKAnnotation {
  var coordinate: CLLocationCoordinate2D
  var title: String
  var subtitle: String
  var status: Int

  init(coordinate: CLLocationCoordinate2D,title: String,subtitle: String,status: Int) {
    self.coordinate = coordinate
    self.title = title
    self.subtitle = subtitle
    self.status = status

  }
}

修改后的mapView-现在利用传递给它的新CustomMapPinAnnotation:

func mapView(aMapView: MKMapView!,viewForAnnotation annotation: CustomMapPinAnnotation!) -> MKAnnotationView! {

        let reuseId = "pin"          
        var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

        if pinView == nil {
            //println("Pinview was nil")
            pinView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true

            // Code to catch my custom CustomMapPinAnnotation so we can check the status and set the color               
            if annotation.isKindOfClass(CustomMapPinAnnotation)
            {
                println("FOUND OUR CustomMapPinAnnotation CLASS IN mapView")
                println(" Custom Title = \(annotation.title)")
                println(" Custom status passed = \(annotation.status)")
                switch annotation.status {

                case 0,1:
                    println("Case 0 or 1 - Setting to Red")
                    pinView!.pinColor = .Red
                case 2:
                    println("Case 2 - Setting to Green")
                    pinView!.pinColor = .Green
                case 3:
                    println("Case 3 - Setting to Purple")
                    pinView!.pinColor = .Purple 
                default:
                    println("Case default - Should Never Happen")
                    break;
                }  // switch   
            }  // if     
        }
        else {
            pinView!.annotation = annotation
        }
        return pinView
} //func mapView

在viewDidLoad中,使用设置和For循环设置注释

override func viewDidLoad() {
    super.viewDidLoad()

    // setup the region and Span 
    var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta,longDelta)

    // Set the region to the the first element of the structure array.
    var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(MySupplierData[0].latitude,MySupplierData[0].longitude),theSpan)

    // This set the Map Type (Standard,Satellite,Hybrid)
    self.theMapView.mapType = MKMapType.Standard

    // Now loop through the structure data from 1 top the end of the structure to map the data

    var mytitle: String = ""
    var mysubtitle: String = ""
    var myCustomPinAnnotation: CustomMapPinAnnotation

    for mystructindex = 0; mystructindex < MySupplierData.count; ++mystructindex {           
        println("INSIDE SUPPLIER LOOP INDEX = \(mystructindex)" )

        switch MySupplierData[mystructindex].status {
        case 0:
            mytitle =  "(Red) " + MySupplierData[mystructindex].company
        case 1:
            mytitle = "(Red) " + MySupplierData[mystructindex].company
        case 2:
            mytitle = "(Geeen) " + MySupplierData[mystructindex].company
        case 3:
            mytitle = "(Purple) " + MySupplierData[mystructindex].company
        default:
            mytitle = "? " + MySupplierData[mystructindex].company

        }    
        mysubtitle = MySupplierData[mystructindex].subtitle

         // Create the Custom Annotations with my added status code   
        myCustomPinAnnotation = CustomMapPinAnnotation(
            coordinate: CLLocationCoordinate2DMake(MySupplierData[mystructindex].latitude,MySupplierData[mystructindex].longitude),title: mytitle,// custom title
            subtitle: mysubtitle,// custom subtitle
            status: MySupplierData[mystructindex].status)  // status that will drive pin color

        // put this annotation in the view.
        self.theMapView.addAnnotation(myCustomPinAnnotation)
    }  // For

    // This line brings up the display with the specific region in mind,otherwise it seems to default to a US Map.
    self.theMapView.setRegion(theRegion,animated: true)

}  // viewDidLoad

调试输出显示,在mapView中的自定义viewForAnnotation在内部自行执行之前,For循环按预期完成了创建myCustomPinAnnotation的执行。当我将地图移到即时视图之外的区域时,我确实注意到mapView中的viewForAnnotation根据需要被调用,并且我看到我的开关相应地执行了,但是针脚颜色并不总是正确的。初始显示映射中的所有图钉每次都是正确的,因此我一直坚持使用这些外部区域的图钉,以了解它们为何断开。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。