iOS -GPS定位服务和地图应用是两套完全不同的API。
iOS 7 的 四种定位服务
<span style="font-family:SimSun;font-size:12px;">- (void)viewDidLoad { [super viewDidLoad]; locationManager = [[CLLocationManager alloc]init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.distanceFilter = 1000.f; }</span>初始化一个定位管理器 设置好代理
desireAccuracy这个属性用来设置经度的
<span style="font-family:SimSun;font-size:12px;">kCLLocationAccuracyBest; 电池供电的最高精度 kCLLocationAccuracyNearestTenMeters; 精确到10米 kCLLocationAccuracyHundredMeters;精确到100米 kCLLocationAccuracyKilometer;精确到1000米 kCLLocationAccuracyThreeKilometers;精确到3000米</span>
distabceFilter 属性定义了设备移动后获得位置信息的最小距离,单位是米;
设置距离筛选器distanceFilter,表示设备至少移动1000米,才通知委托更新
在视图展现的时候分别开启和关闭定位服务
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [locationManager startUpdatingLocation]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [locationManager stopUpdatingLocation]; }
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation * currentLocation = [locations lastObject]; double altitude = currentLocation.altitude;//高度 double latitude = currentLocation.coordinate.latitude; double longitude = currentLocation.coordinate.longitude; }
cllocation类
初始化函数 – initWithLatitude:longitude: – initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:timestamp: – initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:course:speed:timestamp: 属性: coordinate property 地理坐标 altitude property 高度 horizontalAccuracy property 地图中显示圆的半径 verticalAccuracy property 垂直高度的精确度 timestamp property 位置确定的时间 – distanceFromLocation: 指定位置到当前位置的距离 speed property 运动的速度 course property 设备的方向
CLLocation 封装了位置、高度等信息 altitude是高度值 coordinate是封装的经纬度的结构体 CLLocationCoordinate2D typedef struct { CLLocationDegrees latitude; //经度 CLLocationDegrees longitude; //纬度 } CLLocationCoordinate2D; CLLocationDegrees 是double类型别名