Swift / CoreLocation:来自didUpdateLocation

时间:2017-01-31 09:45:58

标签: swift cllocationmanager location-services cllocationdistance

是从didUpdateLocation方法内部进行标题的方法吗?我正在努力解决将有关用户位置的数据发送到名为Ping的容器类的任务。 Ping类必须包含基本用户位置以及north heading

  • 是否可以通过iOS中的代码启用罗盘?

我的位置服务在设备的timedegree turn期后保存数据或distance traveled - 如果我们转得更快或我们走了一段距离,那么计时器将重新启动。

我还没有在物理设备上测试它(只有模拟器)所以我无法看到如果我计算好的行进距离和角度。

import Foundation
import CoreLocation
import CoreMotion
import UIKit

class LocationService: NSObject, CLLocationManagerDelegate {

    var _locationManager: CLLocationManager!
    var _motionManager: CMMotionManager!
    var _timer = Timer()
    var _heading: Double!
    var _accuracy: Double!
    var _latitude: Double!
    var _longitude: Double!
    var _velocity: Double!
    var _startLocation: CLLocation!
    var _lastLocation: CLLocation!
    var _traveledDistance: Double = 0
    var _seconds = 0
    var _deferringUpdates: Bool = false

    static let _instance : LocationService = LocationService()

    public static var Instance : LocationService {
        get{ return _instance}
    }

    public func InitLocationManager() {

        _locationManager = CLLocationManager()
        _locationManager.delegate = self
        _locationManager.distanceFilter = 1000
        _locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        _locationManager.requestAlwaysAuthorization()
        _locationManager.startUpdatingLocation()
        if CLLocationManager.headingAvailable() {

            _locationManager.headingFilter = 5
            _locationManager.startUpdatingHeading()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateHeading _newHeading: CLHeading) {

        if _newHeading.headingAccuracy < 0 {
            return
        }

        let _theHeading : CLLocationDirection = _newHeading.trueHeading > 0 ? _newHeading.trueHeading : _newHeading.magneticHeading

        self._heading = _theHeading
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations _locations: [CLLocation]) {

        let _location = _locations[0]

        self._accuracy = _location.horizontalAccuracy
        self._latitude = _location.coordinate.latitude
        self._longitude = _location.coordinate.longitude
        self._velocity = _location.speed

        _timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(UpdateTimer), userInfo: nil, repeats: true)

        if _startLocation == nil {
            _startLocation = _locations.first
        } else {
            if let _lastLocation = _locations.last {
                let _distance = _startLocation.distance(from: _lastLocation)
                let _lastDistance = _lastLocation.distance(from: _lastLocation)
                _traveledDistance += _lastDistance

                if _traveledDistance <= 100 {
                    PostNotificationWithLocation()
                    _traveledDistance = 0
                    _timer.invalidate()
                }
            }
        }
        _lastLocation = _locations.last

        _motionManager = CMMotionManager()
        if _motionManager?.isDeviceMotionAvailable == true {
            _motionManager?.deviceMotionUpdateInterval = 0.1

            let queue = OperationQueue()

            _motionManager?.startDeviceMotionUpdates(to: queue, withHandler: { (_motion, _error) in
                if let attitude = _motion?.attitude {
                    if(attitude.pitch == 30/M_PI) {
                        self.PostNotificationWithLocation()
                        self._timer.invalidate()
                    }
                }
            })
        }

        _locationManager.stopUpdatingLocation()
    }

    func PostNotificationWithLocation() -> Void {
         NotificationCenter.default.post(name: LOCATION_NOTIFICATION, object: nil, userInfo: ["latitude":_latitude, "longitude":_longitude, "velocity":_velocity, "traveledDistance":_traveledDistance])
    }

    func UpdateTimer() {
        _seconds += 1
        if _seconds == 10 {
            PostNotificationWithLocation()
            _timer.invalidate()
            _seconds = 0
        }
    }

}

提前致谢!!

0 个答案:

没有答案