一般的な解決策は、サブクラスを作成しNSObject
、単一のプロパティを定義することCLLOcationCoordinate2D
です。これらのオブジェクトをインスタンス化して配列に追加します。
@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
そして、 yourlocationArray
はMKUserLocation
(それ自体が に準拠するMKAnnotation
) の配列であるため、次のことができます。
NSMutableArray *path;
path = [NSMutableArray array];
for (id<MKAnnotation> annotation in locationArray)
{
// determine latitude and longitude
[path addObject:[[Coordinate alloc] initWithCoordinate:annotation.coordinate]];
}
CLLocation
または、またはなどの既存のオブジェクト型の配列を作成しますMKPinAnnotation
。
または、この配列がマップ上に描画されるパスである場合、独自の配列を使用するのを避け、代わりに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];
これは、その目的が何であるかによって異なります。malloc
しかし、 andを回避する以前の構造の 1 つを使用できる場合、それはfree
おそらく理想的です。これらの手法は、リークや無効なポインターの使用などを困難にする目的の C パターンを活用します。