如何根据远程文件中的位置检查我的当前位置 - iOS应用程序

时间:2012-12-02 06:46:01

标签: ios xcode gps

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

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

如何每隔1分钟查看locations.txt以查看用户是否位于某个位置的300米范围内?

1 个答案:

答案 0 :(得分:2)

此问题的标准答案为Shape-Based Regions,如位置感知指南中所述。通常,如果区域数量有限,则基于形状的区域是可行的方法。但是,鉴于您需要很多地区,您可能需要“自己动手”:

  • 开启位置服务并监控您的位置。请参阅Location Awareness Programming Guide。如果您使用标准位置服务,请务必设置尽可能低的desiredAccuracy以满足功能需求(例如kCLLocationAccuracyHundredMeters)。

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

  • 您可以遍历您的位置数组进行监控(我将它们转换为CLLocation个对象),然后只使用distanceFromLocation

但有几点意见:

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

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

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

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


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

CLLocationManagerDelegate

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

[
  {
    "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 test project on GitHub。这是一个准系统实现,但它可以让您了解手头的工具,即检索// 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]; } 文件并将其与设备检索的位置进行比较。