我正在开发一个应用程序,它涉及在X分钟内跟踪新位置和服务器更新。当时间差异等于或大于X分钟时,我停止位置更新,然后发送到服务器,然后如果它小于X分钟则开始更新位置。但是当小于X分钟时,不会调用didUpdateToLocation委托方法。
我在这里发布我的代码:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (theDiff > 10.0f || myDate == nil)
{
[self stopUpdating];
}
else
{
[self.mLocationManager startUpdatingLocation];
}
}
答案 0 :(得分:1)
试试这个。,
//in your .h
CLLocationManager *locationManager;
@property (nonatomic,retain) CLLocationManager *locationManager;
// Then synthesize it in your .m
@synthesize locationManager;
//in your viewDidLoad
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
无论您何时随手机移动,都会调用此方法。您也可以通过更改模拟器locatin来检查这一点。
希望这会有所帮助。谢谢,快乐的编码。
答案 1 :(得分:0)
在.m中尝试添加此方法,或者如果您有任何其他自定义代码发布
-(void)LocationWithTime
{
[CLController sharedInstance].locationManager.desiredAccuracy = 100.0;
[CLController sharedInstance].delegate = self;
BOOL locationAccessAllowed = NO ;
if( [CLLocationManager respondsToSelector:@selector(locationServicesEnabled)] )
{
locationAccessAllowed = [CLLocationManager locationServicesEnabled] ;
}
if(locationAccessAllowed == YES)
{
[[CLController sharedInstance].locationManager startUpdatingLocation];
}
}
这是我使用的委托方法 -
-(void)newLocationUpdate:(NSString *)latvalue longValue:(NSString*)longvalue Error:(int) errCode
{
DebugLog(@"LatLong values %@ %@", [UserContext sharedInstance].strLat, [UserContext sharedInstance].strLong);
self.strLALat = latvalue;
self.strLALong = longvalue;
[[CLController sharedInstance].locationManager stopUpdatingLocation];
[CLController sharedInstance].delegate=nil ;
}
答案 2 :(得分:0)
摘自Apple API文档,
/ *
* locationManager:didUpdateToLocation:fromLocation:
*
*此方法已弃用。如果locationManager:didUpdateLocations:是
*实现后,不会调用此方法。
* /
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6, __MAC_NA, __IPHONE_2_0, __IPHONE_6_0);
改为使用以下委托方法,
/ * * locationManager:didUpdateLocations: * locations是按时间顺序排列的CLLocation对象数组。 * /
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);
一旦设置了委托,这将有效。
希望这有帮助。