1

現在地から目的地までのルートを表示したい。このコードhttp://code.google.com/p/octomapkitを取得し、いくつかのログ メッセージを追加しました。ルート座標 (103 付近) を適切に取得します。彼らは途中で、私の場所から目的地までのルートを埋めているので、Google の呼び出しと要素の解析は良好です。しかし、ポリラインの開始のみを表示するよりも MKMapView に表示したい場合。15または20のように、それ以上ではありません。

コードを上から下に投稿しようとします:

元のコードは最初の要素のみを取り、最後の要素を取得した場合、すべてのオーバーレイを追加するよりも何か他のものが表示されるのではないかと考えていました-それが for ループの理由です。

それ以外は : MKPolyline *polyLine = [self.mapView.overlays objectAtIndex:0];

#pragma mark MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *routeLineView = nil;
    // should take the objectAtIndex:0 , but for test I will check if it has more
    for(int i=0;  i <self.mapView.overlays.count; i++ ){

        MKPolyline *polyLine = [self.mapView.overlays objectAtIndex:i];       

        routeLineView = [[[MKPolylineView alloc] initWithPolyline:polyLine] autorelease];
        routeLineView.fillColor = [UIColor redColor];
        routeLineView.strokeColor = [UIColor redColor];
        routeLineView.lineWidth = 3;   
    }   

    return routeLineView;
}


-(void) routeLoadSucceededWithRoutePoints:(MKMapPoint*)routePoints count:(int)pointNumber {
    //NSLog(@"MKMapVew+OctoRoute.routeLoadSucceededWithRoutePoints count: %d", pointNumber);

    MKPolyline* routeLine = nil;
    routeLine = [MKPolyline polylineWithPoints:routePoints count:pointNumber];

    // add the overlay to the map
    if (nil != routeLine) {

        // added zoom support:
        if(shouldZoom){
            MKCoordinateRegion region = [self coordinateRegion];
            [self setRegion:region animated:YES];
        }


        //[self removeOverlays:self.overlays];
        [self addOverlay:routeLine];
    }
}

コメントされ//[self removeOverlays:self.overlays];ているかどうかに関係なく、それがさらに作成されることを望んでいました:) -しかしそうではありません。

内部mapPointCArrayFromJSONStringでは、座標が正しく表示されます。

-(void) jsonLoadSucceededWithData:(NSData*)loadedData {
    self.routeParser.jsonStr = [[[NSString alloc] initWithData:loadedData encoding:NSUTF8StringEncoding] autorelease];
    MKMapPoint *mapPointCArray = [self.routeParser mapPointCArrayFromJSONString];

    //NSLog(@"OctoRouteService.jsonLoadSucceededWithData : %d"+ mapPointCArray.);
    [delegate routeLoadSucceededWithRoutePoints:mapPointCArray count:[self.routeParser numberOfPoints]];
}

steps確かにコーディネートされています。何度もチェック。

-(MKMapPoint*) mapPointCArrayFromJSONString {   
    NSArray *steps = [self routeStepsArrayFromJSONString];

    //NSLog(@"OctoRouteParser.mapPointCArrayFromJSONString steps:%d ", steps.count);
    if(steps.count == 0){
        return nil;
    }

    MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D) * [steps count]*2 -1);
    numberOfPoints = [steps count]-1;

    int index=0;
    for (NSDictionary *stepDict in steps) {
        [self addRouteStepDict:stepDict toMapPointCArray:mapPointCArray atIndex:index];
        index = index+2;        
    }

    return mapPointCArray;
}

地図上のルートの最初の部分だけが赤い線で示されている理由がわかりません。

なにか提案を?

4

1 に答える 1

1

デリゲート メソッドは、viewForOverlayビューを表示する必要があるオーバーレイごとにマップ ビューによって呼び出されます。また、オーバーレイごとに複数回呼び出すこともできます。

コードは、そのメソッドoverlay にパラメーターとして渡された単一のビューを作成して返すことだけを気にする必要があります。

コードは次のようになります。

MKPolylineView *routeLineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
routeLineView.fillColor = [UIColor redColor];
routeLineView.strokeColor = [UIColor redColor];
routeLineView.lineWidth = 3;   
return routeLineView;

overlays質問の既存のコードは、マップビューが呼び出すすべてのオーバーレイの配列にたまたまある最後のオーバーレイに対応するビューを返しますviewForOverlay


さらに、その octomapkit にはmapPointCArrayFromJSONStringメソッドにバグがあります。これらの行:

MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D) 
                                        * [steps count]*2 -1);
numberOfPoints = [steps count]-1;

次のようにする必要があります。

MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D) 
                                        * [steps count]*2);
numberOfPoints = [steps count]*2;

最後の線分の終点が除外されているため、元の最初の線は間違っています。

元の 2 行目は (配列の最後のインデックスではなく)numberOfPoints内のポイントの数を反映するはずなので、非常に間違っています。このままでは、オーバーレイにはルートの半分しか表示されません。mapPointCArraysteps

そのコードを変更して、計算が 1 回だけ行われるようにすると、よりクリーンになります。

numberOfPoints = [steps count]*2;
MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D) 
                                        * numberOfPoints);


メソッドは、viewForOverlay前述のようにコーディングする必要があります。overlayマップ ビューのoverlays配列を直接操作するのではなく、渡されたパラメーターでのみ操作する必要があります。

于 2012-02-20T19:38:51.413 に答える