3

This is what i have now still not working. Notice the comment lines for color. I am trying to make make the Olso pin the standard red. But I would like to change the color of the london pin so its not red. So 2 different pins would have different colors.

VIEWCONTROLLER

 import MapKit
 import UIKit

class ViewController: UIViewController, MKMapViewDelegate {
  @IBOutlet weak var mapView: MKMapView!
 //color
let annotation = ColorPointAnnotation(pinColor: UIColor.blueColor())

 override func viewDidLoad() {
super.viewDidLoad()

let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.")
let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.")



 mapView.addAnnotations([london, oslo])

 }


  func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
   // 1
  let identifier = "Capital"

// 2
if annotation is Capital {


    // 3

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        if annotationView == nil {
        //4
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView!.canShowCallout = true
        //color
          let colorPointAnnotation = annotation as! ColorPointAnnotation
        annotationView?.pinTintColor = colorPointAnnotation.pinColor

        // 5
        let btn = UIButton(type: .detailDisclosure)
        annotationView!.rightCalloutAccessoryView = btn
    } else {
        // 6
        annotationView!.annotation = annotation

    }

    return annotationView
}

// 7
return nil

    }

   func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let capital = view.annotation as! Capital
let placeName = capital.title
let placeInfo = capital.info


let ac = UIAlertController(title: placeName, message: placeInfo, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
 }
 }

CAPITAL

import MapKit
import UIKit

class Capital: NSObject, MKAnnotation {
var title: String?
var coordinate: CLLocationCoordinate2D
var info: String

init(title: String, coordinate: CLLocationCoordinate2D, info: String)           {
    self.title = title
    self.coordinate = coordinate
    self.info = info



  }
  }
  • You haven't provided enough information. What is your code doing now? It's making Oslo red, but it's also making London red and you don't want that? What is the definition of your ColorPointAnnotation class? Why are you an instance variable "annotation" that contains a single ColorPointAnnotation? That makes your code confusing and error-prone, since your mapView(!_: annotation:) method has a parameter called annotation and you have an instance var of the same name. – Duncan C Jan 23 '17 at 22:45
  • @DuncanC I would like Oslo to remain red but turn London to Green in a pin color. Thats all I would like to do. –  Jan 23 '17 at 22:47
  • Ok, what about the rest of my questions? What do your annotation objects look like? Do they have custom color values for each city? Are they all of type Capital? – Duncan C Jan 23 '17 at 22:52
  • they are all of type capital. They do not have custom color views. The annotation objects are just the standard map kit pins. –  Jan 23 '17 at 23:57
  • No, there are 2 different pieces to this. You have to add annotations to the map (Objects that conform to the MKAnnotation protocol. Yours apparently are type `ColorPointAnnotation`.) Then when the map needs to display an annotation, it calls your `mapView(!_:viewFor:)` method. That passes you the annotation object, and you use that to configure an annotation **view** object. It looks like your `ColorPointAnnotation` class has a property `pinColor`. If you set the color on the annotation object for London to a color other than red then London's pin should use that color. – Duncan C Jan 24 '17 at 00:39
  • i added both of my view controllers "viewcontroller" and "CAPITAL". This is all of the code I have. Could you tell me what to put int he code?. Thanks. @DuncanC –  Jan 24 '17 at 01:20
  • What is the ColorPointAnnotation class? How are you adding annotations to your map? – Duncan C Jan 24 '17 at 01:29
  • annotations are being added in the capital view controller. @DuncanC. I don't necessarily want to use colorpointannotation if necessarily. I just want the pins to be 2 different colors. –  Jan 24 '17 at 01:32

0 Answers0