根据位置iOS在UIPickerView中自动选择值

时间:2012-06-19 04:20:25

标签: objective-c ios5 core-location

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

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

我所拥有的是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文件中的任何位置都在几英里之内,然后它将自动选择一个阀门。 UIPickerView。我已经知道如何从UIPickerView中选择一些东西。

2 个答案:

答案 0 :(得分:1)

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

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

CLLocationDistance也可以替换为double。这在iOS开发人员库here

中列出

答案 1 :(得分:0)

我想通了这个数组就像这样

    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];
    }
        }