MKMapView叠加层不会在地图上显示

时间:2011-12-01 16:04:21

标签: objective-c mkmapview mkoverlay

我的应用从谷歌获取路线的积分。响应还包含边界矩形。我的应用程序创建了rect并正确显示了地图,我添加了叠加层。在

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

叠加,我从点构造MKOverlayPathView。我已经确认这些点在边界内。但是,路径覆盖不会在显示的地图上绘制。

我已经检查了所有我能想到的东西,没有任何喜悦,任何建议都会非常感激。

2 个答案:

答案 0 :(得分:2)

我还不知道为什么叠加层没有显示,但在新的新项目中尝试以下代码。

在我的测试中,我将地图视图控件添加到xib,然后将IBOutlet和地图视图的委托出口连接到文件所有者。在代码中创建地图视图也可以工作(只是不要将它添加到xib并确保设置delegate属性)。

这是我的测试TRTrip课程:

//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

在视图控制器的viewDidLoad中,我向地图添加了MKPointAnnotationMKCircleTRTripMKPolyline:< / p>

- (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);
}

这是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

答案 1 :(得分:0)

如果您的示例稍微复杂一点,请检查您是否有这样的调用:

[mMapView removeOverlay:overlay] 
代码中的