[iOS] CLLoction Manager를 통한 이동 속도(Swift 편)
운영 환경
전제 조건
사용자는 위치 정보를 사용할 수 있어야 합니다.상세한 상황은 아래의 문장을 참고하시오.
iOS 8에서 위치 정보를 얻는 방법이 달라집니다. - Qiita.
설치 방법
CLLocation 클래스
CLLocationSpeed 속성을 사용합니다./*
 *  speed
 *  
 *  Discussion:
 *    Returns the speed of the location in m/s. Negative if speed is invalid.
 */
@available(iOS 2.2, *)
public var speed: CLLocationSpeed { get }
단위는 초속(m/s)이다.속도를 얻을 수 없을 때 마이너스를 되돌려줍니다.이동 거리에서 속도를 계산하기 때문에 정밀도가 높지 않다.
설치 예
실행
startUpdatingLocation.override func viewDidLoad() {
    super.viewDidLoad()
    if CLLocationManager.locationServicesEnabled() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
    }
}
위치 정보를 업데이트할 때마다 다음 경계부호를 호출합니다.func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let newLocation = locations.last,
        CLLocationCoordinate2DIsValid(newLocation.coordinate) else {
            return
    }
    self.mpsTextField.text = "".appendingFormat("%.2f", newLocation.speed)
    self.kphTextField.text = "".appendingFormat("%.2f", newLocation.speed * 3.6)
}
실행 결과
 샘플 출처
덤
iOS 시뮬레이터는 시티 비씨클 라이드(자전거를 타고 이동하는 경우) 등을 시뮬레이트해 각자 이동 속도를 낸 결과 이같이 나타났다.
City Run
City Bicycle Ride
Freeway Drive
약 4m/s
약7m/s
약 33m/s
약 14km/h
약 27km/h
약 120km/h
Freeway Drive의 이동 속도가 의외로 빨라 깜짝 놀랐다
Reference
이 문제에 관하여([iOS] CLLoction Manager를 통한 이동 속도(Swift 편)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/koogawa/items/0c23fbb2ab1e6a10055a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)