2013-06-05 22 views
-1

我有一个包含位置的表格视图,并且当我按下按钮时,我想在附近的位置对它们进行过滤。我在想,当我按下按钮时,应用程序可以重新加载附近位置的表格视图,但我无法弄清楚如何进行过滤。UITableView通过按下按钮排序内容

+0

你想排除“过滤器”的位置,如果他们不在附近?或者你只是想显示他们从最近到最远排序?最近可以完成'NSArray'的方法'sortedArrayUsingComparator:' –

+0

对不起,我表达自己错了我想按最近的位置排序 – darkjuso

回答

1

假设你有CLLocationNSArray的对象,姑且称之为locationsCLLocation *currenLocation代表您的当前位置。

NSArray *sortedLocations = [locations sortedArrayUsingComparator:^(id obj1, id obj2) { 
     CLLocationDistance distance1 = [currentLocation distanceFromLocation: (CLLocation *)obj1]; 
     CLLocationDistance distance2 = [currentLocation distanceFromLocation: (CLLocation *)obj2]; 

     return distance1 - distance2; 
    }]; 

编辑:您可以使用CLLocationManager对象通过委托方法来获取用户的位置的频繁更新。你可以从startUpdatingLocation开始。检查this为您提供更多信息。

如果您有地理位置,您可以创建一个CLLocation,编号为– initWithLatitude:longitude:

+0

你可以添加数组和当前位置的代码吗? – darkjuso

+0

你是指实例化它们的代码吗? –

+0

是的,请在地图应用程序上使用CLLocationCoordinate2D,但我不知道该为谁做这个 – darkjuso