Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Add MKAnnotationView To MKMapView

Tags:

People also ask

What is MKAnnotationView?

The visual representation of one of your annotation objects.

How do I use MapKit in swift 5?

Go to the storyboard. Drag a MapKit View to the main View. Give the MapKit View the same size as the main View. Select the MapKit View and go to the Pin button from the Auto Layout button on the bottom-right of the Storyboard and fill in the following values.


I'm trying to add MKAnnotationView to MKMapView but I can't do it… Can anyone help me?

Here is my code:

override func viewDidLoad() {     super.viewDidLoad()     locationManager.desiredAccuracy = kCLLocationAccuracyBest     locationManager.startUpdatingLocation()     var latitude:CLLocationDegrees = locationManager.location.coordinate.latitude     var longitude:CLLocationDegrees = locationManager.location.coordinate.longitude     var homeLati: CLLocationDegrees = 40.01540192     var homeLong: CLLocationDegrees = 20.87901079     var latDelta:CLLocationDegrees = 0.01     var longDelta:CLLocationDegrees = 0.01     var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)     var myHome:CLLocationCoordinate2D = CLLocationCoordinate2DMake(homeLati, homeLong)     var myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)     var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, theSpan)     self.theMapView.setRegion(theRegion, animated: true)     self.theMapView.mapType = MKMapType.Hybrid     self.theMapView.showsUserLocation = true     ///Red Pin     var myHomePin = MKPointAnnotation()     myHomePin.coordinate = myHome     myHomePin.title = "Home"     myHomePin.subtitle = "Bogdan's home"     self.theMapView.addAnnotation(myHomePin)      var anView:MKAnnotationView = MKAnnotationView()     anView.annotation = myHomePin     anView.image = UIImage(named:"xaxas")     anView.canShowCallout = true     anView.enabled = true }