# How to change a pin color in swift 3 using map kit

**URL:** <https://forums.kodeco.com/t/how-to-change-a-pin-color-in-swift-3-using-map-kit/25454>\
**Category:** Swift\
**Created:** [January 22, 2017, 12:01am UTC](https://forums.kodeco.com/t/how-to-change-a-pin-color-in-swift-3-using-map-kit/25454 "2017-01-22T00:01:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [January 22, 2017, 12:01am UTC](https://forums.kodeco.com/t/how-to-change-a-pin-color-in-swift-3-using-map-kit/25454/1 "2017-01-22T00:01:11Z")

</div>

Below is my code that lists 1 annotation pin. Right now its red. How can I make it blue? Do I need to put in a extension file? The code also includes the current users location.

```
 import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet var map: MKMapView!

 let manager = CLLocationManager()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(1.0, 1.0)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)

map.setRegion(region, animated: true)

print(location.altitude)
print(location.speed)

self.map.showsUserLocation = true

 }

override func viewDidLoad() {
super.viewDidLoad()

manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()

let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(37.5656, -122.443)
let annoation = MKPointAnnotation()

       annoation.coordinate = location
annoation.title = "MYSHOP"
annoation.subtitle = "Come Visit"
map.addAnnotation(annoation)

}

 }

```

---

<div class="post-metadata">

**Author:** ![aeberbach](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aeberbach/32/81494_2.png) [@aeberbach](https://forums.kodeco.com/u/aeberbach)\
**Post date:** [January 23, 2017, 12:06am UTC](https://forums.kodeco.com/t/how-to-change-a-pin-color-in-swift-3-using-map-kit/25454/2 "2017-01-23T00:06:20Z")

</div>

If you set your view controller to be MKMapViewDelegate you will get a call to `viewForAnnotation` - at that time you get the chance to return a view to be used by the pin, and you can create the view with `MKPinAnnotationView` - use the `pinTintColor` member to set the colour, and you can use the standard colors red/green/purple with the class funcs provided.

In viewForAnnotation be sure to leave the user location marker alone… return nil if the annotation received is a MKUserLocation.

---

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [January 23, 2017, 5:14am UTC](https://forums.kodeco.com/t/how-to-change-a-pin-color-in-swift-3-using-map-kit/25454/3 "2017-01-23T05:14:48Z")

</div>

This is what i have now still not working. Notice the comment lines for color.

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

```

---

<div class="post-metadata">

**Author:** ![aeberbach](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aeberbach/32/81494_2.png) [@aeberbach](https://forums.kodeco.com/u/aeberbach)\
**Post date:** [January 23, 2017, 10:40pm UTC](https://forums.kodeco.com/t/how-to-change-a-pin-color-in-swift-3-using-map-kit/25454/4 "2017-01-23T22:40:43Z")

</div>

I see some issues there but I would need to step into that with a debugger to figure out exactly what is going on. I suspect that you do not reach the point where you assign pinTintColor… you need to single step and find out exactly why that is. And I suggest just setting the color with a simple `purplePinColor()` at first, walk before running 🙂
