标签:
ios不能指定采用哪种定位方式,会根据设备的情况和周围的环境采用一套最佳的解决方案。
再定位服务的应用中,第一次请求位置信息时,系统会提示用户是否允许开启定位服务。
Ios主要通过三个类来实现定位:
1) CLLocationManager,用于定位服务管理类,它能够给我们提供位置信息和高度信息。
2) CLLocationManagerDelegate,它是CLLocationManager类的委托协议
3) CLLocation.该类封装了位置和高度信息
CLLocationManager类的desiredAccurecy属性,有6个取值。
kCLLocationAccuracyNearestTenMeters,精确到10米
kCLLocatinAccuracyHundredMeters,精确懂100米
kCLLocatinAccuracyKilometer,精确到1000米
kCLLocatinAccuracyThreeKilometer,精确到3000米
kCLLocatinAccuracyBest,设备使用电池供电时最高精度
kCLLocatinAccuracyBestForNavigation,导航情况下的最高精度
两个代理方法:
locationManager:didUpdateLocations:定位成功
locationManager:didFailWithError:定位失败
代码如下:
#import "LocationViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
@interface LocationViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager * locationManager;
@end
@implementation LocationViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化locationManager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 0.1f;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//开始定位
[self.locationManager startUpdatingLocation];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//停止定位
[self.locationManager stopUpdatingLocation];
}
#pragma mark - 代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation * location = [locations lastObject];
NSLog(@"%3.5f", location.coordinate.latitude);//经度
NSLog(@"%3.5f", location.coordinate.longitude);//纬度
NSLog(@"%3.5f", location.altitude);//高度
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error: %@", error);
}
@end
标签:
原文地址:http://www.cnblogs.com/sjzlovecj/p/4667838.html