MKMapView 不显示当前位置

发布于 2024-11-28 01:51:35 字数 1521 浏览 1 评论 0原文

我正在尝试让 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 >worldview 在我的头文件中声明为: IBOutlet 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄兵必败 2024-12-05 01:51:35

您已经在 xib 文件中创建了 MapView,您是否确定已连接 worldView 插座?

此外,您不需要为此设置 CoreLocation。只是为了比较 - 这是我对同一练习的控制器定义。它不显示 - 但我在 xib 文件而不是代码中设置了显示用户位置属性。

#import "MapViewController.h"

@implementation MapViewController
@synthesize mapView;

// New designated initialiser
-(id)init {
    if (!(self = [super initWithNibName:@"MapViewController" bundle:nil])) {
        return nil; // Bail!
    }
    UITabBarItem *tbi = self.tabBarItem;
    tbi.title = @"Map";
    tbi.image = nil;

    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    return [self init];
}

- (void)dealloc {
    [mapView release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidUnload {
    [self setMapView:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - MapKit delegate methods

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    CLLocationCoordinate2D loc = [userLocation.location coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 1000.0f, 1000.0f);
    [aMapView setRegion:region animated:YES];
}

@end

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.

#import "MapViewController.h"

@implementation MapViewController
@synthesize mapView;

// New designated initialiser
-(id)init {
    if (!(self = [super initWithNibName:@"MapViewController" bundle:nil])) {
        return nil; // Bail!
    }
    UITabBarItem *tbi = self.tabBarItem;
    tbi.title = @"Map";
    tbi.image = nil;

    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    return [self init];
}

- (void)dealloc {
    [mapView release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidUnload {
    [self setMapView:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - MapKit delegate methods

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    CLLocationCoordinate2D loc = [userLocation.location coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 1000.0f, 1000.0f);
    [aMapView setRegion:region animated:YES];
}

@end
千年*琉璃梦 2024-12-05 01:51:35

您仅在模拟器中进行测试吗? 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文