Here is my code. I have tried all the solutions available but I am not able to put image I wish to instead of the default annotation red pin in my code.
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
var location = CLLocationCoordinate2DMake(18.956449, 72.810597)
var span = MKCoordinateSpanMake(0.009, 0.009)
var region = MKCoordinateRegion(center: location, span: span)
map.setRegion(region, animated: true)
var annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Wilson College"
annotation.subtitle = "Commerce College"
map.addAnnotation(annotation)
print("showing on map")
mapView(map, viewForAnnotation: annotation)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"
print("inside viewForAnnotation")
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
// Reuse the annotation if possible
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil
{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView!.canShowCallout = true
annotationView!.image = UIImage(named: "VRtest.png")
annotationView!.rightCalloutAccessoryView = detailButton
print("editing annotation")
}
else
{
annotationView!.annotation = annotation
}
return annotationView
}
Any help is appreciated. thank you