gpt4 book ai didi

ios - 这是 MKMapKitDelegate mapView :didUpdateUserLocation? 的错误吗

转载 作者:可可西里 更新时间:2023-11-01 03:37:56 26 4
gpt4 key购买 nike

我创建了一个只有一个 View 的测试应用程序,该 View 包含一个 MKMapView 和一个充当 MapView 委托(delegate)的 Controller 。

当我进行全新构建(在重新安装之前完全从设备中删除)并记录回调时,我可以看到在用户指示他们是否希望之前调用了两次 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 上运行它,而不是模拟器。这是 Controller 代码:

#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

最佳答案

我认为这是一个错误。

当用户甚至没有授予权限时, map View 怎么会说用户位置已更新?

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

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

//do something with userLocation...
}

关于ios - 这是 MKMapKitDelegate mapView :didUpdateUserLocation? 的错误吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9680576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com