IOS 开发,定位相关

来源:互联网 发布:网络负面处理 编辑:程序博客网 时间:2024/04/30 22:28

CoreLocation框架主要由两个常用的类,一个是CLLocationManager,一个是CLGeoCoder。

(1)CoreLocation的使用,先导入CoreLocation框架。

(2)一般是利用位置管理器来操作,即CLLocationManager

——开启,就是startUpdatingLocation;关闭,就是stopUpdatingLocation

——可以先判断位置服务是否开启locationServicesEnabled,如果没开启,直接返回空,不操作。

——iOS8的推送信息,需要获得用户允许,注册以下推送通知

——位置类CLLocation有很多属性,包括方向course,还有坐标coordinate,coordinate有经纬度属性latitude和longitude。

#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()<CLLocationManagerDelegate>@property(nonatomic,strong) CLLocationManager *locMgr;@end@implementation ViewController-(CLLocationManager *)locMgr{    //判断,定位服务是否开启,如果没开启,直接返回nil,不执行下面的操作    if (![CLLocationManager locationServicesEnabled]) return nil;        if (!_locMgr) {        _locMgr=[[CLLocationManager alloc]init];    }    return _locMgr;}- (void)viewDidLoad {    [super viewDidLoad];    if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {                    }    //设置代理    self.locMgr.delegate=self;    [self.locMgr startUpdatingLocation];}-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    CLLocation *loc=[locations firstObject];    NSLog(@"%f %f",loc.coordinate.latitude,loc.coordinate.longitude);    //如果只需要调用1次,则立即关闭,因为默认每隔一分钟调用一次这个方法,耗电    [manager stopUpdatingLocation];}@end

0 0
原创粉丝点击