1

我有一个 NSMutableArray 存储 ID、纬度、经度等...我需要将用户的当前位置与下面数组中存储的项目的纬度和经度进行比较。

我知道如何获取用户的当前坐标,但我不知道如何访问数组中的坐标或如何比较距离。

该数组是一个名为 scrolledPast 的 NSMutableArray(见下文)。假设当前用户的坐标是 21.31,-157.86。我该怎么开始?任何指导将不胜感激。谢谢你的帮助!

array: (
        {
        key1 = 80;
        key2 = "11:34 PM";
        key3 = "place1";
        key4 = "21.3111656";
        key5 = "-157.8606953";
    },
        {
        key1 = 251;
        key2 = "11:34 PM";
        key3 = "place2";
        key4 = "21.310672";
        key5 = "-157.8611839";
    },
        {
        key1 = 79;
        key2 = "11:34 PM";
        key3 = "place3";
        key4 = "21.3106798";
        key5 = "-157.8612934";
    }
)

这是生成上述数组的代码:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

[dict setObject:placeId forKey:@"key1"];
[dict setObject:currentTime forKey:@"key2"];
[dict setObject:textForMyLabel forKey:@"key3"];
[dict setObject:placeLatitude forKey:@"key4"];
[dict setObject:placeLongitude forKey:@"key5"];

[scrolledPast addObject:dict];

NSLog(@"array: %@", scrolledPast);
4

3 回答 3

1

首先为每个商店创建一个模型对象,并在类中实现有用的方法:

商店.h:

@interface Store : NSObject

@property (strong, nonatomic) NSString *storeId;     // Don't use "id" as that is type in Objective-C
@property (assign, nonatomic) NSUInteger timeHour;
@property (assign, nonatomic) NSUInteger timeMinute;
@property (strong, nonatomic) NSString *label;
@property (assign, nonatomic) float lat;
@property (assign, nonatomic) float lng;    // not "long" as that is a type

- (id)initWithDictionary:(NSDictionary *)dict;
- (BOOL)isStoreNearLatitude:(float)lat longitude:(float)lng;

@end;

商店.m:

#import "Store.h"

@implementation Store
@synthesize storeId, timeHour, timeMinute, label, lat, lng;

- (id)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    if (self) {
        self.storeId = [dict objectForKey:@"key1"];
        // etc.

    }
    return self;
}

- (BOOL)isStoreNearLatitude:(float)lat longitude:(float)lng {
    // You will never get an exact match, so you will need to match the lat/long within
    // a certain tolerance.  You might want to pass it in so it can change at runtime...
    #define TOLERANCE 10.0f
    return
        fabs(self.lat - lat) < TOLERANCE &&
        fabs(self.lng - lng) < TOLERANCE;
}

@end

随着项目的进行,您会发现这个新Store对象变得越来越有用。

于 2013-10-06T09:58:58.990 回答
1

我将采取另一种方法来解决您的问题。例如,我将创建一个名为 Coordinate 的新对象来保存数据,例如:

#import "Coordinates.h"

@interface Coordinates ()
@property (nonatomic, strong) NSNumber *identifier;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, assign) CGPoint coordinate;

@end

@implementation Coordinates

- (id) initWithDictionary: (NSDictionary *) dict
{
    //Create and hydrate object
}

- (float) comparePointWithPoint: (CGPoint) point
{
    CGFloat xDist = (self.coordinate.x - point.x);
    CGFloat yDist = (self.coordinate.y - point.y);
    CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));

    return distance;

}

如果要保存坐标,可以使用 CLLocation 类而不是 CGPoint。

于 2013-10-06T10:02:27.203 回答
1

你有一个字典数组。您可以通过以下方式访问元素:

for (NSDictionary *dict in scrolledPast) {
    NSString *lat = dict[@"key4"];
    NSString *lng = dict[@"key5"];
    // compare distance here
}

为了比较距离,最好使用 CLLocation distanceFromLocation:(我想它在内部使用Haversine 公式)。

于 2013-10-06T10:05:55.850 回答