iphone - 将 GPS 集成到我的应用程序中

标签 iphone objective-c ios core-location

我正在关注 tutorial将 GPS 集成到我的应用程序中。我想在 viewDidLoad 方法中显示 LatitudeLongitude 值。根据教程,他们用另一种方法显示了它。但我需要它显示在 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 组件,例如 UIActivityIndi​​catorView,并设置另一种方法来在您获取坐标时显示坐标。

关于iphone - 将 GPS 集成到我的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8870954/

相关文章:

iphone - MPMoviePlayer 流式视频缓冲区大小

c# - 什么是调整亮度的颜色矩阵?

objective-c - 从 Dropbox 下载文件并保存到路径

objective-c - 绘制触摸事件

ios - youtube embed with controls=0 不会在 iPad 上暂停

iphone - 重新发布 App Store 应用程序是否可能且合法?

ios - 带有 AVPlayerLayer 的 AVPlayer 不播放视频

iphone - Xcode 4.2 在 IB 中的选项卡栏上放置导航 Controller 时发出警告

ios - 如何从 TableView 推送到 TableView

iOS 开发 Swift 自定义单元