- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我不知道发生了什么。我的代码运行得非常好,然后我在 Storyboard中工作了一会儿,突然我开始收到错误消息,即我的 CLLocation 在解包时始终为 nil。我做了我能想到的或在网上阅读的所有内容。
如果我将方案更新为具有默认位置,那么它只会一直使用默认位置,并且永远不会更新我的当前位置,当我在实际设备上尝试时也是如此,但如果我将其保留为无,则代表调用错误 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
关于ios - CLLocation 管理器使用默认位置而不更新实际位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38817718/
这个问题在这里已经有了答案: How to convert CLLocation to string in Xcode (3 个答案) 关闭 6 年前。 我有一个包含 String 格式的位置的变量
我正在尝试将位置检测添加到应用程序中,并且我已经弄明白了大部分内容。但是我在使用 reverseGeocodeLocation 方法时遇到了问题。我收到错误 cannot convert value
我尝试在 map View 上显示用户当前位置,但在位置管理器功能的第一行出现错误 这是我的代码 import UIKit import MapKit class FirstViewControlle
我的 CLLocation 遇到问题。 我想知道两个坐标之间的距离,以便将其显示在 tableView 中位置标题旁边: static NSString *CellIdentifier = @
如何在后台保持 CLLocation 更新。我相信您需要注册应用程序才能在应用程序委托(delegate)中执行此操作,但我在任何地方都找不到对此的引用? 最佳答案 以下是相关文档的链接: https
我在CLLocation distanceFromLocation上遇到了一些问题。我下面的代码经过格式化,可以帮助调试,显式分配两个新位置,等等。无论哪种方式,两个几乎相同的位置都会产生巨大的差距,
据我所知,最大值和最小值是: 纬度: [-90, 90] 经度: [-180, 180] CLLocation 仍然接受超出这些限制的值,例如: [[CLLocation alloc] initWi
我是 Swift 的新手(以及这个网站,如果我做错了什么,很抱歉),我正在尝试制作一个正在运行的应用程序来跟踪用户的位置。虽然我用来跟踪距离的函数有效,但它并不是从 0 开始。当我点击开始按钮时,距离
在 iPhone 6+、IOS 11.4、Xcode 9.4.1 上,CLLocation 时间戳始终为零。 纬度:30.598748经度:-97.820877海拔高度:308.921658H准确度:
我试图在内存中保存 self.mapView.userLocation.location在CLLocation属性,但在我将值分配给属性并检查属性的值后,我得到了这个: +/- 0.00m (spe
我已经设法使用 GLGeocoder geocodeAddressString 获取了 2 个城市的坐标 从名称中检索到这 2 个城市的坐标后,我想计算它们之间的距离。但是我面临一个问题,因为 CLL
我正在尝试设置一个自定义类的 CLLocation 属性(名为 objectLocation),使用下面的代码,从我的主 ViewController 调用。不幸的是,我在“注释”行收到一条错误消息,
更新到 iOS 8 后我遇到了很多麻烦。以下代码在我更新到 iOS 和 Xcode 6 之前有效。代码是在 viewDidLoad 中编写的: self.locationManager.delegat
我的类 ViewController 中有这段代码: CLLocationManager *locationManager; - (void)viewDidLoad { [super view
我的代码中有一个奇怪的问题。我想调用 [locationManager startUpdatingLocation]; 并且当更新完成时 - (void)locationManager:(CLLoca
所以我不知道发生了什么。我的代码运行得非常好,然后我在 Storyboard中工作了一会儿,突然我开始收到错误消息,即我的 CLLocation 在解包时始终为 nil。我做了我能想到的或在网上阅读的
info.plist 文件: NSLocationWhenInUseUsageDescription This project would like to know your location NSL
我想在应用程序启动之间保留 2 个 CLLocation。这是我希望在两次启动之间存储在内存中的唯一数据。我该怎么做呢? CLLocation 是否有 writetofile 或将其存储在 cored
这是我的位置管理器委托(delegate)代码。 当我们使用汽车移动时,它不会给出速度,所以至少速度值应该改变。 它总是给出常量值 -1.00。 -(void)locationManager:(CLL
我想使用可靠的位置,即使是在旧的 iPhone 上。然而,许多读数(尤其是来自手机信号塔的读数)都太不准确了。我想。 当我绘制我的位置 + 精度半径(或查看谷歌地图应用程序)时,我注意到估计圆的中心通
我是一名优秀的程序员,十分优秀!