2

iOSアプリケーションを実装していて、マップ上のいくつかの指定された座標の間にポリラインを描画したいと思います。

コードを書いて、無限遠点に到達するポイントからポリラインを描画しました。言い換えれば、線の始点は私の与えられた緯度と経度の点から始まりますが、線の終点は無限であり、他の点ではありません。

これは私のコードです...

NSMutableArrayと呼ばれる場所に座標を入力しましたrouteLatitudes。配列セルは、緯度用と経度用に1つずつ埋められています。

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [routeLatitudes count]); 

for(int idx = 0; idx < [routeLatitudes count]; idx=idx+2)
{
    CLLocationCoordinate2D workingCoordinate;       
    workingCoordinate.latitude=[[routeLatitudes objectAtIndex:idx] doubleValue];
    workingCoordinate.longitude=[[routeLatitudes objectAtIndex:idx+1] doubleValue];  
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
    pointArr[idx] = point;      
}   

// create the polyline based on the array of points. 
routeLine = [MKPolyline polylineWithPoints:pointArr count:[routeLatitudes count]];
[mapView addOverlay:self.routeLine];
free(pointArr);

とオーバーレイデリゲート

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
   MKOverlayView* overlayView = nil;

  if(overlay == routeLine)
  {
    self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine]        autorelease];
    self.routeLineView.fillColor = [UIColor colorWithRed:51 green:51 blue:255  alpha:1];
    self.routeLineView.strokeColor = [UIColor colorWithRed:204 green:0 blue:0 alpha:1];
    self.routeLineView.lineWidth = 3;

    overlayView = routeLineView;
  }
return overlayView;
}

そのため、マップ上のポイント間に線を引く必要があります。行の先頭は最初にドロップされたピンであり、最後は最後にドロップされたピンにあります。

4

1 に答える 1

4

コードによると、routeLatitudes配列には次のようにリストされたオブジェクトがあります。

インデックス 0: ポイント 1 の緯度
インデックス 1: ポイント 1 の経度
インデックス 2: ポイント 2 の緯度
インデックス 3: ポイント 2 の経度
インデックス 4: ポイント 3 の緯度
インデックス 5: ポイント 3 の経度
...

が 6 の場合routeLatitudes.count、実際には 3 ポイントしかありません。

これは、mallocが間違った数のポイントを割り当てており、polylineWithPoints呼び出しでもオーバーレイに間違った数のポイントを指定していることを意味します。

もう 1 つの問題はpointArr、オブジェクトの半分しか含まれrouteLatitudesないため、両方の配列に同じインデックス値を使用できないことです。

forループ インデックス カウンターidxは、反復ごとに 2 ずつインクリメントされます。これは、ポイントがそのように配置されているためですがrouteLatitudesidx設定には同じ値が使用されているためですpointArr

したがって、 foridx=0pointArr[0]設定されますが、 foridx=2pointArr[2]( の代わりにpointArr[1]) 設定されます。これは、他のすべての位置pointArrが初期化されないままになることを意味し、その結果、行が「無限に進む」ことになります。

したがって、修正されたコードは次のようになります。

int pointCount = [routeLatitudes count] / 2;
MKMapPoint* pointArr = malloc(sizeof(MKMapPoint) * pointCount);

int pointArrIndex = 0;  //it's simpler to keep a separate index for pointArr
for (int idx = 0; idx < [routeLatitudes count]; idx=idx+2)
{
    CLLocationCoordinate2D workingCoordinate;       
    workingCoordinate.latitude=[[routeLatitudes objectAtIndex:idx] doubleValue];
    workingCoordinate.longitude=[[routeLatitudes objectAtIndex:idx+1] doubleValue];  
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
    pointArr[pointArrIndex] = point;
    pointArrIndex++;
}   

// create the polyline based on the array of points. 
routeLine = [MKPolyline polylineWithPoints:pointArr count:pointCount];
[mapView addOverlay:routeLine];
free(pointArr); 

また、行に注意してmalloc、に修正sizeof(CLLocationCoordinate2D)しました sizeof(MKMapPoint)。これらの 2 つの構造体はたまたま同じ長さであるため、これは技術的には問題を引き起こしていませんでしたが、それがsizeof(MKMapPoint)配列に含まれるものであるため、使用するのは正しいことです。

于 2012-08-16T11:29:30.557 に答える