MKMapView 不显示当前位置
我正在尝试让 MKMapView 显示我当前的位置。我设法在具有单个视图的应用程序中使其工作,但现在我尝试在 UITabBarController 内实现相同的视图。我的代码或多或少是相同的,但我仍然无法让它工作。
我正在遵循有关 iPhone 开发的 BigNerdRanch 指南。我实现了这个视图,就像他们对书中其他视图所做的那样;我首先编写了 init
方法:
#import "CurrentLocationViewController.h"
#import "MapPoint.h"
@implementation CurrentLocationViewController
- (id)init
{
self = [super initWithNibName:@"CurrentLocationViewController"
bundle:nil];
if (self) {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:@"Location"];
UIImage *i = [UIImage imageNamed:@"Hypno.png"];
[tbi setImage:i];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[worldView setShowsUserLocation:YES];
}
return self;
}
我也通过这样做将该 init 方法指定为指定的初始化程序:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return [self init];
}
如您所见,我确实在我的 上调用了
在我的头文件中声明为: setShowsUserLocation:YES
>worldviewIBOutlet MKMapView *worldView;
。
我可能在这里遗漏了一些东西,但我认为该消息是让基本地图视图正常工作所需的唯一信息?有什么想法吗?
如果需要的话我可以提供更多代码。顺便说一句,该视图确实正确加载,我首先检查了令人讨厌的颜色,它确实显示了地图。它只是没有在上面放置蓝色别针。
谢谢。
I'm trying to get an MKMapView show my current location. I managed to get this working in an application with a single view, but now I'm trying to implement the same view inside a UITabBarController. My code is more or less the same, but I still can't get it to work.
I'm following the BigNerdRanch guide on iPhone development. I implemented the view like they did with the other views in the book; I first wrote the init
method:
#import "CurrentLocationViewController.h"
#import "MapPoint.h"
@implementation CurrentLocationViewController
- (id)init
{
self = [super initWithNibName:@"CurrentLocationViewController"
bundle:nil];
if (self) {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:@"Location"];
UIImage *i = [UIImage imageNamed:@"Hypno.png"];
[tbi setImage:i];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[worldView setShowsUserLocation:YES];
}
return self;
}
And I made that init method the designated initializer by doing this aswell:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return [self init];
}
As you can see, I did call setShowsUserLocation:YES
on my worldview
which is declared as: IBOutlet MKMapView *worldView;
in my header file.
I'm probably missing something here, but I thought that message was the only thing I needed to get the basic mapview working? Any ideas?
I can provide more code if necessary. The view does load properly btw, I checked with an obnoxious colour first and it does show the map. It just doesn't put a blue pin on it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经在 xib 文件中创建了 MapView,您是否确定已连接 worldView 插座?
此外,您不需要为此设置 CoreLocation。只是为了比较 - 这是我对同一练习的控制器定义。它不显示 - 但我在 xib 文件而不是代码中设置了显示用户位置属性。
You've created the MapView in a xib file, have you made sure that you have connected the worldView outlet?
Also, you don't need to set up CoreLocation for this. Just for comparison - here's my controller definition for the same exercise. It doesn't show - but I set the shows user location property in the xib file rather than in code.
您仅在模拟器中进行测试吗? Simulator 中的蓝色 userLocation 点显示在 1 Infinite Loop 处,这意味着如果放大不显示 Cupertino 的区域,您可能无法看到它。
Are you testing in Simulator only? The blue userLocation dot in Simulator is displayed at 1 Infinite Loop, which means you may not be able to see it if you are zoomed in on a region that does not display Cupertino.