我正在关注 tutorial将 GPS 集成到我的应用程序中。我想在 viewDidLoad
方法中显示 Latitude
和 Longitude
值。根据教程,他们用另一种方法显示了它。但我需要它显示在 viewDidLoad
中。我如何修改以下代码以在 viewDidLoad
中显示它?
HelloThereController.h
View Controller
#import "MyCLController.h"
@interface HelloThereViewController : UIViewController <MyCLControllerDelegate> {
MyCLController *locationController;
}
- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;
@end
HelloThereController.m
View Controller
#import "HelloThereViewController.h"
@implementation HelloThereViewController
- (void)viewDidLoad {
locationController = [[MyCLController alloc] init];
locationController.delegate = self;
[locationController.locationManager startUpdatingLocation];
//I need to print the latitude and logitude values here..
}
我的 MyCLController.h 类
@protocol MyCLControllerDelegate
@required
- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;
@end
@interface MyCLController : NSObject {
CLLocationManager *locationManager;
id delegate;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, assign) id delegate;
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error;
@end
我的 MyCLController.m 类
#import "MyCLController.h"
@implementation MyCLController
@synthesize locationManager;
@synthesize delegate;
- (id) init {
self = [super init];
if (self != nil) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.delegate locationUpdate:newLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
[self.delegate locationError:error];
}
- (void)dealloc {
[self.locationManager release];
[super dealloc];
}
@end
最佳答案
如果此 View Controller 在应用程序加载后立即加载,则您不能。 位置管理器需要时间来获取 GPS 更新。
您需要显示一些让用户知道您正在获取位置的 ui 组件,例如 UIActivityIndicatorView,并设置另一种方法来在您获取坐标时显示坐标。
关于iphone - 将 GPS 集成到我的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8870954/