我正在开发一款需要获取用户当前坐标的应用。我计划通过CLLocationManager's didUpdateLocations
方法执行此操作。出于某种原因,didUpdateLocations
未被执行。但是,似乎locationManager.startUpdatingLocation()
被成功调用。我在本网站上看到的其他可能的解决方案都没有为我工作。我已将NSLocationAlwaysUsage
添加到info.plist。以下是我的全部代码:
import UIKit
import MapKit
import CoreLocation
var region: MKCoordinateRegion!
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse, .authorizedAlways:
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
print("Updating location now")
}
case .notDetermined:
locationManager.requestAlwaysAuthorization()
case .restricted, .denied:
print("User must enable access in settings")
break
}
if (region == nil){
}
else {
map.setRegion(region!, animated: true)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Got location")
let userLocation:CLLocation = locations[0]
let lat:CLLocationDegrees = userLocation.coordinate.latitude
let long:CLLocationDegrees = userLocation.coordinate.longitude
let currentPos:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
didUpdateRegion(position: currentPos)
print(lat)
print(long)
}
func didUpdateRegion(position: CLLocationCoordinate2D) {
let span = MKCoordinateSpanMake(0.075, 0.075)
region = MKCoordinateRegion(center: position, span: span)
}
func locationManager(_manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
// If status has not yet been determied, ask for authorization
manager.requestWhenInUseAuthorization()
break
case .authorizedWhenInUse:
// If authorized when in use
manager.startUpdatingLocation()
break
case .authorizedAlways:
// If always authorized
manager.startUpdatingLocation()
break
case .restricted:
print("User must activate location services in settings")
break
case .denied:
print("User must activate location services in settings")
break
default:
break
}
}
当我在模拟器和实际设备上运行此代码时,我会收到允许位置跟踪的通知。接受后,控制台显示"立即更新位置,"但从来没有打印过#34;有位置。"感谢您对此问题的任何启发,我对应用程序开发一般都是新手。
编辑:我在整个代码中添加了代码,而不仅仅是我认为相关的部分。基本上,我试图让地图上显示的区域跟随用户。我尝试通过更新变量" region"每当didUpdateLocations
函数触发时。
答案 0 :(得分:1)
我是否正确并且您只添加了一个密钥 - NSLocationAlwaysUsage? 尝试将两个密钥添加到Info.plist:
此外,如果您实施此协议方法会发生什么?
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("\(error.localizedDescription)")
}
是否可以打印任何内容?对不起,我打算发表评论,但我没有足够的声誉。