如何从位置管理器获取当前位置?

发布于 2024-09-11 01:59:22 字数 1517 浏览 9 评论 0原文

我希望我的应用程序每秒都能获取他所在的确切位置(经度和纬度),这可能吗?到目前为止,我有这段代码,应用程序每 3 秒获取一次当前位置,但如果我移动,它不会更新...

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    NSLog(@"lol");


    [locationManager stopUpdatingLocation];



        if (wtf == 2) {
            [[NSUserDefaults standardUserDefaults] setObject:newLocation forKey:@"old"];
            NSLog(@"wtf");
            wtf =0;


        }

    oldLocation = [[NSUserDefaults standardUserDefaults] objectForKey:@"old"];

        double rep = [oldLocation distanceFromLocation:newLocation];
        NSString *label1 = [NSString stringWithFormat:@"%2.90f",oldLocation.coordinate.latitude];
        NSString *label2 = [NSString stringWithFormat:@"%2.90f",newLocation.coordinate.latitude];

    if ([label1 isEqual:label2]) {
        NSLog(@"penis");
    }

    labelm.text = label1;
    labelkm.text = label2;


}



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

}

-(void)actualise{





    [locationManager startUpdatingLocation];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    wtf = 2;

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(actualise) userInfo:nil repeats:YES];
}
/*

i want my app to get the exact location (longitude and latitude) of where he is as every second, it is possible ? i have this code so far, every 3 second the app get the current location but it doesnt update if i move...

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    NSLog(@"lol");


    [locationManager stopUpdatingLocation];



        if (wtf == 2) {
            [[NSUserDefaults standardUserDefaults] setObject:newLocation forKey:@"old"];
            NSLog(@"wtf");
            wtf =0;


        }

    oldLocation = [[NSUserDefaults standardUserDefaults] objectForKey:@"old"];

        double rep = [oldLocation distanceFromLocation:newLocation];
        NSString *label1 = [NSString stringWithFormat:@"%2.90f",oldLocation.coordinate.latitude];
        NSString *label2 = [NSString stringWithFormat:@"%2.90f",newLocation.coordinate.latitude];

    if ([label1 isEqual:label2]) {
        NSLog(@"penis");
    }

    labelm.text = label1;
    labelkm.text = label2;


}



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

}

-(void)actualise{





    [locationManager startUpdatingLocation];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    wtf = 2;

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(actualise) userInfo:nil repeats:YES];
}
/*

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

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

发布评论

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

评论(1

风蛊 2024-09-18 01:59:22

使用此代码初始化并启动位置管理器:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];

并像这样实现didUpdateToLocation:

- (void) locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*) oldLocation 
{
   // This will be called every time the device has any new location information. 
}

不需要使用任何计时器。

Use this code to initialize and start the location manager:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];

And implement didUpdateToLocation like this:

- (void) locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*) oldLocation 
{
   // This will be called every time the device has any new location information. 
}

There is no need to use any timers.

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