将位置与NSMutableArray中的坐标进行比较

时间:2013-10-06 09:46:21

标签: iphone ios objective-c

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

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

该数组是一个名为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);

3 个答案:

答案 0 :(得分:1)

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

Store.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;

Store.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对象变得越来越有用。

答案 1 :(得分: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。

答案 2 :(得分:1)

你有一系列词典。您可以通过以下方式访问元素:

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

为了比较距离,最好使用CLLocation的distanceFromLocation:(我想在内部使用the Haversine formula)。