Iphone CLLocationManager跟踪错误位置(跟踪我)
我正在代码中执行Iphone CLLocationManager跟踪错误位置(跟踪我),iphone,ios,mkmapview,cllocationmanager,cllocation,Iphone,Ios,Mkmapview,Cllocationmanager,Cllocation,我正在代码中执行跟踪我选项。CLLocationManager未按预期工作。当我启动应用程序时,保持在相同位置,CLLocationManager在1分钟内变化约20-30米。然后我保持不变 如果我改变我的位置来跟踪在开始1分钟内发生的相同事情,那么LocationManager将额外移动20-30分钟,然后以我的速度移动 为什么会这样 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
跟踪我
选项。CLLocationManager
未按预期工作。当我启动应用程序时,保持在相同位置,CLLocationManager
在1分钟内变化约20-30米。然后我保持不变
如果我改变我的位置来跟踪在开始1分钟内发生的相同事情,那么LocationManager将额外移动20-30分钟,然后以我的速度移动
为什么会这样
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 0.0001;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
-(void)start {
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation {
[self processLocationChange:newLocation fromLocation:oldLocation];
}
-(void)processLocationChange:(CLLocation*)newLocation fromLocation:oldLocation {
if (newLocation != oldLocation) {
NSLog(@"Moved from %@ to %@", oldLocation, newLocation);
CLLocation* lastKnownLocation = NULL;
if ([self.locationPoints count] > 0) {
lastKnownLocation = [self.locationPoints objectAtIndex:[self.locationPoints count] - 1];
}
else {
lastKnownLocation = newLocation;
self.bottomLeft = newLocation.coordinate;
self.topRight = newLocation.coordinate;
}
// Check for new boundaries
CLLocationCoordinate2D coords = newLocation.coordinate;
if (coords.latitude < bottomLeft.latitude || coords.longitude < bottomLeft.longitude) {
self.bottomLeft = coords;
NSLog(@"Changed bottom left corner");
}
if (coords.latitude > topRight.latitude || coords.longitude > topRight.longitude) {
self.topRight = coords;
NSLog(@"Changed top right corner");
}
double speed = fabs(newLocation.speed);
double deltaDist = fabs([newLocation distanceFromLocation:lastKnownLocation]);
double newAvgSpeed = (self.totalDistance + deltaDist) / ((double)[self getElapsedTimeInMilliseconds] / 1000.0);
double accuracy = newLocation.horizontalAccuracy;
double alt = newLocation.altitude;
NSLog(@"Change in position: %f", deltaDist);
NSLog(@"Accuracy: %f", accuracy);
NSLog(@"Speed: %f", speed);
NSLog(@"Avg speed: %f", newAvgSpeed);
self.totalDistance += deltaDist;
self.currentSpeed = speed;
self.avgSpeed = newAvgSpeed;
self.altitude = alt;
NSLog(@"Delta distance = %f", deltaDist);
NSLog(@"New distance = %f", self.totalDistance);
// Add new location to path
[self.locationPoints addObject:newLocation];
// Update stats display
[self.first.start1 updateRunDisplay];
// Update map view
[self updateMap:lastKnownLocation newLocation:newLocation];
}
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.locationManager.distanceFilter=0.0001;
self.locationManager.desiredAccuracy=KCallocationAccuracyBest;
}
-(无效)开始{
[self.locationManager startUpdatingLocation];
}
-(无效)locationManager:(CLLocationManager*)经理
DidUpdateLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation{
[self-processLocationChange:newLocation fromLocation:oldLocation];
}
-(void)processLocationChange:(CLLocation*)newLocation fromLocation:oldLocation{
如果(新位置!=旧位置){
NSLog(@“从%@移动到%@”、旧位置、新位置);
CLLocation*lastKnownLocation=NULL;
如果([self.locationPoints count]>0){
lastKnownLocation=[self.locationPoints对象索引:[self.locationPoints计数]-1];
}
否则{
lastKnownLocation=新位置;
self.bottomLeft=newLocation.coordinate;
self.topRight=newLocation.coordinate;
}
//检查新边界
CLLocationCoordinate2D coords=newLocation.coordinate;
if(坐标系纬度<左下角纬度| |坐标系经度<左下角经度){
self.bottomLeft=coords;
NSLog(@“更改的左下角”);
}
if(坐标系纬度>右上角纬度| |坐标系经度>右上角经度){
self.topRight=coords;
NSLog(@“更改的右上角”);
}
双速=晶圆厂(newLocation.speed);
双三角列表=晶圆厂([newLocation distanceFromLocation:lastKnownLocation]);
double newAvgSpeed=(self.totalDistance+deltaDist)/(double)[self getelapsedtimeinms]/1000.0);
双精度=新位置。水平精度;
双高度=新位置高度;
NSLog(@“位置更改:%f”,增量列表);
NSLog(@“精度:%f”,精度);
NSLog(@“速度:%f”,速度);
NSLog(@“平均速度:%f”,新平均速度);
self.totalDistance+=三角洲列表;
self.currentSpeed=速度;
self.avgSpeed=newAvgSpeed;
自身高度=alt;
NSLog(@“增量距离=%f”,增量列表);
NSLog(@“新距离=%f”,自身总距离);
//将新位置添加到路径
[self.locationPoints addObject:newLocation];
//更新统计数据显示
[self.first.start1 updateRunDisplay];
//更新地图视图
[自更新映射:lastKnownLocation新位置:新位置];
}
}
我认为使用此选项可以有效地设置距离过滤器
self.locationManager.distanceFilter = kCLDistanceFilterNone;
您可以开始更新位置方法,但也可以尝试此方法。这两种方法都是获得精确位置所必需的
[locationManager startMonitoringSignificantLocationChanges];
我在当前的计步器应用程序中遇到了同样的问题。我伸了伸懒腰,撞了我的头好几天。然后我发现
CLLocationManager
无法跟踪调用-(void)start方法的位置。它在Viewcontroller中运行良好..没有问题..问题出在位置..注意:您使用的方法是-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation
在iOS 6中不推荐使用。不,我使用了iOS 5..iOS 5也给了我相同的更改。试试我在回答中的建议。应该能解决你的问题。此外,我还编辑了问题的标题以提高搜索能力。你也做了同样的操作,但仍然更改了不变的位置。奇怪的是../您是否设置了locationManager.desiredAccuracy=kCLLocationAccuracyBest代码>?您可以在skype patel.rohn上添加我
self.locationManager.distanceFilter =5.0;
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation=[locations lastObject];
NSDate* eventDate = currentLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if(abs(howRecent)<5.0 && self.currentLocation.horizontalAccuracy<=10 && self.currentLocation.horizontalAccuracy>0)
{
//you have got fresh location event here.
}
}