MKMapView 叠加层不显示在地图上

发布于 2024-12-19 02:01:11 字数 292 浏览 1 评论 0原文

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

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

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

我已经检查了我能想到的一切,没有任何喜悦,任何建议将不胜感激。

My app gets the points for a route from google. The response also contains the bounding rect. My app creates the rect and shows the map correctly and I add the overlay. The

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

overlay is called and I construct a MKOverlayPathView from the points. I have verified that the points are within the bounding rect. However, the route overlay does not draw on the displayed map.

I have checked everything I can think of with no joy, any suggestions would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚你发端 2024-12-26 02:01:11

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

在我的测试中,我将地图视图控件添加到 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 中,我添加一个 MKPointAnnotationMKCircleTRTrip,以及地图上的 MKPolyline

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

I don't know yet why the overlay isn't showing but try the following code in a fresh, new project.

In my test, I added the map view control to the xib and then connected the IBOutlet and the map view's delegate outlet to File's Owner. Creating the map view in code will also work (just don't also add it to the xib and be sure to set the delegate property).

This is my test TRTrip class:

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

In the view controller's viewDidLoad, I add an MKPointAnnotation, MKCircle, TRTrip, and an MKPolyline to the map:

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

and this is the viewForOverlay method:

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

Here is the result:

enter image description here

The red line on top is the TRTrip and the purple one on the bottom is the MKPolyline.

滥情空心 2024-12-26 02:01:11

如果您的示例比这更复杂,请检查

[mMapView removeOverlay:overlay] 

代码中是否有任何类似以下的调用:

If your example is a bit more complex then this, check if you have any calls like this:

[mMapView removeOverlay:overlay] 

in the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文