CLLocation 管理器使用默认位置而不更新实际位置
CLLocation Manager using default location and not updating with actual location
所以我不知道发生了什么。我的代码运行得非常好,然后我在故事板中工作了一会儿,突然我开始收到错误消息,即我的 CLLocation 在解包时始终为 nil。我做了我能想到的或在线阅读的所有内容。
- 已添加到 .plist
- 在不同的地方复制代码
- 将 didUpdateLocations 移动到 viewDidLoad
如果我将方案更新为具有默认位置,那么它只会一直使用默认位置,并且永远不会更新我的当前位置,当我在实际设备上尝试时也是如此,但如果我将其留给 none 然后调用错误 didFailWithError
的委托方法说返回的值为 nil。我在这里失去了它。请帮助。
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("In the location, seems we got some shit --> didFailWithError: \(error.description)")
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue : CLLocationCoordinate2D = locationManager.location!.coordinate
let geoFire = GeoFire(firebaseRef: self.geofireRef.child("Posts"))
let center = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
let circleQuery = geoFire.queryAtLocation(center, withRadius: 10)
circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
print("Key '\(key)' entered the search area and is at location '\(location)'")
})
self.locationManager.stopUpdatingLocation()
}
运行 下面的代码:
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
var locationManager = CLLocationManager()
var mapView = MKMapView()
var screenSize = UIScreen.mainScreen().bounds
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
mapView.showsUserLocation = true
mapView.delegate = self
view.addSubview(mapView)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("In the location, seems we got some error \(error.description)")
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue : CLLocationCoordinate2D = locationManager.location!.coordinate
let center = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
let region = MKCoordinateRegionMakeWithDistance(locValue, 2000, 2000)
mapView.setRegion(region, animated: true)
mapView.centerCoordinate = locValue
print(center)
}
}
当然在Info.plist
中设置要求并在调试菜单部分启用位置模拟器:
给我以下输出:
<+37.33756603,-122.04120235> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/7/16, 11:27:25 PM Central European Summer Time
<+37.33756603,-122.04120235> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/7/16, 11:27:26 PM Central European Summer Time
所以我不知道发生了什么。我的代码运行得非常好,然后我在故事板中工作了一会儿,突然我开始收到错误消息,即我的 CLLocation 在解包时始终为 nil。我做了我能想到的或在线阅读的所有内容。
- 已添加到 .plist
- 在不同的地方复制代码
- 将 didUpdateLocations 移动到 viewDidLoad
如果我将方案更新为具有默认位置,那么它只会一直使用默认位置,并且永远不会更新我的当前位置,当我在实际设备上尝试时也是如此,但如果我将其留给 none 然后调用错误 didFailWithError
的委托方法说返回的值为 nil。我在这里失去了它。请帮助。
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("In the location, seems we got some shit --> didFailWithError: \(error.description)")
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue : CLLocationCoordinate2D = locationManager.location!.coordinate
let geoFire = GeoFire(firebaseRef: self.geofireRef.child("Posts"))
let center = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
let circleQuery = geoFire.queryAtLocation(center, withRadius: 10)
circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
print("Key '\(key)' entered the search area and is at location '\(location)'")
})
self.locationManager.stopUpdatingLocation()
}
运行 下面的代码:
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
var locationManager = CLLocationManager()
var mapView = MKMapView()
var screenSize = UIScreen.mainScreen().bounds
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
mapView.showsUserLocation = true
mapView.delegate = self
view.addSubview(mapView)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("In the location, seems we got some error \(error.description)")
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue : CLLocationCoordinate2D = locationManager.location!.coordinate
let center = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
let region = MKCoordinateRegionMakeWithDistance(locValue, 2000, 2000)
mapView.setRegion(region, animated: true)
mapView.centerCoordinate = locValue
print(center)
}
}
当然在Info.plist
中设置要求并在调试菜单部分启用位置模拟器:
给我以下输出:
<+37.33756603,-122.04120235> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/7/16, 11:27:25 PM Central European Summer Time
<+37.33756603,-122.04120235> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/7/16, 11:27:26 PM Central European Summer Time