首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从CurrentLocation到Plist文件数据的距离

从CurrentLocation到Plist文件数据的距离
EN

Stack Overflow用户
提问于 2011-04-28 02:03:51
回答 2查看 350关注 0票数 0

我有一个长长的列表,上面有纬度和经度。我只想显示距离用户当前位置X距离内的地点的tableView。有没有办法从plist文件中的经纬度创建对象,这样我就可以使用'distanceFromLocation‘了?更重要的是,如何让数组只显示与当前距离小于X的名称?我假设我需要从plist中的经度和经度生成一系列对象,然后在对象distanceFrom小于X的情况下在数组中创建对象,对吗?

请帮帮忙。

这就是我现在所在的位置:我在double clubLatitude行上得到一个错误

代码语言:javascript
复制
- (void)viewDidLoad {
 [super viewDidLoad];

     NSArray *clubArray = [NSArray arrayWithObjects:[self danceClubLocation], nil];
     self.tableData = clubArray;
}

-(CLLocation *)danceClubLocation
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];

    NSEnumerator *e = [array objectEnumerator];
    id object;
    while ((object = [e nextObject])) {
        double clubLatitude = [[array valueForKey:@"Latitude"] doubleValue];
        double clubLongitude = [[array valueForKey:@"Longitude"] doubleValue];
        CLLocation *clubLocation = [[CLLocation alloc] initWithLatitude:clubLatitude longitude:clubLongitude];
        if ([clubLocation distanceFromLocation:myLocation]<=50) {
            return clubLocation;
        }
        else return nil;
    }
    return nil;
}

-(CLLocation *)myLocation
{
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];
    NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude];
    NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude];
    double myLatitudeD = [myLatitude doubleValue];
    double myLongitudeD = [myLongitude doubleValue];
    myLocation = [[CLLocation alloc]initWithLatitude:myLatitudeD longitude:myLongitudeD];
    return myLocation;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-28 02:34:37

正如@DavidNeiss所说,你必须遍历列表(一个以plist为源的NSArray ),它应该是这样的:

代码语言:javascript
复制
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"latlong" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];

NSEnumerator *e = [array objectEnumerator];
id object;
while (object = [e nextObject]) {
  // do something with object
}

然后你就可以做你想做的事情了(移除远离用户的东西或其他东西)。

票数 0
EN

Stack Overflow用户

发布于 2011-04-28 02:20:19

读入你的plist,迭代它以拉出X距离内的数组,并用它们填充任何将成为表视图数据源的数组?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5808513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档