我正在使用 CLLocationManger 来更新当前位置。虽然它给了我位置,但我得到的位置是更古老的时间戳。我面临的问题是,如何强制位置控制器更新新位置而不是缓存位置。
目前我正在使用这个教程......
http://www.mobileorchard.com/hello-there-a-corelocation-tutorial/
有谁知道如何更新当前时间戳而不是缓存的位置?
我正在使用 CLLocationManger 来更新当前位置。虽然它给了我位置,但我得到的位置是更古老的时间戳。我面临的问题是,如何强制位置控制器更新新位置而不是缓存位置。
目前我正在使用这个教程......
http://www.mobileorchard.com/hello-there-a-corelocation-tutorial/
有谁知道如何更新当前时间戳而不是缓存的位置?
The best you can do is throw out locations that are too old by whatever standard you decide to set for your app. Restarting the location manager is supposed to tell the system you want a new location, but usually the first location you get back may be whatever it had last. Checking the timestamp may be all you can do, but be prepared for the possibility that you may never get a newer location. I've seen in my experience that if the device hasn't moved, it never bothers to update the system with a new location if it detects that the current location is good enough.
你可以使用这个方法
- (IBAction)update {
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
NSLog(@"*********this is location update method of login view controller");
[locmanager startUpdatingLocation];
}
-(void)awakeFromNib {
[self update];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (wasFound) return;
wasFound = YES;
CLLocationCoordinate2D loc = [newLocation coordinate];
lat2 = [[NSString stringWithFormat: @"%f", loc.latitude]retain];
logg2= [[NSString stringWithFormat: @"%f", loc.longitude]retain];
NSLog(@"latitude is %@",lat2);
NSLog(@"longitude is %@",logg2);
}
在 viewdidload 方法中使用 perform 选择器调用更新方法。这里 lat2 和 logg2 是字符串值。如果问题无法解决,请输入如何在 google 中查找当前位置。您将获得许多示例源代码和解释
你是在模拟器上还是在 iPhone 上运行你的项目?如果您在模拟器上运行项目,那么
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
只会被调用一次。
如果您在设备上进行测试,那么每次更改位置时都应该调用它。
确保您已设置位置管理器属性,
locationManager.delegate=self;
[locationManager startUpdatingLocation];
希望有帮助...