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