Geolocation only shows global map
I tried to make a goelocation to my iPhone app exactly as shown in this tutorials : http://www.youtube.com/watch?v=qNMNRAbIDoU my problem is when I build and go i see the map but the geolocation service didn't track my position, so I see just the global map :( Here is my code I hope you can help me to identifiy the problem :
appdelegate.h
@interface WhereAmIAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate> {
UIWindow *window;
WhereAmIViewController *viewController;
CLLocationManager *locationManager;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) IBOutlet WhereAmIViewController *viewController;
appdelegate.m:
@implementation WhereAmIAppDelegate
@synthesize window;
@synthesize viewController;
@synthesize locationManager;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.locationManager=[[[CLLocationManager alloc] init] autorelease];
if([CLLocationManager locationServicesEnabled]) {
self.locationManager.delegate=self;
self.locationManager.distanceFilter=100;
[self.locationManager startUpdatingLocation];
}
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#pragma mark CLLocationManagerDelegate Methods
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
MKCoordinateRegion region;
region.span=span;
region.center=newLocation.coordinate;
[viewController.mapView setRegion:region animated:YES];
viewController.mapView.showsUserLocation=YES;
viewController.latitude.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
viewController.longitude.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
}
@end
MyViewController.h
@interface WhereAmIViewController : UIViewController {
MKMapView *mapView;
UILabel *latitude;
UILabel *longitude;
开发者_运维技巧
}
@property (nonatomic,retain)IBOutlet MKMapView *mapView;
@property (nonatomic,retain)IBOutlet UILabel *latitude;
@property (nonatomic,retain)IBOutlet UILabel *longitude;
myViewController.m :
@implementation WhereAmIViewController
@synthesize mapView;
@synthesize latitude;
@synthesize longitude;
- (void)dealloc {
[mapView release];
[latitude release];
[longitude release];
[super dealloc];
}
@end
Two things to check:
1) The simulator does not have location, it will always show the lat/long of the Apple headquarters in California
2) It takes a little bit of time for the location to get settled, so if you ask for location right away you won't get one.
Also, you need some more code in your WhereAmI
view controller. Usually you will see something in the viewWillAppear
. Here is a link to a Big Nerd Ranch tutorial on view controllers and map views. Maybe it will help you.
There are a few things here to note:
1) The iPhone emulator does not have GPS capabilities, however it does have a default location of 1 Infinite Loop, Cupertino, CA (USA)
2) Your delegate method should be didUpdateFromLocation
, not didUpdateToLocation
.
精彩评论