I\'m trying to create then retrieve an array of CLLocationCoordinate2D objects, but for some reason the array is always empty.
I have:
NSMutableArray
For anyone else with this issue, there's another solution if you are planning on using MapKit
.
(The reason I say IF, of course, is because importing a module such as MapKit
purely for a convenient wrapper method is probably not the best move.. but nonetheless here you go.)
@import MapKit;
Then just use MapKit's coordinate value wrapper whenever you need to:
[coordinateArray addObject:[NSValue valueWithMKCoordinate:coordinateToAdd]];
In your example..
[currentlyDisplayedTowers addObject:[NSValue valueWithMKCoordinate:new_coordinate]];
You need to allocate your array.
NSMutableArray* currentlyDisplayedTowers = [[NSMutableArray alloc] init];
Then you can use it. Be sure to call release when you are done with it or use another factory method.
As SB said, make sure your array is allocated and initialized.
You’ll also probably want to use NSValue
wrapping as in your second code snippet. Then decoding is as simple as:
NSValue *wrappedCoordinates = [currentlyDisplayedTowers lastObject]; // or whatever object you wish to grab
CLLocationCoordinate2D coordinates;
[wrappedCoordinates getValue:&coordinates];
I had currentlyDisplayedTowers = nil
which was causing all the problems. Also, the previous advice to init and alloc were necessary. Thanks everyone for the help!
To stay in object land, you could create instances of CLLocation
and add those to the mutable array.
CLLocation *towerLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[currentDisplayedTowers addObject:towerLocation];
To get the CLLocationCoordinate
struct back from CLLocation
, call coordinate
on the object.
CLLocationCoordinate2D coord = [[currentDisplayedTowers lastObject] coordinate];