iOS开发——定位 获取经纬度

来源:互联网 发布:个人网页源码 编辑:程序博客网 时间:2024/05/29 06:37

最近公司提供了一个学习iOS的机会,问我是否愿意学习iOS,还是继续只做android开发。我感觉非常幸运,能有此机会去学习iOS,当即说要学习iOS开发。

学习了几天啦,做了一个小demo实现了获取经纬度,海拔,航向,及具体位置信息的功能。

iOS使用定位不像android直接指定使用gps,获取网络获取。iOS定位需要使用Core Location框架,它提供了三种技术来获取位置:GPS、蜂窝或WiFi。

1   GPS卫星  优点最为准确,缺点 耗电量大,不能遮挡(建筑物内不行)

2   WI-FI通过WIFI路由器定位 优点比较省电,经济实惠

3   蜂窝式移动电话基站。通过移动运营商基站定位 误差比较大

在这些技术中,GPS最为精准,如果有GPS硬件,Core Location将优先使用它。如果设备没有GPS硬件(如WiFi iPad)或使用GPS获取当前位置时失败,

Core Location将退而求其次,选择使用WiFi,再次则使用蜂窝。

首先看一下定位的类 CLLocationManager 的方法


要使用位置管理器,必须首先将框架Core Location加入到项目中,再导入其接口文件:

导入框架的过程如下图所示:


#import<CoreLocation/CoreLocation.h>

还需要实现代理CLLocationManagerDelegate
如下ViewController.h

#import <UIKit/UIKit.h>#import <CoreLocation/CoreLocation.h>@interface ViewController : UIViewController <CLLocationManagerDelegate>{    CLLocationManager *_lm;    NSString *_latitude;    NSString *_longitude;}- (IBAction)startLocation;- (IBAction)stopLocation;@property (weak, nonatomic) IBOutlet UILabel *latLon;@property (weak, nonatomic) IBOutlet UILabel *city;@property (weak, nonatomic) IBOutlet UILabel *location;@end

初始化定位服务,定位精度和位置更新距离可以根据上面的方法自行设置。

这里需要提示的是:iOS 8 还提供了更加人性化的定位服务选项。App 的定位服务不再仅仅是关闭或打开。

现在,定位服务的启用提供了三个选项,「永不」「使用应用程序期间」和「始终」。同时,考虑到能耗问题,如果一款 App 要求始终能在后台开启定位服务,iOS 8 不仅会在首次打开 App 时主动向你询问,还会在日常使用中弹窗提醒你该 App 一直在后台使用定位服务,并询问你是否继续允许。在iOS7及以前的版本,如果在应用程序中使用定位服务只要在程序中调用 startUpdatingLocation方法应用就会询问用户是否允许此应用是否允许使用定位服务,同时在提示过程中可以通过在info.plist 中配置通过配置Privacy - Location Usage Description告诉用户使用的目的,同时这个配置是可选的。 
但是在iOS8中配置配置项发生了变化,可以通过配置NSLocationAlwaysUsageDescription或者 NSLocationWhenInUseUsageDescription来告诉用户使用定位服务的目的,并且注意这个配置是必须的,如果不进行配置则默 认情况下应用无法使用定位服务,打开应用不会给出打开定位服务的提示,除非安装后自己设置此应用的定位服务。同时,在应用程序中需要根据配置对 requestAlwaysAuthorization或locationServicesEnabled方法进行请求。


调用方法requesetAlwaysAuthorization则需要在info.plist中配置NSLocationAlwaysUsageDescription,type为string,value可填可不填。

调用方法requesetWhenInUseAuthorization则需要在info.plist中配置NSLocationWhenInUseUsageDescription,type为string,value可可不填。

这样才能获取到位置信息。

#import "ViewController.h"#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    if ([CLLocationManager locationServicesEnabled]) {        if (nil == _lm) {            _lm = [[CLLocationManager alloc]init];            _lm.delegate = self;            //设置定位精度            _lm.desiredAccuracy = kCLLocationAccuracyBest;            //设置位置更新的最小距离            _lm.distanceFilter = 100.f;            if (IS_IOS8) {//ios8之后点版本需要使用下面的方法才能定位。使用一个即可。                //[_lm requestAlwaysAuthorization];                [_lm requestWhenInUseAuthorization];            }        }    }else{        NSLog(@"定位服务不可利用");    }}

- (IBAction)startLocation{    NSLog(@"开始定位");    [_lm startUpdatingLocation];}- (IBAction)stopLocation {    NSLog(@"停止定位");    [_lm stopUpdatingLocation];}- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{    //NSLog(@"location %@",error);}- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{    _latitude = [NSString stringWithFormat:@"%3.5f",newLocation.coordinate.latitude];    _longitude = [NSString stringWithFormat:@"%3.5f",newLocation.coordinate.longitude];}- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    // 设备的当前位置    CLLocation *currLocation = [locations firstObject];    //获取经纬度    _latitude = [NSString stringWithFormat:@"%3.5f",currLocation.coordinate.latitude];    _longitude = [NSString stringWithFormat:@"%3.5f",currLocation.coordinate.longitude];    _latLon.text =[NSString stringWithFormat:@"lat %@,\nlong %@",_latitude,_longitude];    //获取海拔 航向 速度    NSLog(@"经度:%@,纬度:%@,海拔:%f,航向:%f,行走速度:%f",_longitude,_latitude,currLocation.altitude,currLocation.course,currLocation.speed);    CLGeocoder *geoCd = [[CLGeocoder alloc] init];//反编码通过经纬度获取具体位置信息。    [geoCd reverseGeocodeLocation:currLocation completionHandler:^(NSArray *array, NSError *error){        if (array.count > 0){            CLPlacemark *placemark = [array objectAtIndex:0];            //将获得的所有信息显示到label上            self.location.text = placemark.name;            //获取城市            NSString *cty = placemark.locality;            if (!cty) {                //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)                cty = placemark.administrativeArea;            }            self.city.text = cty;            NSLog(@"city = %@", cty);        }        else if (error == nil && [array count] == 0)        {            NSLog(@"No results were returned.");        }        else if (error != nil)        {            NSLog(@"An error occurred = %@", error);        }    }];  }@end

仔细看上面的代码,你会发现两个地方都在获取经纬度,你会很诧异吧,

这是因为

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

这个方法在ios6中已经废除变成另一个方法了。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

可以通过locations的下标获取新的定位和上一次的定位信息。

效果如图所示:


点击开始定位按钮,会startUpdatingLocation,由于定位服务是比较费电的,不用的时候要及时关闭定位,也可以获取定位成功后直接调用

stopUpdatingLocation停止定位,也就是在方法(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations中直接停止。



代码下载地点:http://pan.baidu.com/s/1eQuLPIE

有什么问题欢迎指正,大家一起进步啊。

0 0
原创粉丝点击