地理位置仅显示全球地图
我尝试按照本教程中所示对我的 iPhone 应用程序进行精确定位: http://www. youtube.com/watch?v=qNMNRAbIDoU 我的问题是,当我构建并运行时,我看到了地图,但地理定位服务没有跟踪我的位置,所以我只看到了全局地图:( 这是我的代码,我希望你能帮助我找出问题:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两件事需要检查:
1)模拟器没有位置,它总是显示苹果加州总部的经纬度
2)确定位置需要一点时间,所以如果你立即询问位置,你不会得到。
此外,您还需要在
WhereAmI
视图控制器中添加更多代码。通常您会在viewWillAppear
中看到一些内容。 此处是有关视图控制器和地图视图的 Big Nerd Ranch 教程的链接。也许它会对你有所帮助。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 theviewWillAppear
. Here is a link to a Big Nerd Ranch tutorial on view controllers and map views. Maybe it will help you.这里有一些事情需要注意:
1) iPhone 模拟器没有 GPS 功能,但它有一个默认位置 1 Infinite Loop, Cupertino, CA (USA)
2) 您的委托方法应该是
didUpdateFromLocation< /code>,而不是
didUpdateToLocation
。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
, notdidUpdateToLocation
.