OC - 位置操作

OC - 位置操作 首页 / iOS入门教程 / OC - 位置操作

只要用户允许应用程序在核心位置框架的帮助下访问信息,无涯教程就可以在iOS中轻松找到用户的当前位置。

位置处理步骤

步骤1 - 创建一个基于View的简单应用程序。

步骤2 - 选择您的项目文件,然后选择目标,然后添加CoreLocation.framework,如下所示-

iOS Tutorial

步骤3 - 在 ViewController.xib 中添加两个标签,并创建ibOutlets,分别将标签命名为 latitudeLabel 和 longitudeLabel 。

无涯教程网

步骤4 - 通过选择File→New→File...→Object C Class创建一个新文件,然后单击下一步。

步骤5 - 将类命名为 LocationHandler ,将"sub class of"作为NSObject。

步骤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(nonatomicstrong) 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委托并创建两个ibOutlet-

#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

运行应用程序时,将获得以下输出-

iOS Tutorial

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学架构 -〔李运华〕

代码精进之路 -〔范学雷〕

消息队列高手课 -〔李玥〕

零基础入门Spark -〔吴磊〕

超级访谈:对话张雪峰 -〔张雪峰〕

郭东白的架构课 -〔郭东白〕

中间件核心技术与实战 -〔丁威〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

高并发系统实战课 -〔徐长龙〕

好记忆不如烂笔头。留下您的足迹吧 :)