New coder, trying to figure out how to use MapKit. The goal is to create a map that users can add pins to using their address. However, the step I am at now, I am having trouble figuring out how to add pins to the map at all.
How can I add a pin to the map? I have been struggling to figure out how to use annotations thus far.
That's what I'm hoping for help/direction with. Thanks!
import UIKit import MapKit import CoreLocation class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { @IBOutlet weak var bigMap: MKMapView! let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() self.locationManager.delegate = self self.locationManager.desiredAccuracy = kCLLocationAccuracyBest self.locationManager.requestWhenInUseAuthorization() self.locationManager.startUpdatingLocation() self.bigMap.showsUserLocation = true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let location = locations.last let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)) self.bigMap.setRegion(region, animated: true) self.locationManager.stopUpdatingLocation() } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("Errors " + error.localizedDescription) } }
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.
For placing a pin in MapKit we have 3 ways Using MKPlacemark. Using MKPointAnnotation. Using MKAnnotation. 1. Using MKPlacemark: MKPlacemark is the simplest way to place a pin on the map. It just required coordinates of the place to where to set pin. Use this function to set pin: 2. Using MKPointAnnotation:
First just select the Swift file that will show the map in your project, for our case its the ContentView.swift file. Then just simply navigate to Debug -> Simulate Location -> Select a location
We can use it to load a create pins with notes on our map! Creating a MapAnnotation for your Map requires two parts. First is the array of locations that you want to add to the Map, second is the design or content of the MapAnnotation. First step is to create a structure ( struct) that will be used as the basic skeleton of our map locations.
First is the array of locations that you want to add to the Map, second is the design or content of the MapAnnotation. First step is to create a structure ( struct) that will be used as the basic skeleton of our map locations.
They're called annotations
in the MapKit
and you should instantiate them like so:
let annotation = MKPointAnnotation()
then in the viewDidLoad()
method just set the coordinates and add them to the map like:
annotation.coordinate = CLLocationCoordinate2D(latitude: 11.12, longitude: 12.11) mapView.addAnnotation(annotation)
The numbers are your coordinates
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With