首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未绘制MKPolygon

未绘制MKPolygon
EN

Stack Overflow用户
提问于 2012-04-27 04:52:38
回答 1查看 460关注 0票数 1

我试图在我的地图视图上绘制一个MKPolygon (三角形),但它没有显示出来。我还画了一条线,它正在显示,所以我不确定为什么三角形没有出现。以下是相关代码:

代码语言:javascript
复制
           CGPoint base = [mapView convertCoordinate:carLocation toPointToView:mapView];
            CGPoint pivot = [mapView convertCoordinate:locationToLookup toPointToView:mapView];
            MKPolygon *triangle = [self drawTriangle:pivot:base];
            [mapView removeOverlays:[mapView overlays]];
            [mapView addOverlay:line];
            [mapView addOverlay:triangle];


-(MKPolygon *) drawTriangle:(CGPoint) pivot:(CGPoint)base {
    if ((abs(pivot.x - base.x) >= 70) || abs(pivot.y - base.y) >= 70 )  {
        double triangleHeight = 30;
        double triangleWidth = 30;
        double triangleSide = sqrt(triangleWidth*triangleWidth/4+triangleHeight*triangleHeight);
        double openingAngle = asin( triangleWidth/2.0/triangleSide );
        double angle = atan2( pivot.y - base.y, pivot.x - base.x );

        CGPoint a;
        a.x = (int) (base.x + triangleSide*cos( angle + openingAngle));
        a.y = (int) (base.y + triangleSide*sin( angle + openingAngle));
        CGPoint b;
        b.x = (int) (base.x + triangleSide*cos( angle - openingAngle ));
        b.y = (int) (base.y + triangleSide*sin( angle - openingAngle ));

        MKMapPoint * mp = malloc(sizeof(MKMapPoint) * 3);
        MKMapPoint init;

        init.x = base.x;
        init.y = base.y;
        mp[0] = init;
        init.x = a.x;
        init.y = a.y;
        mp[1] = init;
        init.x = b.x;
        init.y = b.y;
        mp[2] = init;
        NSLog(@"base x: %f y: %f    a x: %f y: %f     b x: %f y :%f", base.x, base.y, a.x, a.y, b.x, b.y);
        MKPolygon* triangle = [MKPolygon polygonWithPoints:mp count:3];
        NSLog(@"inside");
        free(mp);
        return triangle;
    }
    NSLog(@"here");
    return nil;

}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay
{
    MKOverlayView* overlayView = nil;

    if (overlay == line) {
        NSLog(@"In line");
    MKPolylineView* mkov =  [[MKPolylineView alloc] initWithPolyline:overlay];
   // [mkov fillColor:[UIColor redColor]];
    mkov.fillColor = [UIColor redColor];
    mkov.strokeColor = [UIColor redColor];
    mkov.lineWidth = 3;  
    overlayView = mkov;


    return overlayView;
    } else {
        NSLog(@"In here");
        MKPolygonView *mkpv = [[MKPolygonView alloc] initWithPolygon:overlay];
        mkpv.fillColor = [UIColor blueColor];
        mkpv.strokeColor = [UIColor blueColor];
        mkpv.lineWidth = 3;
        overlayView = mkpv;
        return overlayView;
    }
}

三角形的点似乎是正确的:(这里的一个测试:基数x: 43.000000 y: 25.500000 a x: 50.000000 y: 58.000000 b x: 73.000000 y :38.000000),但是viewForOverlay永远不会被调用...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-27 21:56:18

我解决了这个问题,将我的CGPoints转换回CLLocationCoordinate2D,然后创建三角形:

代码语言:javascript
复制
    init = [mapView convertPoint:base toCoordinateFromView:mapView];
    mp[0] = init;
    init = [mapView convertPoint:a toCoordinateFromView:mapView];
    mp[1] = init;
    init = [mapView convertPoint:b toCoordinateFromView:mapView];
    mp[2] = init;
    MKPolygon* triangle = [MKPolygon polygonWithCoordinates:mp count:3];

我不确定为什么它不能与polygonWithPoints一起工作,但它可以与坐标一起工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10341274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档