【问题标题】:Auto select value in UIPickerView based on location iOS基于位置 iOS 在 UIPickerView 中自动选择值
【发布时间】:2012-06-21 02:22:28
【问题描述】:

我想设置一些位置并让我的应用程序检查它是否通过这些位置中的任何一个,如果用户是,那么我希望UIPickerView 自动选择我根据他们所在的位置设置的值。

我非常熟悉 Objective c 和 iOS 编码,但我真的没有对核心位置做过任何事情。我读过一些东西,但我需要一些帮助。

我拥有的是 plist 文件中位置的纬度和经度。我可以获取当前用户的位置并使用此代码获取坐标

  int degrees = newLocation.coordinate.latitude;
  double decimal = fabs(newLocation.coordinate.latitude - degrees);
  int minutes = decimal * 60;
  double seconds = decimal * 3600 - minutes * 60;
  NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                   degrees, minutes, seconds];

  degrees = newLocation.coordinate.longitude;
  decimal = fabs(newLocation.coordinate.longitude - degrees);
  minutes = decimal * 60;
  seconds = decimal * 3600 - minutes * 60;
  NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                     degrees, minutes, seconds];

我想做的是将用户当前位置与 plist 文件中的位置进行比较,如果它们与 plist 文件中的任何位置相距几英里,那么它将自动选择一个阀门UIPicker 视图。我已经知道如何从 UIPickerView 中选择一些东西了。

【问题讨论】:

  • 你读了什么?你试过什么?你在哪个部分有问题?获取位置、从位置列表中选择正确的值或设置选择器?
  • 我遇到的麻烦是获取用户的位置并将其与我设置的位置进行比较,看看他们是否在距该位置几英里的范围内。
  • 你应该重新表述你的问题 - 目前它太模糊和广泛。举例说明您在列表中拥有的位置数据(坐标、仅地名?)以及您从设备获得的位置。
  • 现在这是一个更好的问题!编辑做得很好。

标签: objective-c ios5 core-location


【解决方案1】:

您可以使用distanceFromLocation: 实例方法(在CLLocation 对象中找到)

CLLocation *myLocation = 'get the location'
CLLocation *storedLocation = 'get the stored location'
CLLocationDistance distance = [myLocation distanceFromLocation:storedLocation];

CLLocationDistance 也可以替换为double。 iOS 开发者库here

中对此进行了概述

【讨论】:

  • 好的,我知道它是如何工作的,我将如何通过一个数组运行它并找到 10 米以内的那个?
  • 因此将 plist 加载到 NSDictionary 中,我可以比较各个位置并查看它们的距离。比较我的字典中所有位置的最佳方法是什么?
【解决方案2】:

我发现我只是像这样循环了数组

    NSArray *locations = [_locationDict allKeys];
NSInteger count = [location count];
for (int i = 0; i < count; i++){

    NSString *locationName = [[NSString alloc] initWithFormat:@"%@",[locations objectAtIndex:i]];


NSDictionary *dict = [_locationDict objectForKey:locationName];
CLLocation *itemLocation = [[CLLocation alloc] initWithLatitude:[[dict objectForKey:@"latitude"] floatValue] longitude:[[dict objectForKey:@"longitude"] floatValue]];

CLLocationDistance distance = [itemLocation distanceFromLocation:newLocation];
    NSLog(@"%F",distance);

    if (distance < 100) {
        whatby.text = [locations objectAtIndex:i];
    }
        }

【讨论】:

    猜你喜欢
    • 2020-07-11
    • 1970-01-01
    • 2012-08-22
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    相关资源
    最近更新 更多