【问题标题】:Adding annotation from current location从当前位置添加注释
【发布时间】:2016-01-18 18:55:48
【问题描述】:

当我点击按钮时,我想从用户位置添加注释。我猜它接近工作了,但是当我点击按钮时,它会在非洲的某个地方添加注释。 (纬度:0,经度:0)。为什么它不会显示正确的用户位置?

导入 UIKit 导入 MapKit 导入核心位置

类 ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

}

@IBAction func addAnnotation(sender: UIBarButtonItem) {

    let addPin = MKPointAnnotation()
    addPin.coordinate = CLLocationCoordinate2D(latitude: self.mapView.userLocation.coordinate.latitude, longitude: self.mapView.userLocation.coordinate.longitude)
    addPin.title = "Hello world"
    mapView.addAnnotation(addPin)

}

}

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

    您遇到的问题是您没有为用户获取位置。您需要在代码中添加一些东西来实现这一点。我已经添加了整个代码,请查看 cmets 以了解说明。

    import CoreLocation
    import MapKit
    import UIKit
    
    class ViewController: UIViewController, CLLocationManagerDelegate {
    
        @IBOutlet var mapView: MKMapView!
        var locationManager = CLLocationManager()
        // Add two variables to store lat and long
        var lat = 0.0
        var long = 0.0
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Check if you have the right to get the location
            self.locationManager.requestWhenInUseAuthorization()
            if CLLocationManager.locationServicesEnabled() {
                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
            }
        }
    
        override func viewWillAppear(animated: Bool) {
            // Start get the location on viewWillAppear
            locationManager.startUpdatingLocation()
        }
    
        @IBAction func addAnnoation(sender: AnyObject) {
            locationManager.startUpdatingLocation()
            let annotation = MKPointAnnotation()
            // Set the annotation by the lat and long variables 
            annotation.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
            annotation.title = "User location"
            self.mapView.addAnnotation(annotation)
        }
    
        // To get the location for the user
        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let location:CLLocationCoordinate2D = manager.location!.coordinate
            lat = location.latitude
            long = location.longitude
        }
    
        func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
            print("Error")
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
    
    
               // Dispose of any resources that can be recreated.
            }
        }
    

    您还需要打开您的info.plist 文件并添加此属性: NSLocationWhenInUseUsageDescription 并添加一个字符串为什么你需要获取用户位置(这是强制性的,否则你将无法获取用户位置)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 2017-04-15
      相关资源
      最近更新 更多