2013-01-19 106 views
3

我在我的代码中实现了Track me选项。 CLLocationManager未按预期工作。当我启动应用程序保持在同一位置时,CLLocationManager在1分钟内变化20-30米左右..然后我保持不变。CLLocationManager跟踪错误的位置(跟踪我)

,如果我改变我的位置跟踪同样的事情发生在开始1分钟CLLocationManager运动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]; 

    } 

} 
+0

你在哪里调用 - (void)start方法。 – Madhu

+0

ya它在Viewcontroller ..它工作正常..没有问题,那...问题是在位置.. – Christien

+0

注意:你正在使用的方法 - (void)locationManager:(CLLocationManager *)管理器didUpdateToLocation :(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation'已在iOS 6中弃用。 –

回答

1

我想给距离滤波器的有效使用此

self.locationManager.distanceFilter = kCLDistanceFilterNone; 

,你可以开始更新位置的方法,但也尝试这种方法需要这些两种方法得到EXAC t位置

[locationManager startMonitoringSignificantLocationChanges]; 
2

我在当前的计步器应用程序中遇到了同样的问题。我伸了两天,把头撞了几天。然后我发现CLLocationManager无法跟踪< 5米的距离和位置生成更新。我保持self.locationManager.distanceFilter =2.0;,它给了我位置更新甚至设备是固定的。所以我只是将distancefilter改为5.0米,并开始工作。尝试服用5米,应该工作,我测试和我所有的错误通知的问题消失了:

self.locationManager.distanceFilter =5.0; 

你正在服用self.locationManager.distancefilter=0.0001这是我想出来能力CLLocationManager的跟踪这些小运动。您还需要过滤旧位置,即如Location Awareness Guide by Apple中所述的缓存位置更新。我在我的代码中使用了这个条件来过滤所有大于5秒的事件。

- (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. 
    } 
} 
+0

你做了同样的事情,但仍然从不变的位置改变立场。 – Christien

+0

然后奇怪..:/你有没有设置'locationManager.desiredAccuracy = kCLLocationAccuracyBest;'? –

+0

ya ..你可以给我你的邮件编号,这样你就可以检查... – Christien