gpt4 book ai didi

ios - 需要访问包含 CLLocation 信息的 NSArray 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:47 25 4
gpt4 key购买 nike

我是 objective-c 的新手,过去 2 个半小时一直在努力解决这个问题。到目前为止,我已经取得了一些成功,我的应用程序可以在应用程序启动后立即开始搜索用户的位置。

然后我根据这个建议为 CLLocationManager 设置了一个委托(delegate):

“添加声明此类 (ViewController.h/m) 正在采用此特定协议(protocol)的语法。默认情况下,创建此类实例的任何其他类也将自动采用该协议(protocol)。

@interface ViewController : UIViewController <CLLocationManagerDelegate>

上面列出的代码行显示了用于显示 ViewController 采用 CLLocationManagerDelegate 协议(protocol)的语法。我们只需将它添加到标题中 @interface 行的尖括号 <> 之间。”

所以我成功地将上面的行添加到我的 ViewController.m 和 ViewController.h 文件中,然后我也遵循了相同的教程建议:

“完整的方法是:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 

并且在实现时它会告诉委托(delegate)人新的位置数据可用。我们对这个方法有两个论点。第一个让您知道哪个 CLLocationManager 提供了更新,最后一个提供了存储在 NSArray 中的 CLLocation 信息。”

下面是我所有的 ViewController.h 代码,接下来是我的 ViewController.m 代码:​​

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (nonatomic, strong) IBOutlet UILabel *gpsLabel;


-(IBAction)gpsButton;


@end

这是我的 ViewController.m 代码:​​

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>


@property (nonatomic, strong) CLLocationManager * gpsLM;

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

@end

@implementation ViewController


- (void)viewDidLoad
{

[super viewDidLoad];

self.gpsLM = [[CLLocationManager alloc]init];

[self.gpsLM startUpdatingLocation];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(IBAction)gpsButton{

}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

}

@end

我对从这里去哪里感到困惑。假设到目前为止我什至正确地做所有事情(是吗?),那么我如何访问存储在名为 locations 的 NSArray 对象中的位置数据?

谢谢你的帮助。

最佳答案

欢迎来到 iOS 社区!

首先,您可能会发现查看 CLLocationManagerDelegate 会很有帮助引用,在 locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 方法的标题下。这里重要的一点是:

locations: An array of CLLocation objects containing the location data. This array always contains at least one object representing the current location. If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries. The objects in the array are organized in the order in which they occurred. Therefore, the most recent location update is at the end of the array.

重要的是,当用户的位置发生变化时,系统会调用此方法。

因此,例如,当位置更新时,您可以通过将您的实现更改为类似以下内容来向您的控制台打印一条消息:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *currentLocation = [locations lastObject];
NSLog(@"Location is: %.5f %.5f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}

如果你想做一些事情来响应用户事件,你可以使用 manager.location,它会在每次 CLLocationManager 检测到新位置时自动更新。 (如果不存在,您需要向您的类添加另一个实例变量以存储最近的位置,并在 locationManager:didUpdateLocations: 中更新它。)

因此,例如,如果您想在按下按钮时使用当前位置更新您的标签,您可以添加如下内容:

-(IBAction)gpsButton {
CLLocation *currentLocation = self.gpsLM.location;
self.gpsLabel.text = [NSString stringWithFormat:@"Location is: %.5f, %.5f"];
}

(注意:这假定 gpsButton 操作和 gpsLabel socket 已连接到 Interface Builder 中的图形化内容。)

如果您熟悉其他编程语言,这就是推与拉的区别。 CLLocationManager 提供了一个push模型(它调用你的locationManager:didUpdateLocations:方法来立即通知你变化),还有一个pull模型(你可以随时通过 .location 向它询问最新位置)。您使用哪一个取决于您想要做什么。

关于ios - 需要访问包含 CLLocation 信息的 NSArray 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20715152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com