使用MapKit将Swins添加到Map中 - Swift 3.0

rog*_*ips 24 mapkit ios mapkitannotation swift

新编码器,试图弄清楚如何使用MapKit.目标是创建一个地图,用户可以使用他们的地址添加引脚.但是,我现在的步骤,我很难搞清楚如何将地图添加到地图.

如何在地图上添加图钉?到目前为止,我一直在努力弄清楚如何使用注释.

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

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

Mis*_*vor 62

它们annotations在MapKit中被调用,你将它们实例化为:

let annotation = MKPointAnnotation()

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

annotation.coordinate = CLLocationCoordinate2D(latitude: 11.12, longitude: 12.11)
mapView.addAnnotation(annotation)
Run Code Online (Sandbox Code Playgroud)

数字是你的坐标

  • 我使用你的代码添加了注释,但注释在地图中不可见??? (6认同)

小智 15

我学会了如何做到这一点的方式是:

  1. 在与viewDidLoad()相同的函数中,放置以下代码行:

     let annotation = MKPointAnnotation()
     annotation.title = "Your text here"
     //You can also add a subtitle that displays under the annotation such as
     annotation.subtitle = "One day I'll go here..."
     annotation.coordinate = center 
    
    Run Code Online (Sandbox Code Playgroud)

如果所有其他方法都失败,这是我唯一可以看到坐标的地方,只需添加坐标(如果您
需要知道如何创建坐标,请在Google上搜索)并将其设置为等于
"annotation.coordinate"变量

map.addAnnotation(annotation)
Run Code Online (Sandbox Code Playgroud)
  1. Dassit!