1

MKuserlocation タイプ - locationArray を保存する配列 NSMutableArray があります。とにかく、この配列からデータを取得し、CLLocationCoordinate2D 型の配列に保存したいと考えています。しかし、locationArray に保存するものはすべて id タイプのものなので、これから座標を取得して 2 番目の配列に保存するにはどうすればよいですか?

  CLLocationCoordinate2D* coordRec = malloc(pathLength * sizeof(CLLocationCoordinate2D));
    for(id object in locationArray){
        for (int i = 0; i < pathLength; i++)
            ?????

これが可能かどうかはわかりません!

ありがとう

4

3 に答える 3

0

一般的な解決策は、サブクラスを作成し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

そして、 yourlocationArrayMKUserLocation(それ自体が に準拠する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 パターンを活用します。

于 2013-05-20T12:52:30.693 に答える
0

Appleドキュメントの参照

CLLocationCoordinate2DMakeからのデータで関数を使用するMKUserLocationか、次の情報を直接抽出する必要がありMKUserLocationます。

object.location.coordinate // it's a CLLocationCoordinate2D from your 'object' example

また

CLLocationCoordinate2DMake(object.location.coordinate.latitude, object.location.coordinate.longitude)

この助けを願っています。

于 2013-05-20T13:04:14.533 に答える