[英]How to stop multiple times method calling of didUpdateLocations() in ios
這是我的代碼......
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
location_updated = [locations lastObject];
NSLog(@"updated coordinate are %@",location_updated);
latitude1 = location_updated.coordinate.latitude;
longitude1 = location_updated.coordinate.longitude;
self.lblLat.text = [NSString stringWithFormat:@"%f",latitude1];
self.lblLon.text = [NSString stringWithFormat:@"%f",longitude1];
NSString *str = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",latitude1,longitude1];
url = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection)
{
webData1 = [[NSMutableData alloc]init];
}
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(latitude1,longitude1);
marker.title = formattedAddress;
marker.icon = [UIImage imageNamed:@"m2.png"];
marker.map = mapView_;
marker.draggable = YES;
}
這個方法多次調用,我不想......
雖然分配好自己的LocationManager
對象,你可以設置distanceFilter
的財產LocationManager
。 距離過濾器屬性是CLLocationDistance
值,可以將其設置為通知位置管理器有關以米為單位移動的距離。 您可以按如下方式設置距離過濾器:
LocationManager *locationManger = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 100.0; // Will notify the LocationManager every 100 meters
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
在那里添加一些限制。 對於位置和准確度之間的時間跨度
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = locations.lastObject;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (newLocation.horizontalAccuracy < 0) return;
// Needed to filter cached and too old locations
//NSLog(@"Location updated to = %@", newLocation);
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
double distance = [loc1 distanceFromLocation:loc2];
if(distance > 20)
{
_currentLocation = newLocation;
//significant location update
}
//location updated
}
最簡單的方法:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
[manager stopUpdatingLocation];
manager.delegate = nil;
//...... do something
}
如果沒有委托引用,管理器找不到您的didUpdateLocations方法:-D
但是在使用startUpdatingLocation之前不要忘記再次設置它
我有類似的情況。 你可以使用dispatch_once:
static dispatch_once_t predicate;
- (void)update
{
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
[_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
}
_locationManager.delegate = self;
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
predicate = 0;
[_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[manager stopUpdatingLocation];
manager = nil;
dispatch_once(&predicate, ^{
//your code here
});
}
您可以使用靜態變量來存儲最新的位置時間戳,然后將其與最新的位置時間戳進行比較,如下所示:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[manager stopUpdatingLocation];
static NSDate *previousLocationTimestamp;
CLLocation *location = [locations lastObject];
if (previousLocationTimestamp && [location.timestamp timeIntervalSinceDate:previousLocationTimestamp] < 2.0) {
NSLog(@"didUpdateLocations GIVE UP");
return;
}
previousLocationTimestamp = location.timestamp;
NSLog(@"didUpdateLocations GOOD");
// Do your code here
}
如果要停止更新位置管理器,請編寫此方法
[locationManager stopUpdatingLocation];
對於時間限制,我不理解接受的答案中的代碼,發布不同的方法。 正如Rob指出的那樣“當你第一次啟動位置服務時,你可能會多次看到它被調用”。 下面的代碼作用於第一個位置,並在前120秒忽略更新的位置。 它是解決原始問題“如何停止多次調用didUpdateLocations的方法”的一種方法。
在.h文件中:
@property(strong,nonatomic) CLLocation* firstLocation;
在.m文件中:
// is this the first location?
CLLocation* newLocation = locations.lastObject;
if (self.firstLocation) {
// app already has a location
NSTimeInterval locationAge = [newLocation.timestamp timeIntervalSinceDate:self.firstLocation.timestamp];
NSLog(@"locationAge: %f",locationAge);
if (locationAge < 120.0) { // 120 is in seconds or milliseconds?
return;
}
} else {
self.firstLocation = newLocation;
}
// do something with location
你可以設置一個標志(Bool)。 當您實例化您的locationsManager set flag = true時,當locationManager:didUpdateLocations在您想要僅運行一次的代碼塊內返回set flag = false時。 這樣它只會運行一次。
if flag == true {
flag = false
...some code probably network call you only want to run the once
}
位置管理器將被多次調用,但是您只想執行一次代碼,我認為這是您要實現的目標?
locationManager.startUpdatingLocation()連續獲取位置並且didUpdateLocations方法調用多次,只需在調用locationManager.startUpdatingLocation()之前設置locationManager.distanceFilter值的值。
因為我設置200米(你可以根據你的要求改變)工作正常
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 200
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
你可以寫:[經理stopUpdatingLocation]; 經理=零; 在didupdatelocation委托中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.