Search code examples
ioscocoa-touchcllocationmanagercllocationdistance

Notify when the user moved every 50/100 meters iOS


Hi i want to call the webservice whenever there is a location change of 50 meters(not less than this). I have tried using significant changes but it works for minimum 500 meters and startupdatelocations will call all the time. So How can i detect if the device moved 50 or 100 meters from the location.

I have used the distance filter as 50 meters as said in many stackoverflow questions. But it doesnt work before moving to 50 meters i got the location updates in device.

Here some one explained about distance filter - iphone core location: distance filter how does it work?


Solution

  • It's Very Easy.

    First Write in your ViewDidload Method To alloc CLLocationManager.

    Here i set 50M distance .

    locationManager = [[CLLocationManager alloc] init];
    
        //there will be a warning from this line of code
        [locationManager setDelegate:self];
    
        //and we want it to be as accurate as possible
        //regardless of how much time/power it takes
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    
        //set the amount of metres travelled before location update is made
        [locationManager setDistanceFilter:50];
        [locationManager requestAlwaysAuthorization];
        [locationManager requestWhenInUseAuthorization];
        [locationManager startUpdatingLocation];
    

    So Every 50 Meter change Device This Method is called :

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        if (locations.count > 0) {
            CLLocation *location = locations.lastObject;
            User_latitude = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
            User_longitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
            NSLog(@"latitude = %f",location.coordinate.latitude);
            NSLog(@"longitude = %f",location.coordinate.longitude);
            [self webservice_UpdateLocation];
        }
    }