LoginSignup
21
19

More than 5 years have passed since last update.

[iOS] CLLocationManagerで移動速度を取得する

Last updated at Posted at 2015-01-04

Swift編はこちら:http://qiita.com/koogawa/items/0c23fbb2ab1e6a10055a


CLLocation クラスの CLLocationSpeed プロパティを使います。

/*
 *  speed
 *  
 *  Discussion:
 *    Returns the speed of the location in m/s. Negative if speed is invalid.
 */
@property(readonly, nonatomic) CLLocationSpeed speed __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_2_2);

単位は m/s です。位置情報が取得できない時はマイナスの値が返ります。精度はそこまで高くないようです。

実装例

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    // Speedを更新
    self.speedTextField.text = [NSString stringWithFormat:@"%.2f", newLocation.speed];

    // km/hで表示したい場合
    self.kphTextField.text = [NSString stringWithFormat:@"%.2f", newLocation.speed * 3.600];
}

実行結果

iOSシミュレータのスクリーンショット 2015.01.04 11.46.17.png

サンプルソース

おまけ

iOSシミュレータでは City Bicycle Ride(自転車で移動している場合)などを再現できますが、それぞれの速度を取得したところ次のような結果になりました。

City Run City Bicycle Ride Freeway Drive
約4m/s 約7m/s 約33m/s
約14km/h 約27km/h 約120km/h

Freeway Drive の移動速度が意外と速くて驚きましたw

21
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
19