0

So I have set up my MKAnnotations in MapView and it's working as I want it to. 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.

Zach
  • 180
  • 1
  • 2
  • 12
  • Show the viewForAnnotation code that you tried. Did you confirm that the delegate method was actually getting called (with a breakpoint or NSLog)? See http://stackoverflow.com/questions/24523702/stuck-on-using-mkpinannotationview-within-swift-and-mapkit for a simple example. Remember to set/connect the map view's delegate outlet. –  Apr 25 '15 at 02:21
  • updated the code above with the viewForAnnotation method. The NSLog is not getting called. – Zach Apr 25 '15 at 02:44
  • 1
    The viewForAnnotation method must be outside other methods -- at the top level of the class (same as viewDidAppear). Also, the first parameter may have to be "mapView" not "aMapView". –  Apr 25 '15 at 03:03
  • So this allows the pin colors to change, but I'm running into 2 issues. First, My blue current location beacon is no longer appearing and instead my current location is replaced with a pin and follows my current location around. Secondly, i set the callout to true but don't get an option to segue to a new view when the annotation title/subtitle are tapped. Any advice? – Zach Apr 25 '15 at 05:05

1 Answers1

-1

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

300baud
  • 540
  • 4
  • 17