ios - 使用 MapKit - Swift 3.0 将图钉添加到 map

标签 ios swift mapkit mapkitannotation

新程序员,试图弄清楚如何使用 MapKit。目标是创建一个 map ,用户可以使用他们的地址添加图钉。然而,就我现在所处的步骤而言,我根本无法弄清楚如何向 map 添加图钉。

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

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

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() 方法中设置坐标并将它们添加到 map 中,如下所示:

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

数字是你的坐标

关于ios - 使用 MapKit - Swift 3.0 将图钉添加到 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40331986/

相关文章:

html - iPhone 在 Safari 中不显示字体

swift - CGFloat 没有转换为 UInt32?

swift - 删除以前的引脚并更新坐标

IOS:放大两个标注点的mapkit

ios - 去抖属性包装器

iphone - 如何在 iOS6 中制作与 iOS5 相同的 map

ios - 如何使用静态自身实例将模型类保存在 UserDefaults 中?

xcode - Swift XCTest 加载文件

ios - 在 Alamofire 中,等待一组调用完成的正确方法是什么?

ios - MapKit 自定义引脚注释