CLLocationManager跟踪错误的位置(跟踪我)

时间:2013-01-19 07:51:19

标签: iphone ios mkmapview cllocationmanager cllocation

我正在我的代码中实施Track me选项。CLLocationManager无法按预期工作。当我开始app保持在同一位置时,CLLocationManager在20-30米左右变化 - 在1分钟内......然后我保持不变。

如果我改变我的位置以跟踪同样的事情发生在开始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];

    }

}

2 个答案:

答案 0 :(得分:2)

我在目前的计步器应用程序中遇到了同样的问题。我伸了个懒腰,把头撞了几天。然后我发现CLLocationManager无法跟踪&lt; 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.
   }
}

答案 1 :(得分:1)

我认为通过此

使距离滤镜有效
self.locationManager.distanceFilter = kCLDistanceFilterNone;

您可以开始更新位置方法,但也可以尝试这种方法,这两种方法都需要获得准确的位置

[locationManager startMonitoringSignificantLocationChanges];