0

In my app I am trying to make a table view order it self within distance order from the device! So lets say I have 20 rows in the table, they then go into distance order.

The only problem is, I am quite new to making apps so may need quite a detailed explanation! I have been trying to do this now for ages...

If someone assist what code and what I need to do in Xcode please do! Will be highly appreciated!!

any help?

4

1 回答 1

0

我强烈建议阅读 CLLocation(http://developer.apple.com/library/ios/#DOCUMENTATION/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html),因为它会为您提供您需要计算的所有信息出用户的当前位置。然后,一旦您有了当前位置,就可以使用该位置和每个位置的纬度/经度来计算距离。一旦您在对象上设置了距离,您就可以按对象上的属性(在您的情况下为俱乐部)对它们进行排序。

我正在使用以下代码段来计算从用户当前位置到另一个位置(在本例中为设施)的距离:

Facility *facility = [_facilities objectAtIndex:i];
CLLocation *facilityLocation = [[CLLocation alloc] initWithLatitude:[facility.latitude floatValue] longitude:[facility.longitude floatValue]];
if(facilityLocation){
    CLLocationDistance distance = [currentLocation distanceFromLocation:facilityLocation];
    double mileConversion = distance * 0.000621371192;

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.maximumFractionDigits = 1;
    facility.distanceFromCurrentLocation = [formatter numberFromString:[formatter stringFromNumber:[NSNumber numberWithDouble:mileConversion]]];
}

不久前,我从某处获得了英里转换,这是因为一英里有 1,609.344 米。如果您只需要仪表,则可以排除该转换。另请注意,我仅使用 1 个小数来显示 UI。

于 2012-07-18T20:52:11.347 回答