- iOS 教程
- iOS - 首页
- iOS - 入门
- iOS - 环境设置
- iOS - Objective-C 基础
- iOS - 首个 iPhone 应用程序
- iOS - 动作和出口
- iOS - 委托
- iOS - UI 元素
- iOS - 加速计
- iOS - 通用应用程序
- iOS - 摄像头管理
- iOS - 定位处理
- iOS - SQLite 数据库
- iOS - 发送电子邮件
- iOS - 音频和视频
- iOS - 文件处理
- iOS - 访问地图
- iOS - 应用内购买
- iOS - iAd 集成
- iOS - GameKit
- iOS - 情节提要
- iOS - 自动布局
- iOS - Twitter 和 Facebook
- iOS - 内存管理
- iOS - 应用程序调试
- iOS 实用资源
- iOS - 快速指南
- iOS - 实用资源
- iOS - 讨论
iOS - 定位处理
只要用户允许应用程序利用核心位置框架访问信息,我们就能轻松定位 iOS 中的当前位置。
定位处理 – 涉及的步骤
步骤 1 − 创建一个基于视图的简单应用程序。
步骤 2 − 选择您的项目文件,然后选择目标,然后再按如下添加 CoreLocation.framework −
步骤 3 − 在 ViewController.xib 中添加两个标签,并创建名为 latitudeLabel 和 longitudeLabel 的 ibOutlets。
步骤 4 − 通过依次选择“文件 → 新建 → 文件... → 选择 Objective C 类”并单击“下一步”来创建一个新文件。
步骤 5 − 使用 “子类”为 NSObject 来将类的名称指定为 LocationHandler。
步骤 6 − 选择“创建”。
步骤 7 − 如下更新 LocationHandler.h −
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationHandlerDelegate <NSObject>
@required
-(void) didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation;
@end
@interface LocationHandler : NSObject<CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@property(nonatomic,strong) id<LocationHandlerDelegate> delegate;
+(id)getSharedInstance;
-(void)startUpdating;
-(void) stopUpdating;
@end
步骤 8 − 如下更新 LocationHandler.m −
#import "LocationHandler.h"
static LocationHandler *DefaultManager = nil;
@interface LocationHandler()
-(void)initiate;
@end
@implementation LocationHandler
+(id)getSharedInstance{
if (!DefaultManager) {
DefaultManager = [[self allocWithZone:NULL]init];
[DefaultManager initiate];
}
return DefaultManager;
}
-(void)initiate {
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
}
-(void)startUpdating{
[locationManager startUpdatingLocation];
}
-(void) stopUpdating {
[locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if ([self.delegate respondsToSelector:@selector
(didUpdateToLocation:fromLocation:)]) {
[self.delegate didUpdateToLocation:oldLocation
fromLocation:newLocation];
}
}
@end
步骤 9 − 如下更新 ViewController.h,我们在其中实现了 LocationHandler 委托并创建了两个 ibOutlets −
#import <UIKit/UIKit.h>
#import "LocationHandler.h"
@interface ViewController : UIViewController<LocationHandlerDelegate> {
IBOutlet UILabel *latitudeLabel;
IBOutlet UILabel *longitudeLabel;
}
@end
步骤 10 − 如下更新 ViewController.m −
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[LocationHandler getSharedInstance]setDelegate:self];
[[LocationHandler getSharedInstance]startUpdating];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
[latitudeLabel setText:[NSString stringWithFormat:
@"Latitude: %f",newLocation.coordinate.latitude]];
[longitudeLabel setText:[NSString stringWithFormat:
@"Longitude: %f",newLocation.coordinate.longitude]];
}
@end
输出
当我们运行此应用程序时,我们将得到以下输出 −
广告