Reputation: 199
I have an array NSMutableArray where I save an MKuserlocation type - locationArray. anyway now I want to get the data from this array and save it to an array from type CLLocationCoordinate2D. but since everything I save in locationArray is from id type how can I get the coordinates from this and save it to the second array?
CLLocationCoordinate2D* coordRec = malloc(pathLength * sizeof(CLLocationCoordinate2D));
for(id object in locationArray){
for (int i = 0; i < pathLength; i++)
?????
I dont know if this even possible!
Thanks
Upvotes: 1
Views: 1905
Reputation: 39502
Why do you need a c-style array of CLLocationCoordinate2D objects?
Here you go:
NSArray* userLocations; // contains your MKUserLocation objects...
CLLocationCoordinate2D* coordinates = malloc( userLocations.count * sizeof( CLLocationCoordinate2D) );
for ( int i = 0 ; i < userLocations.count ; i++ )
{
coordinates[i] = [[[userLocations objectAtIndex: i] location] coordinate];
}
Upvotes: 1
Reputation: 438162
The typical solution is to create a NSObject
subclass and define a single property, a CLLOcationCoordinate2D
. Instantiate and add those objects to your array.
@interface Coordinate : NSObject
@property (nonatomic) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end
@implementation Coordinate
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self) {
_coordinate = coordinate;
}
return self;
}
@end
And then, because your locationArray
is an array of MKUserLocation
(which, itself, conforms to MKAnnotation
), you can do:
NSMutableArray *path;
path = [NSMutableArray array];
for (id<MKAnnotation> annotation in locationArray)
{
// determine latitude and longitude
[path addObject:[[Coordinate alloc] initWithCoordinate:annotation.coordinate]];
}
Or make an array of existing object type, such as CLLocation
or MKPinAnnotation
or whatever.
Or if this array is a path to be drawn on the map, you might want to avoid using your own array, and instead make a MKPolyline
.
NSInteger pathLength = [locationArray count];
CLLocationCoordinate2D polylineCoordinates[pathLength]; // note, no malloc/free needed
for (NSInteger i = 0; i < pathLength; i++)
{
id<MKAnnotation> annotation = locationArray[i];
polylineCoordinates[i] = annotation.coordinate;
}
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:polylineCoordinates count:pathLength]
[self.mapView addOverlay:polyline];
It depends upon what the purpose of this is. But if you can use one of the previous constructs that avoids malloc
and free
, that's probably ideal. These techniques leverage Objective-C patterns which make it harder to leak, use an invalid pointer, etc.
Upvotes: 0
Reputation: 1427
Refering to Apple docs
You should certainly use CLLocationCoordinate2DMake
function
with data from MKUserLocation
or directly extract infos from MKUserLocation
:
object.location.coordinate // it's a CLLocationCoordinate2D from your 'object' example
or
CLLocationCoordinate2DMake(object.location.coordinate.latitude, object.location.coordinate.longitude)
Hope this help.
Upvotes: 0