Skip to content

Instantly share code, notes, and snippets.

@taktamur
Created March 17, 2013 11:43
Show Gist options
  • Save taktamur/5181207 to your computer and use it in GitHub Desktop.
Save taktamur/5181207 to your computer and use it in GitHub Desktop.
MKMapViewのKVOを利用して、初回1回だけ現在位置に 地図を移動させる。
// MKMapViewのlocationにaddObserverして、場所が更新されたら中心点に移動する。
// http://blog.syuhari.jp/archives/2035
[self.mapView.userLocation addObserver:self forKeyPath:@"location" options:0 context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
// 一度しか現在地に移動しないなら removeObserver する
[self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment