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

发布于 2024-11-04 04:19:10 字数 1724 浏览 0 评论 0原文

我在 plist 中有一长串带有纬度和经度的地点。我想显示仅在用户当前位置 X 距离内的地点的 tableView。有没有办法从纬度和纬度创建对象?在 plist 文件中长整,以便我可以使用“distanceFromLocation”?更重要的是,如何让数组只显示与当前距离小于 X 的名称?我假设我需要用纬度和纬度制作一系列物体。在 plist 中长整型,然后如果对象 distanceFrom 小于 X,则在数组中执行一个对象,对吗?

请帮忙。

这就是我现在所在的位置:我在 double ClubLatitude 线上收到错误

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

I have a long list of places with Latitudes and Longitudes in a plist. I want to show a tableView of the places only within X distance of the user's current location. Is there a way to create objects from the lats & longs in the plist file so I can use 'distanceFromLocation'? More importantly, how do I get the array to only display the names with a distance from current less than X? I'm assuming I would need to make a series of objects from lats & longs in the plist, then do an objects in array if objects distanceFrom is less than X, correct?

Please help.

Here's where I am now: I get an error on the double clubLatitude line

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染墨丶若流云 2024-11-11 04:19:10

正如 @DavidNeiss 所说,你必须迭代列表(一个以 plist 作为源的 NSArray),它会是这样的:

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
}

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

As @DavidNeiss said, you have to iterate over the list (an NSArray with the plist as source) and it would be something like this:

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
}

Then you can do what you want (removing what's far from the user or whatever).

指尖上得阳光 2024-11-11 04:19:10

读入您的 plist,对其进行迭代以提取 X 距离内的内容,并用它们填充任何数组,这些数组将成为您的表视图的数据源?

Read in your plist, iterate over it to pull out ones within X distance and populate any array with them that will be the data source for your table view?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文