まだオーバーレイが表示されない理由はわかりませんが、新しいプロジェクトで次のコードを試してみてください。
私のテストでは、マップビューコントロールを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
、私はマップにMKPointAnnotation
、MKCircle
、TRTrip
、および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;
}
ここ
結果である:

上に赤い線がTRTrip
であり、底部に紫の一つはMKPolyline
あります。
RTFファイルにコードを投稿したり、コードを含む編集内容をここに投稿したりしないでください。 –
TRTripクラスのboundingMapRectメソッドでは、計算されたレスポンスの代わりに 'MKMapRectWorld'を返してください(fillColor、strokeColor、およびlineWidthのコメントを外してください)。 – Anna
がMKMapRectWorldを返し、fill、stroke、およびwidthを非推奨にしました。 Worldmapが表示されますが、ルート(オーバーレイ)は表示されません。 –