【问题标题】:Adding Pins to Map using MapKit - Swift 3.0使用 MapKit 向地图添加引脚 - Swift 3.0
【发布时间】:2017-03-12 22:25:36
【问题描述】:

新编码员,试图弄清楚如何使用 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)
    }
}

【问题讨论】:

    标签: ios swift mapkit mapkitannotation


    【解决方案1】:

    它们在MapKit 中被称为annotations,您应该像这样实例化它们:

    let annotation = MKPointAnnotation() 
    

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

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

    数字是你的坐标

    【讨论】:

    • 米斯拉夫,感谢您的帮助!这正是我所需要的。
    • 我已使用您的代码添加了注释,但该注释在地图中不可见???
    • 他们因此没有出现:stackoverflow.com/questions/39469529/…
    【解决方案2】:

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

    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 
      

    这是我唯一能看到你有坐标的地方 如果一切都失败了,只需添加坐标(如果你在谷歌上搜索
    需要知道如何创建坐标)并将其设置为等于
    “annotation.coordinate”变量

    map.addAnnotation(annotation)
    
    1. 混蛋!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多