CLLocationManager 问题

CLLocationManager issue

我正在学习 swift 中的用户本地化。我正在尝试在控制台上打印本地化信息(稍后我会将其用于标签上的信息,所以我想检查它是否有效),但我不知道为什么它什么都不打印。即使删除对字符串的转换,只留下打印任何东西,它仍然不起作用。请帮忙。

是的,我添加了 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager = CLLocationManager()
    var myPosition = CLLocationCoordinate2D()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {

        print("Got location: \(newLocation.coordinate.latitude), \(newLocation.coordinate.longitude)")

        myPosition = newLocation.coordinate

        locationManager.stopUpdatingLocation()
    }

}

didUpdateToLocationCLLocationManagerDelegate 的过时方法。它在 10.6 及更早版本中可用。相反,使用 didUpdateLocations,这将 return 所有位置对象的数组,按最近的时间顺序排列。然后,您访问最新的最新位置,获取 returned 数组中的最后一个对象。

所以试试这个

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

          var latestLocation: CLLocation = locations.last;
         print("Got location: \(latestLocation.coordinate.latitude), \(latestLocation.coordinate.longitude)")

    }

告诉我进展如何。