6

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

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

传递给回调的 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 的预期行为还是错误?

更新

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

#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.locationnil

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

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

而是检查 nil 用户位置(其中 0,0 坐标实际上可能是有效的),您应该使用:

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

也许这对某人有帮助。对我来说,“问题”是我也通过界面构建​​器设置了显示用户的位置。在这里删除它并通过代码设置它使委托方法可以正常工作。

于 2015-01-27T15:23:03.367 回答
0

使用“self.mapView.showsUserLocation = YES;” 在 viewDidLoad 中,它将开始尝试立即获取用户的位置。如果你把它拿出来,它会等到用户按下按钮。

于 2012-03-13T17:59:27.420 回答