使い方は最初に通知を設定
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(_updatedLocation:)
name:LKLocationManagerDidUpdateLocationNotification
object:nil];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(_finishedLocation:)
name:LKLocationManagerDidFinishLocationNotification
object:nil];ハンドラを書いて
- (void)_updatedLocation:(NSNotification*)notification
{
LKLocationManager* manager = notification.object;
CLLocation* location = manager.location;
:
}
- (void)_finishedLocation:(NSNotification*)notification
{
LKLocationManager* manager = notification.object;
CLLocation* location = manager.location;
:
}位置取得をスタート
[LKLocationManager.sharedManager startUpdate];
十分な精度になるか一定時間が過ぎたら自動的に止まる(詳細はコードを見て!)。
更新→ LKLocationManagerDidUpdateLocationNotification 更新→ LKLocationManagerDidUpdateLocationNotification : 精度条件を満たした or タイムアウト → LKLocationManagerDidUpdateLocationNotification(最後のコール) → LKLocationManagerDidFinishLocationNotification途中の状態は statusプロパティで取得できる。
typedef NS_ENUM(NSInteger, LKLocationManagerStatus) {
LKLocationManagerStatusIdle = 0,
LKLocationManagerStatusLocationUpdating,
LKLocationManagerStatusLocationUpdated,
LKLocationManagerStatusLocationCanceled,
LKLocationManagerStatusLocationFailed
};stopUpdateを使えば自分で止める事もできるが、最近の機種は精度の高い位置情報を取得するのに時間がかからなくなっているのであまり使う場面は無いと思われる(場所にもよるが)。別クラスで位置情報から地名を取得する(Reverse Geocoding)クラスも同梱している。
[LKReverseGeocoder reverseGeocodeLocation:manager.location
completionHandler:^(NSArray *placemarks,
NSString *addressString,
NSDictionary *addressDictionary,
NSError *error) {
self.place.text = addressString;
:
}];addressString は AddressBookUI フレームワークを使ってローカライズされた住所を返す。
こんな感じ
東京都千代田区丸の内1丁目9番地1
addressDictionary はこう
{
City = "San Francisco";
Country = "United States";
CountryCode = US;
FormattedAddressLines = (
"Apple Store, San Francisco",
"1800 Ellis St",
"San Francisco, CA 94115-4004",
"United States"
);
Name = "Apple Store, San Francisco";
PostCodeExtension = 4004;
State = CA;
Street = "1800 Ellis St";
SubAdministrativeArea = "San Francisco";
SubLocality = "Union Square";
SubThoroughfare = 1800;
Thoroughfare = "Ellis St";
ZIP = 94115;
}FormattedAddressLines からも住所をが得られる(ただこちらは国まで入っている)。



Responses
Leave a Response