Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray of ClLocationCoordinate2D

Tags:

I'm trying to create then retrieve an array of CLLocationCoordinate2D objects, but for some reason the array is always empty.

I have:

NSMutableArray *currentlyDisplayedTowers; CLLocationCoordinate2D new_coordinate = { currentTowerLocation.latitude, currentTowerLocation.longitude }; [currentlyDisplayedTowers addObject:[NSData dataWithBytes:&new_coordinate length:sizeof(new_coordinate)] ]; 

I've also tried this for adding the data:

[currentlyDisplayedTowers addObject:[NSValue value:&new_coordinate withObjCType:@encode(struct CLLocationCoordinate2D)] ]; 

And either way, the [currentlyDisplayedTowers count] always returns zero. Any ideas what might be going wrong?

Thanks!

like image 250
Sina Avatar asked Feb 23 '11 18:02

Sina


People also ask

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

What is cllocationcoordinate2d?

The latitude and longitude associated with a location, specified using the WGS 84 reference frame.

What is NSMutableArray Objective C?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .

What is NSArray?

NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArrayRef . See Toll-Free Bridging for more information on toll-free bridging.


1 Answers

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]; 
like image 72
Anurag Avatar answered Jan 04 '23 03:01

Anurag