码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 定位

时间:2015-03-17 12:07:01      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

在移动互联网时代,移动app能解决用户的很多生活琐事,比如
导航:去任意陌生的地方
周边:找餐馆、找酒店、找银行、找电影院

在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2大功能,必须基于2个框架进行开发
Map Kit :用于地图展示
Core Location :用于地理定位

2个热门专业术语
LBS :Location Based Service
SoLoMo :Social Local Mobile(索罗门)
CoreLocation框架使用前提
导入框架



导入主头文件
#import <CoreLocation/CoreLocation.h>

CoreLocation框架使用须知
CoreLocation框架中所有数据类型的前缀都是CL
CoreLocation中使用CLLocationManager对象来做用户定位


CLLocationManager的常用操作
开始用户定位
- (void)startUpdatingLocation;

停止用户定位
- (void) stopUpdatingLocation;

当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下面方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
locations参数里面装着CLLocation对象


CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
经纬度

@property(readonly, nonatomic) CLLocationDistance altitude;
海拔

@property(readonly, nonatomic) CLLocationDirection course;
路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)

@property(readonly, nonatomic) CLLocationSpeed speed;
行走速度(单位是m/s)

用- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location方法可以计算2个位置之间的距离

@property(assign, nonatomic) CLLocationDistance distanceFilter;
每隔多少米定位一次

@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;
定位精确度(越精确就越耗电)

 

CLLocationCoordinate2D是一个用来表示经纬度的结构体,定义如下
typedef struct {
        CLLocationDegrees latitude; // 纬度
        CLLocationDegrees longitude; // 经度
} CLLocationCoordinate2D;

一般用CLLocationCoordinate2DMake函数来创建CLLocationCoordinate2D

 

用户隐私的保护

从iOS 6开始,苹果在保护用户隐私方面做了很大的加强,以下操作都必须经过用户批准授权
要想获得用户的位置
想访问用户的通讯录、日历、相机、相册等等

当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权

开发者可以在Info.plist中设置NSLocationUsageDescription说明定位的目的(Privacy - Location Usage Description)

 技术分享

技术分享

 

从iOS 8开始,用户定位分两种情况
总是使用用户位置:NSLocationAlwaysUsageDescription
使用应用时定位:NSLocationWhenInUseDescription

当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权

 

使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息

地理编码方法
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

反地理编码方法
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

CLGeocodeCompletionHandler
当地理\反地理编码完成时,就会调用CLGeocodeCompletionHandler
typedef void (^CLGeocodeCompletionHandler)(NSArray *placemarks, NSError *error);
这个block传递2个参数
error :当编码出错时(比如编码不出具体的信息)有值
placemarks :里面装着CLPlacemark对象


CLPlacemark的字面意思是地标,封装详细的地址位置信息
@property (nonatomic, readonly) CLLocation *location;
地理位置

@property (nonatomic, readonly) CLRegion *region;
区域

@property (nonatomic, readonly) NSDictionary *addressDictionary;
详细的地址信息

@property (nonatomic, readonly) NSString *name;
地址名称

@property (nonatomic, readonly) NSString *locality;
城市
//
//  ViewController.m
//  01-CoreLocation的基本使用
//
//  Created by apple on 15/1/30.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>

@property(nonatomic,strong)CLLocationManager *mgr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
    // 1.创建定位管理者
    CLLocationManager *mgr = [[CLLocationManager alloc] init];
    
    // 2.设置代理
    mgr.delegate = self;
    */
    
    // 3.开始定位
    [self.mgr startUpdatingLocation];
    
    // 4.计算两个经纬度之间的距离
    [self countDistance];
}

/**
 *  计算两个经纬度之间的距离
 */
- (void)countDistance
{
    CLLocation *location1 = [[CLLocation alloc] initWithLatitude:23.23 longitude:113.33];
    CLLocation *location2 = [[CLLocation alloc] initWithLatitude:40.06 longitude:116.39];
    
    CLLocationDistance distance = [location1 distanceFromLocation:location2];
    NSLog(@"%f", distance);
}

/**
 *  定位到用户的位置会调用该方法(并且该方法调用非常频繁)
 *
 *  @param locations 存放着定位的所有位置
 */
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // 1.获取用户位置的对象
    CLLocation *location = [locations lastObject];
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude);
    
    // 2.停止定位
    [manager stopUpdatingLocation];
}

#pragma mark - 懒加载
- (CLLocationManager *)mgr
{
    if (_mgr == nil) {
        // 1.创建定位管理者
        _mgr = [[CLLocationManager alloc] init];
        
        // 2.设置代理
        _mgr.delegate = self;
        
        // 3.位置间隔之后重新定位
        _mgr.distanceFilter = 10;
        
        // 4.定位的精确度
        _mgr.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        double verson = [[UIDevice currentDevice].systemVersion doubleValue];
        if (verson>=8.0) {
            [_mgr requestWhenInUseAuthorization];
        }
        
    }
    return _mgr;
}

@end

 

地理编码:

//
//  GeocodeViewController.m
//  02-Geocoder的使用
//
//  Created by apple on 15/1/30.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "GeocodeViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface GeocodeViewController ()

@property (weak, nonatomic) IBOutlet UITextField *addressFIeld;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

/**
 *  地理编码
 */
- (IBAction)geocode;

@end

@implementation GeocodeViewController

- (IBAction)geocode {
    // 1.取出用户输入的地址
    NSString *address = self.addressFIeld.text;
    if (address.length == 0) {
        NSLog(@"输入地址不能为空");
        return;
    }
    
    // 2.地理编码
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
        // 1.如果解析有错误,或者解析出的数组个数为0.直接返回
        if (placemarks.count == 0 || error) return;
        
        // 2.便利所有的地标对象(如果是实际开发,可以给用户以列表的形式展示)
        for (CLPlacemark *pm in placemarks) {
            // 2.1.取出用户的位置信息
            CLLocation *location = pm.location;
            // 2.2.取出用户的经纬度
            CLLocationCoordinate2D coordinate = location.coordinate;
            
            // 2.3.将信息设置到界面上
            self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", coordinate.latitude];
            self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f",coordinate.longitude];
            self.resultLabel.text = pm.name;
        }
    }];
}
@end

 反地理编码:

//
//  RervsecodeViewController.m
//  02-Geocoder的使用
//
//  Created by apple on 15/1/30.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "RervsecodeViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface RervsecodeViewController ()

@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UITextField *longitudeField;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

/**
 *  反地理编码
 */
- (IBAction)rervsecode;

@end

@implementation RervsecodeViewController

- (IBAction)rervsecode {
    // 1.取出输入的经纬度
    NSString *latitude = self.latitudeField.text;
    NSString *longitude = self.longitudeField.text;
    if (latitude.length == 0 || longitude.length == 0) {
        return;
    }

    // 2.反地理编码
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude.floatValue longitude:longitude.floatValue];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        // 如果有错误,或者解析出来的地址数量为0
        if (placemarks.count == 0 || error) return ;
        
        // 取出地标,就可以取出地址信息,以及CLLocation对象
        CLPlacemark *pm = [placemarks firstObject];
        
#warning 注意:如果是取出城市的话,需要判断locality属性是否有值(直辖市时,该属性为空)
        if (pm.locality) {
            self.resultLabel.text = pm.locality;
        } else {
            self.resultLabel.text = pm.administrativeArea;
        }
    }];
}
@end

 

备注:如果使用iOS8+模拟器iphone6如果定位会有问题,需要使用iphone5s模拟器,并判断iOS新增的方法设置

double verson = [[UIDevice currentDevice].systemVersion doubleValue];
        if (verson>=8.0) {
            [_mgr requestWhenInUseAuthorization];
            [_mgr requestAlwaysAuthorization];
        }

 

iOS 定位

标签:

原文地址:http://www.cnblogs.com/HJiang/p/4343945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!