sp309 February 2016

getting different location even if I am not moving

I am using below code for getting longitude and latitude , but it gives me diifferent values even if I am not moving from my place

 locationmanager=[[CLLocationManager alloc]init];
    geocoder=[[CLGeocoder alloc]init];

    locationmanager.delegate=self;
    locationmanager.desiredAccuracy=kCLLocationAccuracyBest;
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [locationmanager requestWhenInUseAuthorization];
    }
    [locationmanager startUpdatingLocation];



    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"error" message:@"Failed to get Location." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        CLLocation *currentlocation=newLocation;

    if (currentlocation!=nil)
    {

        longitude=currentlocation.coordinate.longitude;
        latitude=currentlocation.coordinate.latitude;

    }
         [geocoder reverseGeocodeLocation:currentlocation completionHandler:^(NSArray *placemarks, NSError *error) {

          } ];

    }

please guide me, I want same location, if I am not moving from my place.

Answers


bhavin ramani February 2016

In ViewController.h file:

@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager *location;
} 

In ViewController.m file:

- (void)viewDidLoad
{
     location = [[CLLocationManager alloc] init];
     location.delegate = self;
     [location requestWhenInUseAuthorization];
     [location requestAlwaysAuthorization];
     [location startUpdatingLocation];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *locati = [locations lastObject];
    float lat=locati.coordinate.latitude;
    float lon=locati.coordinate.longitude;
    NSLog(@"%f %f",lat,lon);

    [location stopUpdatingLocation];
}

In your info.plist add :

enter image description here


Satish A February 2016

Cellular network location gives an approximate area were the phone existing. It is based on the service providers tower.So current location will automatically changes a bit depending on network/gps location tracking/accuracy.

I recommend you to get location using distance filter, so until you are in that region your current address will remain same as location only updates for every X meters.

locationManager.distanceFilter = 100;//Your own distance

i.e.,

self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    _locationManager.delegate = self;
    // This is the most important property to set for the manager. It ultimately determines how the manager will
    // attempt to acquire location and thus, the amount of power that will be consumed.
    _locationManager.distanceFilter = 100;
    // Once configured, the location manager must be "started".
    [_locationManager startUpdatingLocation];

Hope this helps.

Post Status

Asked in February 2016
Viewed 2,400 times
Voted 6
Answered 2 times

Search




Leave an answer