使用iOS核心位置的准确地图路径

时间:2019-07-05 06:54:32

标签: ios swift google-maps gmsmapview

我正在GMSMapView上绘制用户移动路径。当用户开车或步行时,路径足够准确,但是在建筑物中工作(停留)几个小时时,我得到的位置值非常不准确。请参阅所附的屏幕截图:

enter image description here

以下是我使用的一些代码:

    var isFirstLocationUpdate = true
    var previousLocation: CLLocation?
    let locationManager = CLLocationManager()        

    public func setupLocationSettings()
    {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 50
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.pausesLocationUpdatesAutomatically = false
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        guard let location = locations.last else
        {
            return
        }

        if locationValid(location)
        {
            previousLocation = location
            saveLocation(location)
        }
    }

    func locationValid(_ location: CLLocation) -> Bool
    {
        guard !isFirstLocationUpdate else
        {
            isFirstLocationUpdate = false
            return true
        }

        let age = location.timestamp.timeIntervalSinceNow

        if abs(age) >= 10  //seconds
        {
            return false
        }

        if location.horizontalAccuracy < 0 || location.horizontalAccuracy > 50
        {
            return false
        }

        if let prevLocation = previousLocation
        {
            let distance = location.distance(from: prevLocation)
            let noiseFactor = 0.5 

            if distance >= location.horizontalAccuracy * noiseFactor
            {
                return true
            }
            else
            {
                return false
            }
        }

        return true
    }

我尝试过滤位置数据,但是我仍然得到非常不准确的值。任何建议都欢迎。

0 个答案:

没有答案