对于从GPS进行大量计算的应用程序,我需要每0.5秒获得纬度/经度和速度,以便非常准确并避免延迟.
我在用:
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];- locationManager:didUpdateToLocation:fromLocation:函数将值存储在3个变量中:newLatitude,newLongitude和newSpeedNSTimer每0.5秒调用一次函数的对象.请参阅下面的Excel生成的图表,表示18秒内的纬度值:

我们可以清楚地看到我们每秒都有位置更新,而不是每隔0.5秒就有所需.我在办公室里做样品,所以我的速度在0到65英里/小时之间.因此,当我驱动50MPH时,我应该每隔0.5秒从iPhone获得不同的lat/lon/speed值?
如果您对该CLLocationManager对象的准确性有任何了解,请告诉我如何每0.5秒获取一次这些位置更新.
谢谢!
在用户第一次选择"不允许"选项后,调用[CLLocationManager requestAlwaysAuthorization]不会显示警报.无论如何,强制应用程序在需要时再次显示警报?
我正在尝试确定我的用户的位置并且我已经正确设置了所有内容,但是我的设备始终返回 -1 的速度和路线。当我在模拟器上运行完全相同的设置时,速度和课程值都很好。
我已经在我的测试设备上校准了指南针,地图显示了正确的位置。我还重置了位置和隐私设置,并使用了 5 部不同的手机。所有设备上的故事总是相同的。
manager = [[CLLocationManager alloc] init];
self.manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
manager.distanceFilter = 500;
manager.activityType = CLActivityTypeOther;
[manager startUpdatingLocation];
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
for (CLLocation *location in locations) {
//double speed = 0.0;
//speed = location.speed;
//NSLog(@"%f", s);
NSLog(@"%@", location.description);
}
}
Run Code Online (Sandbox Code Playgroud)
这将返回:
2015-05-07 12:16:57.703 Health_[323:84932] Location: (
"<+52.55719238,+11.02049967> +/- 165.00m (speed -1.00 mps / course -1.00) @ 07.05.15 12:16:57 Mitteleurop\U00e4ische Sommerzeit"
)
Run Code Online (Sandbox Code Playgroud)
有谁知道发生了什么?
我有一个 CLLocation 管理器。它工作正常。
locManager = CLLocationManager()
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.requestWhenInUseAuthorization()
locManager.startUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)
当然,我有委托方法:
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
println("didfail")
println(error)
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
println("location manager called..")
let locationLast = locations.last as! CLLocation
latitude = locationLast.coordinate.latitude.description
longitude = locationLast.coordinate.longitude.description
locManager.stopUpdatingLocation()
//....
}
Run Code Online (Sandbox Code Playgroud)
运行没有任何问题!但是,当我在这里 47.5775679 19.0488392(或在此点的 1-2 公里半径内)时,它会引发此错误:
Error Domain=kCLErrorDomain Code=0 “操作无法完成。(kCLErrorDomain 错误 0。)”
我也尝试过模拟器和真实设备。当我尝试时,我在真实设备上有互联网连接。有黑洞吗?:) 或者我不知道。
任何人都可以请给我任何想法,导致这种现象的原因是什么?
谢谢!
我正在使用Parse和geoPointForCurrentLocationInBackground我可以在收到位置后停止更新,而不必手动停止它.
在使用CLLocationManager接收位置后,如何立即停止更新位置?
编辑
我知道[self.locationManager stopUpdatingLocation]; 阻止它.我真正要问的是,我怎么知道我第一次收到位置然后立即停止?
我面临的问题是,当我按下UIButton时 - 需要位置服务来启动操作.但是,如果用户在应用程序的初始启动时拒绝定位服务 - 应用程序将崩溃.
我试图找到一种方法来实现CLAuthorizationStatus .Denied但我似乎无法找到一种方法.我似乎可以实现的唯一代码是didChangeAuthorizationStatus,它只在应用程序的首次启动时启动请求.
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
if status == .AuthorizedAlways || status == .AuthorizedWhenInUse
{
manager.startUpdatingLocation()
}
else
{
manager.requestWhenInUseAuthorization()
}
}
Run Code Online (Sandbox Code Playgroud)
如果我按下UIButton发送API请求,如果位置服务被拒绝,应用程序将崩溃.
我的问题是如何在按钮的IBAction中实现一个方法,该方法将指导用户转到他们的设置并启用位置服务.:)
如何从(userLocation.swift)中的一个类(GPSLocation)获取定位结果,并在文件(target.swift)中的另一个类中使用它们?
我需要以下国家/地区的国家/地区代码,城市,经度和纬度:
userLocation.swift
import UIKit
import MapKit
class GPSLocation {
func getGPSLocation(completion: () -> Void) {
let locManager = CLLocationManager()
var currentLocation: CLLocation!
locManager.desiredAccuracy = kCLLocationAccuracyBest
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {
currentLocation = locManager.location
let latitude = String(format: "%.7f", currentLocation.coordinate.latitude)
let longitude = String(format: "%.7f", currentLocation.coordinate.longitude)
let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
fetchCountryAndCity(location: location) { countryCode, city in
// debugPrint("Country:", countryCode)
// debugPrint("City:", city)
}
// debugPrint("Latitude:", latitude)
// debugPrint("Longitude:", longitude)
}
}
//MARK: Find …Run Code Online (Sandbox Code Playgroud) 我正在尝试将kCLLocationAccuracyHundredMeters 的位置精度用于我的应用程序中的标准位置更新,这似乎在一段时间内工作得相对较好,但每隔一段时间我会在更新之间收到 10 分钟的间隔(在行驶数英里时)不同的方向),这似乎与设置不符。这是我的位置管理器上的设置。此外,我只使用前台位置更新,因此对于这些测试,我一直在屏幕上显示应用程序,而应用程序始终处于前台。
self.locManager = [[CLLocationManager alloc] init];
self.locManager.delegate = self;
self.locManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.locManager.distanceFilter = 15;
Run Code Online (Sandbox Code Playgroud)
我使用相同的其他设置尝试了kCLLocationAccuracyBest,我在整个驱动器中都获得了完美的测量结果,但我不需要那么准确的位置。我认为百米精度仍然会以较低的精度提供相对一致的更新,这就是我们正在寻找的。
这只是使用kCLLocationAccuracyHundredMeters设置与 kCLLocationAccuracyBest 的副作用吗?
在 iPhone 6s 上测试:11.0.2 和 iPhone 7Plus:11.0.3
任何帮助表示赞赏,谢谢!
编辑:
我应该澄清一下,我确实在下面列出的 CLLocationManager 委托方法中设置了日志记录,但从未收到任何回调/日志。
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
- (void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(nullable NSError *)error;
- (void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager;
- (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager;
Run Code Online (Sandbox Code Playgroud) 我正在使用swift4.2并且Xcode 10我正在尝试使iOS应用程序使用位置服务,但它给了我一个例外:Fatal error: Unexpectedly found nil while unwrapping an Optional value
所以我试图检查位置是否返回 nil 所以我复制我的代码并打印位置并返回null,我Xcode从产品>方案>编辑方案>默认位置和调试区域中的检查位置模拟位置,并模拟到位置我选择任何一个知道问题所在吗?
import CoreLocation
class LocationVC: UIViewController,CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var currentlocation:CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
authorizelocationstates()
}
func authorizelocationstates(){
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
currentlocation = locationManager.location
print(currentlocation)
} …Run Code Online (Sandbox Code Playgroud) 标题是不言自明的。我试过viewDidLayoutSubviews()按照这里的建议调用它,但它没有给我带来任何乐趣。
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization()
mapView.mapType = .normal
mapView.settings.zoomGestures = true
mapView.settings.tiltGestures = true
mapView.settings.rotateGestures = true
mapView?.isMyLocationEnabled = true
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
if(locationManager.location != nil){
centerMapOnLocation(location: locationManager.location!)
}
}
func centerMapOnLocation(location: CLLocation)
{
let camera = GMSCameraPosition.camera(withLatitude: locationManager.location!.coordinate.latitude, longitude: locationManager.location!.coordinate.longitude, zoom: zoom)
mapView?.animate(to: camera)
}
Run Code Online (Sandbox Code Playgroud) ios ×8
swift ×5
objective-c ×2
cllocation ×1
geolocation ×1
google-maps ×1
iphone ×1
mapkit ×1