Luke Noel Luke Noel - 2 months ago 12
Swift Question

Annotations not being placed on map

I'm trying to place an annotation (pin) on the map, and the pin is not showing on the simulator (there is no pin placed). I receive no errors in the code. Below is my viewDidLoad.

import UIKit
import MapKit

class MapViewController: UIViewController {

var itemStore: ItemStore!
var mapView: MKMapView!

override func viewDidLoad() {
super.viewDidLoad()

mapView = MKMapView()

let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 40.71304, longitude: -74.0072)
annotation.title = "Test"
mapView.addAnnotation(annotation)
}

Answer Source

Remove mapView = MKMapView(). This line creates a new MKMapView instance, while what you want to do is use the one that you created on your Storyboard. If you are not using a Storyboard, than you should use another initializer of MKMapView that sets its size as well.