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

标签 ios iphone objective-c cllocationmanager cllocation

我是 Objective-C 的新手,在过去的两个半小时里一直在努力解决这个问题。到目前为止,我取得了一些成功,我的应用程序一启动就可以开始搜索用户的位置。

然后,我根据以下建议为 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 导出已连接到 Interface Builder 中的图形方式。)

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

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

相关文章:

ios - 如何使用 iOS 获取 UIKeyboard 大小

ios - 使用Core Data轻量级迁移考虑多个数据模型版本

iphone - iOS 当应用程序位于前台时,我可以使用本地通知播放声音吗

ios - 图片未按JSON上的base64字符串发布

ios - 嵌入 2 个动态 UITableView

iphone - IOS isEqualToString 不工作

ios - 除非另有说明,否则所有 iOS GameCenter 回调都在主线程上执行吗?

ios - 要从主应用程序发送到键盘扩展的图像

ios - UIScreen.mainScreen 在所有设备上大小相同

ios - 在 View Controller 关闭时取消或销毁循环后台线程