[英]CLLocation Manager not updating new location
我正在调用CLLocationManager
,它也在调用其委托方法。 但是我的问题是,行驶1公里后是否不更新其新位置。
这是我的代码:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLocation1:) userInfo:nil repeats:YES];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
// Method did update location - Update location when location change
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// this method is calling after every 1 sec interval time..
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// this method is not calling once also after travelling around a km ....
}
我究竟做错了什么?
您应该在位置管理器上调用startUpdatingLocation
或startMonitoringSignificantLocationChanges
,以便它开始检查位置更改并调用委托方法。
locationManager:didUpdateToLocation:fromLocation:
已过时,因此您可以期望它不会总是被使用。
如果正在调用locationManager:didUpdateLocations:
,则您正在接收位置更新。
- (void)viewDidLoad
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//[manager stopUpdatingLocation];
CLLocationCoordinate2D coordinate_currentlocation = [newLocation coordinate];
float latitude_current = newLocation.coordinate.latitude;
float longitude_current = newLocation.coordinate.longitude;
}
您的第一种方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// this method is calling after every 1 sec interval time..
}
自iOS 6起使用。
从iOS 6开始不推荐使用第二种方法,并且在iOS 6之前使用第二种方法。可以通过添加帮助程序方法来使用这两种方法,具体取决于运行应用程序的设备的系统版本。
- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation
{
[self locationManager:manager helperForLocation:newLocation];
}
- (void) locationManager:(CLLocationManager *) manager didUpdateLocations:(NSArray *) locations
{
if([locations count] > 0)
{
[self locationManager:manager helperForLocation:[locations objectAtIndex:[locations count] - 1]];
}
}
- (void) locationManager:(CLLocationManager *) manager helperForLocation:(CLLocation *) newLocation
{
// your code what to do with location goes here
}
iOS 6检索到的位置被包装在一个列表中,并且可能不止1个。在我的示例中,我将最后一个放置到助手中。
我已经在我的应用程序中使用了此功能
myLocationManager = [[CLLocationManager alloc] init];
myLocationManager.delegate = self;
myLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
[myLocationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location, Please turn on GPS and Restart The Application"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
// Stop Location Manager
[myLocationManager stopUpdatingLocation];
}
-(void)postCurrentLocationOfDevice
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
self.locationManager.delegate = self;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CURRENT_LOCATION = [locations objectAtIndex:0];
[self.locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CURRENT_LOCATION = newLocation;
[self.locationManager stopUpdatingLocation];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.