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シミュレータでは 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