Reputation: 65
I'm using Swift 3 and Xcode 10 beta 3 and I need to use a custom image for my pins on the map. I need to avoid the red annotation pins and use some custom logos that I made for this. I tried all the solutions found on stack but nothing helped me. This is my code:
import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
//let span:MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
let locationANTOMI:CLLocationCoordinate2D = CLLocationCoordinate2DMake(45.4509339, 9.1713609)
let locationANTOTO:CLLocationCoordinate2D = CLLocationCoordinate2DMake(45.06666, 7.68826)
//let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
//map.setRegion(region, animated: true)
let annotationANTOMI = MKPointAnnotation()
annotationANTOMI.coordinate = locationANTOMI
annotationANTOMI.title = "ANTONIOLI MILANO"
map.addAnnotation(annotationANTOMI)
let annotationANTOTO = MKPointAnnotation()
annotationANTOTO.coordinate = locationANTOTO
annotationANTOTO.title = "ANTONIOLI TORINO"
map.addAnnotation(annotationANTOTO)
}
}
How should I do it?
Upvotes: 1
Views: 4341
Reputation: 719
The way I usually do it is to create a new swift file that will be your custom annotation which inherits from MKAnnoatation. Example below
import MapKit
class MyAnnotation: NSObject, MKAnnotation {
let title: String?
let subtitle: String?
let coordinate: CLLocationCoordinate2D
var image: UIImage? = nil
init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
//self.image
super.init()
}
}
You will need to initialise your annotation where the CLCoordinate is compulsory. Then set the image property with your custom image. MyAnnotation.image = "myImage.png". You will then need to add your annotation to your map view
mapView.addAnnotations(MyAnnotation).
I also implement the method below from the MKMapViewDelegate (Make sure your inherit this in your class). This is so that the user can tap on the annotation and receive information about it. Hope this helps.
In your view controller:
let marker = MyAnnotation(title: "title" as! String, subtitle: "subtitle" as! String, coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
marker.image = UIImage("my image.png")
self.mapView.addAnnotations(marker)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? MyAnnotation {
let identifier = "identifier"
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.image = annotation.image //add this
annotationView?.canShowCallout = true
annotationView?.calloutOffset = CGPoint(x: -5, y: 5)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
return annotationView
}
return nil
}
Upvotes: 1