我有一个简单的位置 map ,我想让我的应用程序在用户接近远程文件中列出的位置时发出蜂鸣声
列出的位置在我名为 locations.txt 的服务器上
我如何每 1 分钟检查一次 locations.txt 以查看用户是否在某个位置的 300 米范围内??
最佳答案
这个问题的标准答案是Shape-Based Regions如位置感知指南中所述。通常,如果区域数量有限,则基于形状的区域是可行的方法。但是,鉴于您需要很多区域,您可能必须“自己动手”:
开启定位服务并监控您的位置。查看Location Awareness Programming Guide .如果您使用标准定位服务,请确保设置尽可能低的
desiredAccuracy
以满足功能需求(例如kCLLocationAccuracyHundredMeters
)。一旦您成功收到第一个
didUpdateLocations
,如果你真的想每分钟检查一次,你可以在那个时候创建一个计时器。如果该计时器的目的只是检查用户的位置,那么实际上不需要计时器,您可以等待didUpdateLocations
的出现。您可以遍历要监视的位置数组(我会将它们转换为
CLLocation
对象)并简单地使用distanceFromLocation
.
不过,有几点观察:
您建议您每分钟检查一次
locations.txt
以查看用户是否在 300 米的位置范围内。我可以想象您提出该解决方案的两个原因:服务器的
locations.txt
有变化吗? 如果这是您要解决的问题,更好的解决方案是 push notifications (又名“远程通知”)并且您想确保客户可以访问最新信息。不断重新检索文件的过程非常昂贵(在带宽、电池、计算方面);或用户移动了吗?如果您担心用户是否移动了,正确的解决方案不是每分钟检查一次,而是等待 [
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];
}
实现可能类似于 this test project on GitHub .这是一个准系统实现,但它让您了解手头的工具,即检索您的 locations.json
文件并将其与设备检索到的位置进行比较。
关于ios - 如何根据远程文件中的位置检查我的当前位置 - iOS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13667062/