標籤:
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
ios定位服務