iOS开发中最基本的位置功能实现示例

2020-01-14 16:25:30王冬梅
易采站长站为您分析iOS开发中最基本的位置功能实现示例,需要的朋友可以参考下  

定位获取位置及位置编码-反编码
我们的应用程序,可以通过添加Core Location框架所包含的类,获取设备的地图位置。
添加CoreLocation.framework框架,导入#import<CoreLocation/CoreLocation.h>。
使用地图服务时,会消耗更多地设备电量.因此,在获取到设备的位置后,应该停止定位来节省电量。
我们通过一个demo来展示内容与效果

 

复制代码
// 
// HMTRootViewController.h 
// My-GPS-Map 
// 
// Created by hmt on 14-4-12. 
// Copyright (c) 2014年 胡明涛. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface HMTRootViewController : UIViewController <CLLocationManagerDelegate> 

@end 

// 
// HMTRootViewController.m 
// My-GPS-Map 
// 
// Created by hmt on 14-4-12. 
// Copyright (c) 2014年 胡明涛. All rights reserved. 
// 

#import "HMTRootViewController.h" 
#import <AddressBook/AddressBook.h> 

@interface HMTRootViewController (){ 

CLLocationManager * _locationManage; 


@property (nonatomic,retain) CLLocationManager * locationManage; 

@end 

@implementation HMTRootViewController 

- (void)dealloc{ 

RELEASE_SAFELY(_locationManage); 
[super dealloc]; 



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
// Custom initialization 

return self; 


- (void)viewDidLoad 

[super viewDidLoad]; 
// Do any additional setup after loading the view. 

[self createGPSMap]; 
self.view.backgroundColor = [UIColor redColor]; 



- (void)createGPSMap{ 

// 初始化位置服务 
self.locationManage = [[CLLocationManager alloc]init]; 

// 要求CLLocationManager对象返回全部信息 
_locationManage.distanceFilter = kCLDistanceFilterNone; 

// 设置定位精度 
_locationManage.desiredAccuracy = kCLLocationAccuracyBest; 

// 设置代理 
_locationManage.delegate = self;