iphone - MKMapView 不显示当前位置

标签 iphone ios ios4 mkmapview

我正在尝试让 MKMapView 显示我当前的位置。我设法使它在具有单个 View 的应用程序中工作,但现在我试图在 UITabBarController 中实现相同的 View 。我的代码大致相同,但我仍然无法正常工作。

我正在遵循 BigNerdRanch iPhone 开发指南。我实现了这个 View ,就像他们对书中其他 View 所做的那样;我先写了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];
}

如您所见,我确实在我的 worldview 上调用了 setShowsUserLocation:YES,它在我的声明为:IBOutlet MKMapView *worldView;头文件。

我可能在这里遗漏了一些东西,但我认为该消息是让 basic map View 正常工作所需的唯一信息?有任何想法吗?

如有必要,我可以提供更多代码。顺便说一句, View 确实加载正确,我先检查了一种令人讨厌的颜色,它确实显示了 map 。它只是没有在上面放一个蓝色别针。

谢谢。

最佳答案

你已经在 xib 文件中创建了 MapView,你确定你已经连接了 worldView 导出吗?

此外,您不需要为此设置 CoreLocation。只是为了比较 - 这是我对同一练习的 Controller 定义。它不显示 - 但我在 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

关于iphone - MKMapView 不显示当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6934388/

相关文章:

iphone - 无法更新数据库ios

iphone - 在我的 WebView 中打开 PDF 并通过它调用电话

javascript - iPhone/iPad 应用程序图像格式化最佳实践

iOS - 设置 UIButton 背景颜色只为角落着色

ios - 将 CVImageBufferRef 转换为 CGImageRef 以直接处理像素有什么意义?

iphone - NSURLConnection 委托(delegate)和线程 - iPhone

ios - Sinch 视频通话声音来自前置扬声器

ios - 如何从 Spotify 获取当前用户的播放列表

iphone - 根据时区创建 NSDate 对象

iphone - 查看未更新,但主线程上的所有内容