Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Pins to Map using MapKit - Swift 3.0

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)     } } 
like image 756
roguephillips Avatar asked Oct 30 '16 18:10

roguephillips


People also ask

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.

How to place a pin in mapkit?

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:

How do I create a map in Swift?

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

How to load a create pins with notes on the map?

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.

How to add a new location to the map?

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.


1 Answers

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

like image 117
Mislav Javor Avatar answered Sep 19 '22 16:09

Mislav Javor