【发布时间】:2011-04-22 13:39:34
【问题描述】:
这不是一个编码问题,如果你愿意,它只是我试图实现的概念(alogirthm)。
基本上我想要做的是检查用户是否在一个特定的地方,即英国博物馆!然后给他们发消息说欢迎来到英国博物馆。
我的数据库中只有三个位置
- 英国博物馆
- 杜莎夫人蜡像馆
- 威斯敏斯特教堂
我知道我需要使用实时检查!所以我想我得到用户的位置,检查它是否匹配任何三个值,然后发送消息。你能提出更好的建议吗?谢谢
【问题讨论】:
这不是一个编码问题,如果你愿意,它只是我试图实现的概念(alogirthm)。
基本上我想要做的是检查用户是否在一个特定的地方,即英国博物馆!然后给他们发消息说欢迎来到英国博物馆。
我的数据库中只有三个位置
我知道我需要使用实时检查!所以我想我得到用户的位置,检查它是否匹配任何三个值,然后发送消息。你能提出更好的建议吗?谢谢
【问题讨论】:
是的,这听起来会奏效。请务必检查半径内的位置匹配(位置永远不会完全相同),即
if ( dist ( userCoordinate - museumCoordinate) < THRESHOLD ) ...
(使用distanceFromLocation:方法来计算这个)
另外,为了获得实时检查,您可能希望使用CLLocationManager 的委托回调在用户位置更改时接收更新。
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
// The location has changed to check if they are in one of your places
}
【讨论】:
你想要的叫做geocoding。通常是通过向实际为您进行转换的服务发送请求来完成的。 Google 和其他一些公司提供这项服务。
以下可作为代码参考
-(CLLocationCoordinate2D) addressLocation:(NSString *)input {
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
【讨论】: