这是MKMapKitDelegate

这是MKMapKitDelegate

本文介绍了这是MKMapKitDelegate mapView:didUpdateUserLocation的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个测试应用程序,其中只有一个包含MKMapView的视图和一个充当MapView委托的控制器。

I've created a test application with only one view containing an MKMapView and a controller which acts as the MapView's delegate.

当我进行全新构建时(删除)从设备完全重新安装之前)并记录回调,我可以看到 mapView:didUpdateUserLocation 在用户指示是否希望显示其当前位置之前被调用两次或不是。

When I do a fresh build (removed from the device completely before re-installing) and log the callbacks, I can see that mapView:didUpdateUserLocation is called twice before the user has indicated whether they wish to show their current location or not.

传递给回调的MKUserLocation对象无效:

The MKUserLocation objects passed to the callback are invalid:

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的预期行为还是错误?

Is this the expected behaviour for MKMapKit or a bug?

我在iPhone 4上运行它,而不是模拟器。
这是控制器代码:

I'm running this on my iPhone 4, not a simulator.Here's the controller code:

#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


推荐答案

我认为这是一个错误。

My opinion is that it's a bug.

当用户甚至没有授予权限时,地图视图如何说用户位置已更新?

How can the map view say user location has been updated when the user hasn't even granted permission?

我使用的解决方法是检查 userLocation.location 是否 nil

The workaround I use is to check if userLocation.location is nil:

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

    //do something with userLocation...
}

这篇关于这是MKMapKitDelegate mapView:didUpdateUserLocation的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:49