我在斯威夫特学习关于用户本地化的知识。我试图在控制台上打印本地化(稍后我将使用它作为标签上的信息,所以我想检查它是否有效),但我不知道为什么它什么也不打印。即使删除到字符串的转换,并只留下打印任何东西,它仍然不工作。请帮帮忙。
是的,我添加了NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription。
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()
}
}发布于 2016-09-04 19:16:23
didUpdateToLocation是CLLocationManagerDelegate过时的方法。它在10.6版及更早版本中提供。相反,使用didUpdateLocations,它将按最近的时间顺序返回所有location对象的数组。然后访问最新的位置,获取返回数组中的最后一个对象。
所以试试这个
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var latestLocation: CLLocation = locations.last;
print("Got location: \(latestLocation.coordinate.latitude), \(latestLocation.coordinate.longitude)")
}告诉我事情进展如何。
https://stackoverflow.com/questions/39320233
复制相似问题