单击按钮时 CLLocation 管理器不断获得相同的纬度和经度?

发布于 2024-11-03 18:22:29 字数 2787 浏览 1 评论 0原文

这是我在这个网站上的第一个问题。

我有这个严重的问题...我将从头开始解释这一点...

在我的应用程序中,当用户单击应用程序中的按钮时,我需要获取用户的当前位置..但问题是何时单击在按钮上,它不会更新到当前位置,而是会获取以前的位置。但是当我重置 iPhone 应用程序中的位置警告时,它会获得正确的位置。

这是我为此应用程序执行的代码步骤,用于获取用户的当前位置...

首先我导入到应用程序...

然后我使用全局文件来保存应用程序的数据,因为我需要通过应用。

所以我在 globle.m 和 .h 文件中所做的是...

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;
}

然后在我的另一个视图控制器中放置 CLLocationManagerDelegate ,在 .m 文件中

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

我将标签放置到视图控制器中,然后去不同的地方获取纬度和经度..但它总是获得相同的纬度和经度...但是当我重置位置警告并再次运行应用程序时,它需要正确的纬度和经度...所以如果我需要获取当前位置,我总是必须重置它。但我需要的是每次单击搜索按钮时获取当前位置...

任何人都可以说出这段代码中有什么问题并且任何人都可以帮助我...

并且对于我糟糕的英语也非常非常抱歉。 ..:)

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 wrong in this code and can any one help me ....

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽梦忆笙歌 2024-11-10 18:22:29

LocationManager 将返回先前的位置,因为它会尝试尽可能快地返回,并且它认为该位置可能足够好。我通常会检查新位置的时间戳以确保它是新位置。如果它太旧了,我不会阻止经理并等待下一个。

我建议您查看Apple提供的示例代码, https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007801

这段代码是从示例中复制的:

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

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;
}
戏蝶舞 2024-11-10 18:22:29

我错过了键入我的代码...在我发现我的应用程序开始完美工作之后...谢谢你们的大力帮助..

[_sharedGlobals.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

相反,我使用了

[_sharedGlobals.locationManager setDesiredAccuracy:kCLLocationAccuracyBestforNavigation];

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文