gpt4 book ai didi

ios - CLLocation 管理器使用默认位置而不更新实际位置

转载 作者:行者123 更新时间:2023-11-28 16:18:14 25 4
gpt4 key购买 nike

所以我不知道发生了什么。我的代码运行得非常好,然后我在 Storyboard中工作了一会儿,突然我开始收到错误消息,即我的 CLLocation 在解包时始终为 nil。我做了我能想到的或在网上阅读的所有内容。

  • 添加到.plist
  • 在不同的地方复制代码
  • 将 didUpdateLocations 移动到 viewDidLoad 中

如果我将方案更新为具有默认位置,那么它只会一直使用默认位置,并且永远不会更新我的当前位置,当我在实际设备上尝试时也是如此,但如果我将其保留为无,则代表调用错误 didFailWithError 的方法表明返回值为 nil。我在这里失去了它。请帮忙。

enter image description here

enter image description here enter image description here

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 中设置要求,并在调试菜单部分启用位置模拟器:

location simulator

给我以下输出:

<+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

关于ios - CLLocation 管理器使用默认位置而不更新实际位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38817718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com