CLLocationManager tracks wrong location (Track Me)

I am using parameter Track me

in my code. CLLocationManager

does not work as expected. When I launch the app, stay in the same position, CLLocationManager

change by about 20-30 meters in 1 minute. then I remain constant.

And if I change my position to keep track of the same thing that happens at the beginning of 1 min. CLLocationManager

moves for 20-30 minutes and then moves at my speed.

Why is this happening..

 - (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];

    }

}

      

+3


source to share


2 answers


I faced the same issue in my current Pedometer app. I stretched, hit my head for a couple of days. Then I found out that I was CLLocationManager

not able to track distance up to 5 meters and location. I saved self.locationManager.distanceFilter =2.0;

and it gave me location updates, even the device was stationary. So I just changed the distance format to 5.0 meters and it started working fine. Try to take 5 meters, it should work, I tested and all my wrong notification questions disappeared:

  self.locationManager.distanceFilter =5.0;

      



You take self.locationManager.distancefilter=0.0001

, which, I believe, is not capable of CLLocationManager

tracking such minor movement. Also you need to filter out old locations, i.e. cached location updates, as mentioned in Apple's Location Awareness Guide . I used this condition in my code to filter all events that are older than 5 seconds.

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

      

+2


source


I find the distance filter is effective with this

self.locationManager.distanceFilter = kCLDistanceFilterNone;

      



and you can start updating the location method, but also try this method, both methods are needed to get the exact location

[locationManager startMonitoringSignificantLocationChanges]; 

      

+1


source







All Articles