简体   繁体   English

尝试在代码中更改MKAnnotation颜色?

[英]Attempting to change MKAnnotation color within my code?

So I have set up my MKAnnotations in MapView and it's working as I want it to. 因此,我已经在MapView中设置了我的MKAnnotations ,它可以按我的意愿工作。 Now I would like to experiment further and try to change the colors of the pins, what would be the most efficient way to implement this within the following code: 现在,我想进一步尝试并尝试更改引脚的颜色,这是在以下代码中实现此功能的最有效方法:

override func viewDidAppear(animated: Bool) {
var annotationQuery = PFQuery(className: "Post")
currentLoc = PFGeoPoint(location: MapViewLocationManager.location)
//annotationQuery.whereKey("Location", nearGeoPoint: currentLoc, withinMiles: 10)
annotationQuery.whereKeyExists("Location")
annotationQuery.findObjectsInBackgroundWithBlock {
    (points, error) -> Void in
    if error == nil {
        // The find succeeded.
        println("Successful query for annotations")
        // Do something with the found objects

        let myPosts = points as! [PFObject]

        for post in myPosts {
            let point = post["Location"] as! PFGeoPoint
            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
            annotation.title = post["title"] as! String!
            annotation.subtitle = post["username"] as! String!

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

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

                            println("Pinview was nil")
                            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
                            pinView!.canShowCallout = true
                            pinView!.animatesDrop = true
                            pinView!.pinColor = .Green
                        return pinView
                }


            self.mapView.addAnnotation(annotation)
        }

    } else {
        // Log details of the failure
        println("Error: \(error)")
    }
}

I can't seem to tackle this on my own even though it seem's simple enough. 即使看起来很简单,我似乎也无法独自解决。 I am also confused about the viewForAnnotation method, from what I could gather it seems like i need to use it, but nothing i'm trying is working. 我也对viewForAnnotation方法感到困惑,从我可以收集的信息来看,好像我需要使用它,但是我正在尝试的任何方法都没有用。

I know this is a late answer but for anyone else searching, check out the tutorial here which shows exactly how to colour pins 我知道这是一个较晚的答案,但对于其他进行搜索的人,请查看此处的教程,该教程确切显示了如何给图钉上色

Ray Wenderlich MapView 雷·温德利希MapView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM