1

我有一个简单的位置图,当用户接近远程文件中列出的位置时,我想让我的应用程序发出哔哔声

列出的位置在我的名为 locations.txt 的服务器上

我如何每 1 分钟检查一次 locations.txt 以查看用户是否在某个位置的 300m 范围内?

4

1 回答 1

2

这个问题的标准答案是位置感知指南中描述的基于形状的区域。通常,如果您的区域数量有限,则可以选择基于形状的区域。但是,鉴于您想要很多区域,您可能必须“自己动手”:

  • 打开定位服务并监控您的位置。请参阅位置感知编程指南。如果您使用标准位置服务,请确保将 a 设置desiredAccuracy为尽可能低以实现功能需求(例如kCLLocationAccuracyHundredMeters)。

  • 一旦你成功收到第一个didUpdateLocations,如果你真的想每分钟检查一次,你可以在那个时候创建​​一个计时器。如果该计时器的目的只是检查用户的位置,那么实际上不需要计时器,您只需等待didUpdateLocations.

  • 您可以遍历要监视的位置数组(我会将它们转换为CLLocation对象)并简单地使用distanceFromLocation.

不过,有几点观察:

  • 您建议您locations.txt每分钟检查一次以查看用户是否在位置 300m 范围内。我可以想象您可能提出该解决方案的两个原因:

    • 服务器locations.txt变了吗?如果这是您要解决的问题,更好的解决方案是推送通知(也称为“远程通知”),并且您希望确保客户端可以访问最新信息。不断重新检索文件的过程非常昂贵(在带宽、电池、计算方面);或者

    • 用户移动了吗?如果您担心用户是否已移动,正确的解决方案不是每分钟检查一次,而是等待调用[ CLLocationManagerDelegate] 实例方法。didUpdateLocations如果您想避免过多的冗余检查,您可以随时跟踪最后一个请求是否发生在不到一分钟之前,然后仅在超过一分钟之前再次检查。但这与每分钟检查你是否需要有很大不同。

  • 您建议使用文本文件。您可能想要考虑使用 JSON 文件(或 XML 文件),这是从服务器检索数据的更好机制。


例如,如果您有一个 JSON 格式的文本文件,您可以在另一行代码 ( JSONObjectWithData) 中解析结果。为了说明,让我向您展示 JSON 文件的外观(其中方括号表示数组,大括号表示字典,因此这是一个字典数组):

[
  {
    "name" : "Battery Park",
    "latitude" : 40.702,
    "longitude" : -74.015
  },
  {
    "name" : "Grand Central Station",
    "latitude" : 40.753,
    "longitude" : -73.977
  }
]

然后,您的应用程序可以通过两行非常轻松地检索结果:

NSData *locationsData = [NSData dataWithContentsOfURL:url];
NSArray *locationsArray = [NSJSONSerialization JSONObjectWithData:locationsData options:0 error:&error];

因此,您需要启动位置服务:

if (nil == self.locationManager)
   self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

// Set a movement threshold for new events.
self.locationManager.distanceFilter = 500;

[self.locationManager startUpdatingLocation];

然后,您将有一个检查当前位置的例程:

- (void)checkLocation
{
    NSURL *url = [NSURL URLWithString:kLocationsUrlString];
    NSData *locationsData = [NSData dataWithContentsOfURL:url];
    NSAssert(locationsData, @"failure to download data"); // replace this with graceful error handling

    NSError *error;
    NSArray *locationsArray = [NSJSONSerialization JSONObjectWithData:locationsData
                                                              options:0
                                                                error:&error];
    NSAssert(locationsArray, @"failure to parse JSON");   // replace with with graceful error handling

    for (NSDictionary *locationEntry in locationsArray)
    {
        NSNumber *longitude = locationEntry[@"longitude"];
        NSNumber *latitude = locationEntry[@"latitude"];
        NSString *locationName = locationEntry[@"name"];

        CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue]
                                                          longitude:[longitude doubleValue]];
        NSAssert(location, @"failure to create location");

        CLLocationDistance distance = [location distanceFromLocation:self.locationManager.location];

        if (distance <= 300)
        {
            NSLog(@"You are within 300 meters (actually %.0f meters) of %@", distance, locationName);
        }
        else
        {
            NSLog(@"You are not within 300 meters (actually %.0f meters) of %@", distance, locationName);
        }
    }
}

当用户的位置发生变化时,这将被调用:

// this is used in iOS 6 and later

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [self checkLocation];
}

// this is used in iOS 5 and earlier

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0)
        [self checkLocation];
}

实现可能看起来像GitHub 上的这个测试项目。这是一个准系统实现,但它让您了解手头的工具,即检索locations.json文件并将其与设备检索的位置进行比较。

于 2012-12-02T17:55:02.857 回答