Reputation: 591
I have an NSMutableArray that stores IDs, latitudes, longitudes, etc... I have a requirement to compare the user's current location to the latitudes and longitudes of the items stored in the array below.
I know how to get the user's current coordinates, but I don't know how to access the coordinates in the array or how to compare the distances.
The array is an NSMutableArray called scrolledPast (see below). Lets say the current user's coordinates are 21.31,-157.86. How would I even start? Any guidance would be greatly appreciated. Thank you al for your wonderful help!
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";
}
)
Here is the code that generates the above array:
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);
Upvotes: 1
Views: 142
Reputation: 122391
First create a model object for each store and implement useful methods within the class:
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
You will find that this new Store
object becomes more and more useful as your project progresses.
Upvotes: 1
Reputation: 9006
You have an array of dictionaries. You can access elements in the following way:
for (NSDictionary *dict in scrolledPast) {
NSString *lat = dict[@"key4"];
NSString *lng = dict[@"key5"];
// compare distance here
}
For comparing distances it's best to use CLLocation's distanceFromLocation:
(I suppose that internally it uses the Haversine formula).
Upvotes: 1
Reputation: 185
I will take another approach to solve your problem. I will create a new object called Coordinate for example to save the data like:
#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;
}
You can use CLLocation class instead of CGPoint if you want to save the coordinates.
Upvotes: 1