开发者

CLLocation manager keep getting same latitude and longitude when clicking button?

This is my first question in this site.

I have this serious problem.... I'll explain this from the beginning…

in my app i need to get the current location of the user when the user click on the button in the application.. but the problem is when is click on the button its not updating to the current location its getting the previous location. But when i reset the location warnings in the iphone app its get the correct location.

Here is the code steps i did for this application to get the current location of the user...

First I import to the application ...

then i am using global files to keep data of the application because i need to access them through the application.

so what I did in the globle.m and .h file is ...

CLLocationManager* locationManager;

@synthesize locationManager


+ (Globals*)sharedGlobals {
    @synchronized(self) {
        if(_sharedGlobals == nil) {
            _sharedGlobals = [[super allocWithZone:NULL] init];


   _sharedGlobals.locationManager = [[CLLocationManager alloc]init];
   [_sharedGlobals.locationManager   setDesiredAccuracy:kCLLocationAccuracyBest];

        }
    }

    return _sharedGlobals;
}

Then in my other view controller I put the CLLocationManagerDelegate and in the .m file

-(IBAction) didTapSearchbtn{

    if (![CLLocationManager locationServicesEnabled]) {


    }else {

        [[Globals sharedGlobals].geoLocations removeAllObjects];
        search.text = nil;
        [Globals sharedGlobals].fromTextField = NO;

        [[Globals sharedGlobals].locationManager setDelegate:self];
        [[Globals sharedGlobals].locationManager startUpdatingLocation];


    }


}


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

    if (newLocation.horizontalAccuracy < 0) return; 

    [[Globals sharedGlobals].locationManager setDelegate:nil];
    [[Globals sharedGlobals].locationManager stopUpdatingLocation];

    [[Globals sharedGlobals].geoLocations setObject:[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.latitude] forKey:@"geolat"];
    [[Globals sharedGlobals].geoLocations setObject:[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.longitude] forKey:@"geolong"];

    [self retriveDataFromInternet];

    [[Globals sharedGlobals].locationManager release];

}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    //GPS error

    [[Globals sharedGlobals].locationManager setDelegate:nil];
    [[Globals sharedGlobals].locationManager stopUpdatingLocation];


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"YumTable!", nil) message:NSLocalizedString(@"Enable Your GPS settings to get your current location", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil];
    [alert show];
    [alert release];

    [[Globals sharedGlobals].locationManager release];
}

i put the label to my view controller and went different places to take latitudes and longitudes .. but always it getting same latitude and longitude ... but when I reset the location warnings and run the app again it took the correct latitude and longitude ... so if i need to take current location always i have to reset it. But what i need is to get current location every time when i click the search button...

Can any one can say whats 开发者_StackOverflow中文版wrong in this code and can any one help me ....

And Also very very sorry about my bad english ... :)


The LocationManager will return the previos location because it tries to be as fast as possible and it thinks that this location might be good enough. I usually check the timestamp on the new location to ensure that it is a fresh one. If it is to old I don't stop the manager and wait for the next one.

I would suggest that you look at the sample code provided by Apple, https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007801

This code is copied from the example:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // test the age of the location measurement to determine if the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;
}


I missed Typed my code ... after I figur that out my application start to work perfectly ... Thank you guys for your great help..

[_sharedGlobals.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

instead of that I used

[_sharedGlobals.locationManager setDesiredAccuracy:kCLLocationAccuracyBestforNavigation];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜