试图在我的代码中更改MKAnnotation颜色?

时间:2015-04-25 00:38:21

标签: ios swift annotations mapkit xcode6.3

所以我在MapView中设置了MKAnnotations,它正如我想要的那样工作。现在我想进一步尝试并尝试更改引脚的颜色,这将是在以下代码中实现此功能的最有效方法:

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)")
    }
}

我似乎无法自己解决这个问题,尽管看起来很简单。我也对viewForAnnotation方法感到困惑,从我可以收集的方法来看,似乎我需要使用它,但我正在尝试的任何工作都没有。

1 个答案:

答案 0 :(得分:-1)

我知道这是一个迟到的答案,但对于其他任何人来说,请查看此处的教程,其中详细说明了如何着色针

Ray Wenderlich MapView