6

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
4

4 に答える 4

10

私の意見では、それはバグだと思います。

ユーザーが権限を付与していない場合でも、マップビューでユーザーの場所が更新されたことをどのように確認できますか?

私が使用する回避策は、次のことを確認することuserLocation.locationですnil

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    if (userLocation.location == nil)
        return;

    //do something with userLocation...
}
于 2012-03-14T13:21:17.623 に答える
5

代わりに、nil userlocation(0,0座標が実際に有効である可能性がある)をチェックする必要があります。

if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
  // do something with the coords
} else {
  // the coords are invalid
}
于 2012-10-12T09:07:28.237 に答える
0

「self.mapView.showsUserLocation=YES;」の場合 viewDidLoadでは、ユーザーの場所をすぐに取得しようとし始めます。それを取り出すと、ユーザーがボタンを押すまで待機します。

于 2012-03-13T17:59:27.420 に答える
0

多分これは誰かを助けます。私にとっての「問題」は、show user's location troughinterfaceBuilderも設定していたことでした。ここでそれを削除し、コードを介して設定すると、デリゲートメソッドが疑わしいとおりに機能するようになりました。

于 2015-01-27T15:23:03.367 に答える