import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {
   @IBOutlet weak var mapView: MKMapView!

   override func viewDidLoad() {
      super.viewDidLoad()
      let location = CLLocation(latitude: 40.7637825011971, longitude: -73.9731328627541)
      let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)
      mapView.setRegion(region, animated: false)
        
      let annotation = MyAnnotation(coordinate: location.coordinate)
      annotation.title = "Apple Store"
      annotation.picture = UIImage(named: "appstore")
      mapView.addAnnotation(annotation)
        
      mapView.delegate = self
   }
   func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
      if let temp = annotation as? MyAnnotation {
         var aView = mapView.dequeueReusableAnnotationView(withIdentifier: "Pins")
         if aView == nil {
            aView = MKAnnotationView(annotation: temp, reuseIdentifier: "Pins")
            aView?.image = UIImage(named: "iconmap")
            aView?.canShowCallout = true
                
            let leftImage = UIImageView(image: temp.picture)
            aView?.leftCalloutAccessoryView = leftImage
         } else {
            aView?.annotation = annotation
         }
         return aView
      }
      return nil
   }
}