ios - 如何加快在 Swift 中从 GPS 获取坐标位置?

标签 ios swift gps

我是初学者,我正在制作一个获取用户坐标的应用程序。我正在制作如下所示的 locationManager 类

import UIKit
import CoreLocation


class LocationManager: NSObject {
    let manager = CLLocationManager()
    var didGetLocation: ((Coordinate?) -> Void)?

    override init() {
        super.init()

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestLocation()
    }

    func getPermission() {
        // to ask permission to the user by showing an alert (the alert message is available on info.plist)
        if CLLocationManager.authorizationStatus() == .notDetermined {
            manager.requestWhenInUseAuthorization()
        }

    }
}




extension LocationManager : CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            manager.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first else {
            didGetLocation?(nil)
            return

        }
        let coordinate = Coordinate(location: location)
        if let didGetLocation = didGetLocation {
            didGetLocation(coordinate)

        }
    }
}

private extension Coordinate {
    init(location: CLLocation) {
        latitude = location.coordinate.latitude
        longitude = location.coordinate.longitude
    }
}

我只是调用 didGetLocation 属性来获取用户坐标位置。上面的代码实际上可以得到坐标数据。但我认为这需要太多时间(我在 5 - 7 秒后得到了坐标)。

老实说,我是iOS开发的新手,5-7秒左右获取坐标位置正常吗?我可以改进这个吗?

我怀疑是因为我使用的所需精度是kCLLocationAccuracyBest,但如果我更改为kCLLocationAccuracyHundredMeters,它似乎是一样的

那么,我可以改进这个吗?因为如果我与android相比,它获取坐标位置的速度非常快

最佳答案

正如我在评论中所说,如果您使用较小的精度值,假设 kCLLocationAccuracyThreeKilometers 您应该更早获得有效位置,但通常 CLLocationManager 需要一些时间才能获得有效位置,因为大多数位置管理器任务异步运行

Apple 文档对此有何评论

Declaration

var desiredAccuracy: CLLocationAccuracy { get set }

Discussion

The receiver does its best to achieve the requested accuracy; however, the actual accuracy is not guaranteed.

You should assign a value to this property that is appropriate for your usage scenario. For example, if you need the current location only within a kilometer, you should specify kCLLocationAccuracyKilometer and not kCLLocationAccuracyBestForNavigation. Determining a location with greater accuracy requires more time and more power.

When requesting high-accuracy location data, the initial event delivered by the location service may not have the accuracy you requested. The location service delivers the initial event as quickly as possible. It then continues to determine the location with the accuracy you requested and delivers additional events, as necessary, when that data is available.

For iOS and macOS, the default value of this property is kCLLocationAccuracyBest. For watchOS, the default value is kCLLocationAccuracyHundredMeters.

This property is used only in conjunction with the standard location services and is not used when monitoring significant location changes.

关于ios - 如何加快在 Swift 中从 GPS 获取坐标位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49127564/

相关文章:

ios - iOS 9.1 启动屏幕上的黑色方 block

swift - 如何要求在 Swift 协议(protocol)中定义枚举

php - 相同的 MySql 查询执行时间长但存档表短,记录多 600 万条

objective-c - GPS 应用程序在 iOS 5 上停止工作 - 位置管理器未更新

navigation - 使用卡尔曼滤波器集成 GPS 和加速度计数据的伪代码

ios - 使用 Swift 和 Storyboard 在两个 UIViewController 之间传递数据

ios - FB IOS SDK是否提供检查访问 token 有效性的方法?

ios - 从 UITableView 在线保存数据的最佳方法是什么?

android - 处理不同尺寸照片的移动应用程序和服务器的设计

ios - 为什么标签的默认类型是强制展开的可选类型?