2

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

我对 iOS 的 Objective 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 中选择一些东西。

4

2 回答 2

1

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

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

CLLocationDistance 也可以替换为double. 这在此处的 iOS 开发人员库中进行了概述

于 2012-06-19T13:52:09.687 回答
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];
    }
        }
于 2012-06-25T17:27:23.663 回答