Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager tracks wrong Location (Track Me)

I am implementing Track me option in my code.CLLocationManager is not working as expected.when I start app remain at the same position ,CLLocationManager changes around 20-30 meters with-in 1 min minutes..then I remain Constant.

And If i change my position to track same thing happen in starting 1 min CLLocationManager moves 20-30 min extra then moves with my speed..

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

    }

}
like image 204
Christien Avatar asked May 28 '26 16:05

Christien


2 Answers

I faced the same problem in my current Pedometer app. I stretched, banged my head for couple of days. Then I found out that CLLocationManager is not able to track <5 meter distance and location generate updates. I kept self.locationManager.distanceFilter =2.0; and it gave me location updates even device was stationary. So I just changed distancefilter to 5.0 meter and it started working great. Try taking 5 meters it should work, I tested and all my wrong notifications issues vanished:

  self.locationManager.distanceFilter =5.0;

You are taking self.locationManager.distancefilter=0.0001 which is I suppose out of capacity for CLLocationManager to track such a minor movement. Also you need to filter out old locations i.e. cached location updates as mentioned in Location Awareness Guide by Apple. I have used this condition in my code to filter all events which 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.
   }
}
like image 195
rohan-patel Avatar answered May 30 '26 04:05

rohan-patel


I think give the distance filter effective with this

self.locationManager.distanceFilter = kCLDistanceFilterNone;

and you can start updating location method but also try this method these both methods are required to get exact location

[locationManager startMonitoringSignificantLocationChanges]; 
like image 45
Bhavesh Lathigara Avatar answered May 30 '26 05:05

Bhavesh Lathigara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!