我搜索了很多并没有得到任何满意的答案,我有一个场景,我根据用户的当前位置显示卖家列表。我第一次得到一个位置,当我运行我的应用程序后,每当我尝试获取位置时,我得到一个缓存位置数据。我甚至在每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)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<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{}
}
答案 0 :(得分:0)
你可以做点什么,
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)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 (nonatomic,retain) CLLocationManager *locationManager;
与
@property (nonatomic,strong) CLLocationManager *locationManager;
或
将其声明为实例变量,如
CLLocationManager *locationManager;
并从每个实例中删除self!
第二件事,
你应该先做,
[self.locationManager requestWhenInUseAuthorization];
然后开始更新位置
[self.locationManager startUpdatingLocation];
答案 1 :(得分:0)
你能尝试这样吗
self.locationManager.pausesLocationUpdatesAutomatically = NO;
因为它
另一方面,kCLLocationAccuracyBestForNavigation
使用最高速GPS。因此,最好使用此代替kCLLocationAccuracyNearestTenMeters
。
答案 2 :(得分:0)
狮子谢谢你的支持,实际上我正在检查广场内的位置,由于我没有得到正确的位置,我通过外面测试,它显示完美。无论如何感谢狮子,你真的很有帮助。