2011-12-01 31 views
2

私のアプリはGoogleからルートのポイントを取得します。応答には境界矩形も含まれます。私のアプリはrectを作成し、マップを正しく表示し、オーバーレイを追加します。MKMapViewオーバーレイが地図上に表示されない

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>) 

オーバーレイが呼び出され、ポイントからMKOverlayPathViewを作成します。私は、点が境界rect内にあることを確認しました。ただし、ルートオーバーレイは表示されたマップに描画されません。

喜んで私が考えることができるすべてをチェックしましたが、どんな提案も大変ありがとうございます。

+0

RTFファイルにコードを投稿したり、コードを含む編集内容をここに投稿したりしないでください。 –

+0

TRTripクラスのboundingMapRectメソッドでは、計算されたレスポンスの代わりに 'MKMapRectWorld'を返してください(fillColor、strokeColor、およびlineWidthのコメントを外してください)。 – Anna

+0

がMKMapRectWorldを返し、fill、stroke、およびwidthを非推奨にしました。 Worldmapが表示されますが、ルート(オーバーレイ)は表示されません。 –

答えて

2

まだオーバーレイが表示されない理由はわかりませんが、新しいプロジェクトで次のコードを試してみてください。

私のテストでは、マップビューコントロールをxibに追加し、IBOutletとマップビューのデリゲートアウトレットをFile's Ownerに接続しました。コードでマップビューを作成することもできます(xibに追加して、delegateプロパティを設定してください)。

これは私のテストTRTripクラスです:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    CLLocationCoordinate2D bostonCoord = CLLocationCoordinate2DMake(42.3507,-71.0608); 

    //center map on Boston... 
    mMapView.region = MKCoordinateRegionMakeWithDistance(bostonCoord, 30000, 30000); 

    //add boston annotation... 
    MKPointAnnotation *pa = [[MKPointAnnotation alloc] init]; 
    pa.coordinate = bostonCoord; 
    pa.title = @"Boston"; 
    [mMapView addAnnotation:pa]; 
    [pa release]; 

    //add MKCircle overlay... 
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:bostonCoord radius:10000]; 
    [mMapView addOverlay:circle]; 

    //add TRTrip overlay... 
    TRTrip *trt = [[TRTrip alloc] init]; 
    [mMapView addOverlay:trt]; 
    [trt release]; 

    //NOTE: 
    //Using an MKPolyline and MKPolylineView is probably easier than 
    //manually drawing lines using MKOverlayPathView. 

    //add MKPolyline overlay... 
    int numberOfRouteCoords = 3; 
    CLLocationCoordinate2D *routeCoords = malloc(numberOfRouteCoords * sizeof(CLLocationCoordinate2D)); 
    routeCoords[0] = CLLocationCoordinate2DMake(42.34, -71.1); 
    routeCoords[1] = CLLocationCoordinate2DMake(42.25, -71.05); 
    routeCoords[2] = CLLocationCoordinate2DMake(42.3, -71.02); 
    MKPolyline *pl = [MKPolyline polylineWithCoordinates:routeCoords count:numberOfRouteCoords]; 
    [mMapView addOverlay:pl]; 
    free(routeCoords); 
} 

、これは次のとおりです。View ControllerのviewDidLoad

//TRTrip.h... 
@interface TRTrip : NSObject<MKOverlay> 
@property (nonatomic, readonly) MKMapRect boundingMapRect; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@end 

//TRTrip.m... 
@implementation TRTrip 
@synthesize boundingMapRect; 
@synthesize coordinate; 
-(MKMapRect)boundingMapRect { 
    return MKMapRectWorld; 
} 
-(CLLocationCoordinate2D)coordinate { 
    return CLLocationCoordinate2DMake(42.3507,-71.0608); 
} 
@end 

、私はマップにMKPointAnnotationMKCircleTRTrip、およびMKPolylineを追加しますviewForOverlay方法:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay 
{ 
    if ([overlay isKindOfClass:[MKCircle class]]) 
    { 
     MKCircleView *cv = [[[MKCircleView alloc] initWithCircle:overlay] autorelease]; 
     cv.fillColor = [UIColor greenColor]; 
     cv.strokeColor = [UIColor blueColor]; 
     cv.alpha = 0.5; 
     return cv; 
    } 

    if ([overlay isKindOfClass:[TRTrip class]]) 
    { 
     MKOverlayPathView *opv = [[[MKOverlayPathView alloc] initWithOverlay:overlay] autorelease]; 

     opv.strokeColor = [UIColor redColor]; 
     opv.lineWidth = 3; 

     CGMutablePathRef myPath = CGPathCreateMutable(); 

     MKMapPoint mp1 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(42.3507,-71.1)); 
     CGPoint cgp1 = [opv pointForMapPoint:mp1]; 
     CGPathMoveToPoint(myPath, nil, cgp1.x, cgp1.y); 

     MKMapPoint mp2 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(42.45,-71.05)); 
     CGPoint cgp2 = [opv pointForMapPoint:mp2]; 
     CGPathAddLineToPoint(myPath, nil, cgp2.x, cgp2.y); 

     MKMapPoint mp3 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(42.3,-71.0)); 
     CGPoint cgp3 = [opv pointForMapPoint:mp3]; 
     CGPathAddLineToPoint(myPath, nil, cgp3.x, cgp3.y); 

     opv.path = myPath; 

     CGPathRelease(myPath); 

     return opv; 
    } 

    if ([overlay isKindOfClass:[MKPolyline class]]) 
    { 
     MKPolylineView *plv = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease]; 
     plv.strokeColor = [UIColor purpleColor]; 
     plv.lineWidth = 5; 
     return plv; 
    } 

    return nil; 
} 
ここ

結果である:

enter image description here

上に赤い線がTRTripであり、底部に紫の一つはMKPolylineあります。

0

あなたの例は、少し複雑その後、この場合には、あなたがこのような任意の呼び出しがあれば、チェック:コード内

[mMapView removeOverlay:overlay] 

を。

関連する問題