我正处于iOS编程第5章的开头:大书呆子牧场指南(第3版)。我似乎遇到的问题是蓝点(用户)没有开始显示,不仅我的地图似乎也没有显示。 我在XIB文件中的连接似乎是正确的,但我对这个问题感到茫然,有人可以提供帮助。感谢。
import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate,MKMapViewDelegate>
{
CLLocationManager *locationManager;
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTitleField;
}
@end
#import "WhereamiViewController.h"
@interface WhereamiViewController ()
@end
@implementation WhereamiViewController
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Create location manger object
locationManager = [[CLLocationManager alloc] init];
// There will be a warning from this line of code; ignore it for now
[locationManager setDelegate:self];
// And we want it to be as accurate as possible
// regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// Don't need this line as it is implemented in MKMapView
//[locationManager startUpdatingLocation];
}
return self;
}
-(void) viewDidLoad
{
NSLog(@"world view:set show user locatio");
NSLog(@"%@", worldView);
[worldView setShowsUserLocation:YES];
}
-(void) locationManager: (CLLocationManager *) manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation);
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[worldView setRegion:region animated:YES];
}
-(void) locationManger:(CLLocationManager *)manager
didFailWithError: (NSError *) error
{
NSLog(@"Could not find location: %@", error);
}
-(void)dealloc
{
//Tell the location manager to stop sending us message
[locationManager setDelegate:nil];
}
@end
此外,显示您分配+ init WhereamiViewController的代码。
#import <UIKit/UIKit.h>
@class WhereamiViewController;
@interface WhereamiAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) WhereamiViewController *viewController;
@end
#import "WhereamiAppDelegate.h"
#import "WhereamiViewController.h"
@implementation WhereamiAppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[WhereamiViewController alloc] initWithNibName:@"WhereamiViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
请参阅注释部分,了解IBOutlet MKMapView * worldView的调试器输出。