[英]MKMapView doesn't show current location
我正在嘗試讓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];
}
正如你所看到的,我在我的worldview
上調用了setShowsUserLocation:YES
,它被聲明為: IBOutlet MKMapView *worldView;
在我的頭文件中。
我可能在這里遺漏了一些東西,但我認為這個消息是我唯一能讓基本 mapview工作的東西? 有任何想法嗎?
如有必要,我可以提供更多代碼。 視圖確實加載正確順便說一句,我首先檢查了一個令人討厭的顏色,它確實顯示了地圖。 它只是沒有在它上面放一個藍色針腳。
謝謝。
您已經在xib文件中創建了MapView,是否確保已連接worldView插座?
此外,您無需為此設置CoreLocation。 僅供比較 - 這是我對同一練習的控制器定義。 它沒有顯示 - 但是我在xib文件而不是代碼中設置了show user location屬性。
#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
你是否只在模擬器中測試? 模擬器中的藍色userLocation點顯示在1 Infinite Loop,這意味着如果放大了不顯示Cupertino的區域,您可能無法看到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.