使用MapKit添加引脚到地图 – Swift 3.0

新的编码器,试图找出如何使用MapKit。 目标是创build一个地图,用户可以添加引脚来使用他们的地址。 然而,我现在所做的一步,我很难弄清楚如何将地图添加到地图中。

我怎样才能把地图添加到地图上? 到目前为止,我一直在努力弄清楚如何使用注释。

这就是我希望得到的帮助/指导。 谢谢!

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) } } 

他们在MapKit中被称为annotations ,并且像这样实例化它们:

let annotation = MKPointAnnotation()

然后在viewDidLoad()方法中设置坐标并将它们添加到地图中,如下所示:

 annotation.coordinate = CLLocationCoordinate2D(latitude: 11.12, longitude: 12.11) mapView.addAnnotation(annotation) 

数字是你的坐标