【问题标题】:MKMapView overlay does not display on mapMKMapView 覆盖不显示在地图上
【发布时间】:2011-12-01 16:04:20
【问题描述】:

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

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

overlay 被调用,我从这些点构造一个MKOverlayPathView。我已经验证这些点在边界矩形内。但是,路线叠加层不会在显示的地图上绘制。

我没有高兴地检查了所有我能想到的东西,任何建议都将不胜感激。

【问题讨论】:

  • 请不要在 RTF 文件中发布代码或在此处回复包含您的代码的编辑内容。
  • 在您的 TRTrip 类 boundingMapRect 方法中,尝试返回 MKMapRectWorld 而不是计算的响应(并取消注释 fillColor、strokeColor 和 lineWidth)。
  • 返回 MKMapRectWorld 并取消注释填充、描边和宽度。世界地图出现,但没有显示路线(覆盖)。
  • 如果您在其中一个坐标处添加一个简单的 MKCircle 叠加层(给它一个大半径)并更新 viewForOverlay 以返回一个 MKCircleView(如果叠加层是 MKCircle 类型),是否会显示圆圈?
  • 在 viewFor 覆盖点我得到第一个坐标(波士顿市)我创建一个半径为 10000.0 的 mkcircle,从中创建圆形视图并返回视图。仍然没有覆盖。

标签: objective-c mkmapview mkoverlay


【解决方案1】:

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

在我的测试中,我将地图视图控件添加到 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 添加到地图:

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

结果如下:

顶部的红线是TRTrip,底部的紫色线是MKPolyline

【讨论】:

    【解决方案2】:

    如果你的例子比这个复杂一点,请检查你是否有这样的调用:

    [mMapView removeOverlay:overlay] 
    

    在代码中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多