31

iOS アプリの MKMapView に描画された MKPolyline からすべての緯度と経度のポイントを取得する方法を見つけようとしています。

MKPolyline が緯度と経度のポイントを保存しないことは知っていますが、MKPolyline がマップ上で接触する緯度と経度の配列を作成する方法を探しています。

誰かがこれについて具体的な解決策を持っていますか?

ありがとうございました

編集: 最初の応答 (ありがとう) を見た後、コードが何をしているかをよりよく説明する必要があると思います:

  1. 最初にMKDirections オブジェクトで" calculateDirectionsWithCompletionHandler " を呼び出します
  2. 「 polyline」プロパティを持つMKRouteオブジェクトが返されます。
  3. 次に、マップビューで「addOverlay」を呼び出し、MKRouteオブジェクトからポリラインを渡します

それで全部です。

だから、私はすでに私のために構築されたポリラインを持っています。だから私は何とかしてポリラインで見つかったすべてのポイントを取得し、それらを緯度と経度にマッピングしたいと思います...

4

2 に答える 2

51

からポリラインの座標を取得するにはMKRoute、 メソッドを使用しgetCoordinates:range:ます。
そのメソッドは継承元のMKMultiPointクラスにあります。MKPolyline

これは、作成者が作成したか、 が作成したかに関係なく、どのポリラインでも機能することを意味しますMKDirections

必要な座標の数を保持するのに十分な大きさの C 配列を割り当て、範囲を指定します (たとえば、0 番目から始まるすべてのポイント)。

例:

//route is the MKRoute in this example
//but the polyline can be any MKPolyline

NSUInteger pointCount = route.polyline.pointCount;

//allocate a C array to hold this many points/coordinates...
CLLocationCoordinate2D *routeCoordinates 
    = malloc(pointCount * sizeof(CLLocationCoordinate2D));

//get the coordinates (all of them)...
[route.polyline getCoordinates:routeCoordinates 
                         range:NSMakeRange(0, pointCount)];

//this part just shows how to use the results...
NSLog(@"route pointCount = %d", pointCount);
for (int c=0; c < pointCount; c++)
{
    NSLog(@"routeCoordinates[%d] = %f, %f", 
        c, routeCoordinates[c].latitude, routeCoordinates[c].longitude);
}

//free the memory used by the C array when done with it...
free(routeCoordinates);

ルートによっては、数百または数千の座標を用意してください。

于 2014-02-18T21:14:48.957 に答える