所以我不知道发生了什么。我的代码工作得非常好,然后我在故事板上工作了一下,突然我开始得到错误,我的CLLocation在打开时总是为零。我做了我能想到或在线阅读的所有内容。
如果我将方案更新为具有默认位置,那么它只使用默认位置,并且永远不会使用我当前的位置进行更新,当我在实际设备上尝试时也是如此,但是如果我将其保留为无,那么代表调用错误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()
}
答案 0 :(得分:1)
运行以下代码:
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