我搜索了很多但没有得到任何满意的答案,我有一个场景,我根据用户的当前位置显示卖家列表。我第一次得到一个位置,然后当我尝试获取位置时运行我的应用程序时,我正在获取缓存位置数据。我确实在每 24 小时后尝试了一段时间,但仍然获得了我当前位置也被更改的缓存位置。以下是我用作引用的代码。请指教。
头文件中定义的属性
@property (nonatomic,retain) CLLocationManager *locationManager;
- (void)startSingleLocationRequest
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[self.locationManager startUpdatingLocation];
[self.locationManager requestWhenInUseAuthorization];
}
#pragma mark --didUpdateLocations
-(void)locationManagerCLLocationManager *)manager didUpdateLocationsNSArray<CLLocation *> *)locations{
self.latitudeValue = locations.lastObject.coordinate.latitude;
self.longitudeValue = locations.lastObject.coordinate.longitude;
// location set for simulation to UK
if(self.latitudeValue != 51.509979 && self.longitudeValue != -0.133700){
[MBProgressHUD hideHUDForView:self.outletSearchNearBy animated:YES];
[self.locationManager stopUpdatingLocation];
abc *slv =[self.storyboard instantiateViewControllerWithIdentifier"abc"];
slv.receivedLatitudeValue = self.latitudeValue;
slv.receivedLongitudeValue = self.longitudeValue;
[self.navigationController pushViewController:slv animated:YES];
}
else{}
}
你可以这样做,
-(void)locationManagerCLLocationManager *)manager didUpdateLocationsNSArray *)locations{
for(int i=0;i<locations.count;i++){
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0)
{
continue;
}
// do your all stuff here and then
break;
}
}
如果您的位置年龄超过 5 秒,您的代码将不会执行。您可以决定位置年龄是 10 秒还是 20 秒!
更新:
只需替换您的属性(property)声明
@property (nonatomic,retain) CLLocationManager *locationManager;
与
@property (nonatomic,strong) CLLocationManager *locationManager;
或
只需将其声明为实例变量,
CLLocationManager *locationManager;
并从它的每个实例中删除 self!
第二件事,
你应该先做,
[self.locationManager requestWhenInUseAuthorization];
然后开始更新位置,
[self.locationManager startUpdatingLocation];
关于ios - 获取当前位置而不是缓存 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47261066/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |