MKMapViewとMapViewのデリゲートとして機能するコントローラーを含む1つのビューのみを使用してテストアプリケーションを作成しました。
フレッシュビルド(再インストールする前にデバイスから完全に削除)を実行してコールバックをログに記録するとmapView:didUpdateUserLocation
、ユーザーが現在の場所を表示するかどうかを指定する前に、それが2回呼び出されることがわかります。
コールバックに渡されたMKUserLocationオブジェクトは無効です。
2012-03-13 08:20:17.518 MapTest[3325:707] Did update user location: 0.000000,0.000000
2012-03-13 08:20:17.581 MapTest[3325:707] Did update user location: 0.000000,0.000000
これはMKMapKitの予想される動作ですか、それともバグですか?
アップデート
私はこれをシミュレーターではなくiPhone4で実行しています。コントローラコードは次のとおりです。
#import "ViewController.h"
@implementation ViewController
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(IBAction)trackButtonTapped:(id)sender
{
self.mapView.showsUserLocation = !self.mapView.showsUserLocation;
}
#pragma mark - MKMapKitDelegate
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"Did update user location: %f,%f", userLocation.coordinate.latitude, userLocation.coordinate.longitude);
}
-(void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
NSLog(@"Will start loading map");
}
-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
NSLog(@"Did finish loading map");
}
-(void)mapViewWillStartLocatingUser:(MKMapView *)mapView
{
NSLog(@"Will start locating user");
}
-(void)mapViewDidStopLocatingUser:(MKMapView *)mapView
{
NSLog(@"Did stop locating user");
}
-(void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
NSLog(@"Did fail loading map");
}
-(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
if (error.code == kCLErrorDenied){
NSLog(@"User refused location services");
} else {
NSLog(@"Did fail to locate user with error: %@", error.description);
}
}
@end