如何从 iPhone 获取用户的位置?

发布于 2024-11-03 01:05:53 字数 255 浏览 5 评论 0 原文

这不是一个编码问题,如果您愿意的话,这只是我试图实现的概念(算法)。

基本上我想做的是检查用户是否在某个特定的地方,即大英博物馆!然后给他们发一条消息说欢迎来到大英博物馆。

我的数据库中只有三个位置

  • :英国博物馆、
  • 杜莎夫人蜡像馆、
  • 威斯敏斯特修道院

、我知道我需要使用实时检查!所以我想我获取用户位置,检查它是否匹配任何三个值,然后发送消息。你能提出更好的建议吗?谢谢

this is not a coding question, its just the concept im trying to achieve(alogirthm) if you like.

basically what i want to do is check if a user is in a particular place i.e the british meseum!! and then sending them a message saying welcome to the british meseum.

i only have three location in my DB

  • the british meuseum
  • the madamme tussaud
  • westminster abbey

i know that i need to use real time checking! so i was thinking that i get the users location, check to see if it matches any three values, and then send message. can you suggest anything better? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏花。依旧 2024-11-10 01:05:53

是的,这听起来像是可行的。请务必检查半径内的位置匹配(位置永远不会完全相同),即

if ( dist ( userCooperative - MuseumCooperative)

(使用 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
 }

Yea, that sounds like it would work. Just be sure to check the location matches within radius (the location will never be exactly the same) i.e.

if ( dist ( userCoordinate - museumCoordinate) < THRESHOLD ) ...

(use the distanceFromLocation: method To calculate this)

Also, to get the real time checking, you might want to use the delegate callback of the CLLocationManager to receive updates when the users location changes.

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

     // The location has changed to check if they are in one of your places
 }
才能让你更想念 2024-11-10 01:05:53

您想要的称为地理编码。通常是通过向实际为您执行转换的服务发送请求来完成的。 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;
}

What you want to is called geocoding. Typically is done by sending a request to a service which actually does the conversion for you. Google and some other companies offer this service.

Below can be used as code reference

-(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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文