获得当前位置后如何停止更新位置?
How to stop updating location once I get current location?
我正在使用 Parse 和 geoPointForCurrentLocationInBackground,我可以在收到位置后停止更新,而无需手动停止。
如何在使用 CLLocationManager 收到位置后立即停止更新位置?
编辑
我知道 [self.locationManager stopUpdatingLocation];阻止它。
我真正要问的是,我怎么知道我是第一次收到位置然后立即停止?
获取您的位置后,使用此方法:
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
stopMonitoringSignificantLocationChanges
的反义词不是stopUpdatingLocation
,而是startMonitoringSignificantLocationChanges
。
查看 CLLocation documentation 了解更多详情。
调用 didUpdateLocations
方法后立即调用 stopUpdatingLocation
。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[manager stopUpdatingLocation];
//store your location
self.location = [locations lastObject];
}
获取一次位置后调用以下方法保存停止
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
self.location = [locations lastObject]
self.locationManager.delegate = nil;
[self.locationManager stopUpdatingLocation];
}
BOOL first_time = YES; // public
每次开始更新位置时,将 first_time 设置为是:
first_time = YES;
[self.locationManager startUpdatingLocation];
在您的 didUpdateUserLocation 方法中:
if (userLocation == nil) {
NSLog(@"User location is nil. maybe wating for permission");
} else if (!CLLocationCoordinate2DIsValid(userLocation.coordinate)) {
NSLog(@"User location is not valid 2d coordinates. maybe called in background");
} else {
NSLog(@"Did update user location: %f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
// here is the first time you receive user location
if (first_time)
{
first_time = NO;
[self.locationManager stopUpdatingLocation];
}
}
终于在另一个问题中找到了答案...在这里转载,因为我花了一段时间才偶然发现它。
我不得不这样称呼:
[_locationManager stopMonitoringSignificantLocationChanges];
尽管我从来没有调用过 startMonitoringSignificantLocationChanges,但似乎我不得不 "un-call" 它......奇怪的是它以这种方式工作,但一旦我添加了那条线,定位服务就会立即关闭.希望这对其他人有帮助。
一旦当前位置更新停止使用 stopUpdatingLocation
的位置管理器。
region.center = self.mapView.userLocation.coordinate;
if (region.center.longitude != 0 && region.center.latitude != 0) {
[self.locationManager stopUpdatingLocation];
}
self.yourLocationManager.stopUpdatingLocation()
我正在使用 Parse 和 geoPointForCurrentLocationInBackground,我可以在收到位置后停止更新,而无需手动停止。
如何在使用 CLLocationManager 收到位置后立即停止更新位置?
编辑
我知道 [self.locationManager stopUpdatingLocation];阻止它。 我真正要问的是,我怎么知道我是第一次收到位置然后立即停止?
获取您的位置后,使用此方法:
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
stopMonitoringSignificantLocationChanges
的反义词不是stopUpdatingLocation
,而是startMonitoringSignificantLocationChanges
。
查看 CLLocation documentation 了解更多详情。
调用 didUpdateLocations
方法后立即调用 stopUpdatingLocation
。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[manager stopUpdatingLocation];
//store your location
self.location = [locations lastObject];
}
获取一次位置后调用以下方法保存停止
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
self.location = [locations lastObject]
self.locationManager.delegate = nil;
[self.locationManager stopUpdatingLocation];
}
BOOL first_time = YES; // public
每次开始更新位置时,将 first_time 设置为是:
first_time = YES;
[self.locationManager startUpdatingLocation];
在您的 didUpdateUserLocation 方法中:
if (userLocation == nil) {
NSLog(@"User location is nil. maybe wating for permission");
} else if (!CLLocationCoordinate2DIsValid(userLocation.coordinate)) {
NSLog(@"User location is not valid 2d coordinates. maybe called in background");
} else {
NSLog(@"Did update user location: %f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
// here is the first time you receive user location
if (first_time)
{
first_time = NO;
[self.locationManager stopUpdatingLocation];
}
}
终于在另一个问题中找到了答案...在这里转载,因为我花了一段时间才偶然发现它。
我不得不这样称呼:
[_locationManager stopMonitoringSignificantLocationChanges];
尽管我从来没有调用过 startMonitoringSignificantLocationChanges,但似乎我不得不 "un-call" 它......奇怪的是它以这种方式工作,但一旦我添加了那条线,定位服务就会立即关闭.希望这对其他人有帮助。
一旦当前位置更新停止使用 stopUpdatingLocation
的位置管理器。
region.center = self.mapView.userLocation.coordinate;
if (region.center.longitude != 0 && region.center.latitude != 0) {
[self.locationManager stopUpdatingLocation];
}
self.yourLocationManager.stopUpdatingLocation()