mkpolyline polylinewithpointsバグ?
質問
自分の場所から目的地までのルートを表示したいと思います。このコードを取りました http://code.google.com/p/octomapkit そして、いくつかのロギングメッセージを追加しました。ルート座標(約103)を適切に取得します。彼らは途中で、私の場所から目的地へのルートを埋めているので、Googleが呼び出して要素を解析するのは良いことです。しかし、Polylineの開始のみを示すよりも、mkmapviewに表示したい場合。 15または20のように。
コードを上から下に投稿しようとします。
元のコードは最初の要素のみを採用しましたが、最後に手に入れると、すべてのオーバーレイを追加するよりも、他の何かを見ると考えていました。
それ以外は : 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;
}
私のマップ上のルートの最初の割合で、赤い線がある理由がある理由を見ることができません。
なにか提案を?
解決
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
のポイント数を反映することになっています mapPointCArray
(の最後のインデックスではありません steps
配列)。そのように、オーバーレイはルートの半分しか表示されません。
そのコードを変更する方がクリーンであるため、計算は一度だけ行われます。
numberOfPoints = [steps count]*2;
MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D)
* numberOfPoints);
viewForOverlay
メソッドは、前述のようにまだコーディングする必要があります。でのみ動作する必要があります overlay
パラメーターはそれに渡され、マップビューの場合は直接ではありません overlays
配列。