您的位置:首页 > 移动开发 > IOS开发

iOS CoreLocation框架第二章—— CLLocationManagerDelegate全解

2016-05-12 10:53 417 查看
1、更新位置

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

//locationManager:didUpdateLocations:(调用很频繁)

//更新位置的方法之后就调用这个方法,数组中是按照时间的先后顺序,即为将旧的和新的位置放在数组中

下面的方法也是更新位置,但是此方法已经废弃。如果调用上面的方法,则下面的方法不会被调用

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

问题1、

发现有的时候更新位置的时候我们停止了相应的更新方法,但是还是打印出两个或者三个更行的位置。

2015-08-14 17:23:09.285 mylocation[450:209668] location is :<+22.52849988,+113.93162929> +/- 65.00m (speed -1.00 mps / course -1.00) @ 15/8/14 中国标准时间下午5:23:08

2015-08-14 17:23:09.285 mylocation[450:209668] location is :<+22.52849988,+113.93162929> +/- 65.00m (speed -1.00 mps / course -1.00) @ 15/8/14 中国标准时间下午5:23:08

更新的代码为:

#pragma mark CLLocationManagerDelegate

//定位成功

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

{

CLLocation *location = [locations lastObject];

NSLog(@" location is :%@ ",location.description);

[self.locationManager stopUpdatingLocation];

}

那是因为,我们将 [self.locationManager stopUpdatingLocation];放在了输出相应的信息之后,当我们停止定位的时候需要一定的时间,这段时间内系统又重新定位了一次或则多次,所以应该停止定位之后再输出,这样就可以只是输出当前的一次更新信息。

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

{

[self.locationManager stopUpdatingLocation];//先停止定位

CLLocation *location = [locations lastObject];

NSLog(@" location is :%@ ",location.description);

}

问题2、用模拟器定位失败

2015-08-14 17:17:17.455 mylocation[2953:98656] Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"

//定位失败之后显示的信息。

解决方法:

1.确定模拟器(手机)已经联网并且允许程序获取地理位置

2.重置地理位置服务或者网络服务

PS:如果是模拟器就果断直接重置模拟器吧 IOS Simulator - Reset Content and Settings..。

这样就重新设置模拟器,模拟器就是初始化的状态。

2、//定位失败了之后调用

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;

//3.方向的更新以后调用

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading;

//4.用于判断是否显示方向的校对,返回yes的时候,将会校对正确之后才会停止,或者dismissheadingcalibrationdisplay方法解除。

- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager;

//5.

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region;

//6.

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region;

//7.

- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error ;

//8.进入指定区域

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region ;

//9.离开指定的区域

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region;

//区域定位失败

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error ;

//改变里授权的状态

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus (CLAuthorizationStatus)status ;

//开始控制指定的区域

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region;

//已经停止位置的更更新

- (void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager ;

//位置定位重新开始定位位置的更新

- (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager ;

//已经完成了推迟的更新

- (void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error;

//就是已经访问过的位置,就会调用这个表示已经访问过,这个在栅栏或者定位区域都是使用到的

- (void)locationManager:(CLLocationManager *)manager didVisit:(CLVisit *)visit;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: