2012-04-26 46 views
1

我想在我的mapview上畫一個MKPolygon(一個三角形),但它沒有顯示出來。我也畫了一條線,它顯示出來了,所以我不確定爲什麼這個三角形不是。下面是相關的代碼:未繪製MKPolygon

  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斧:50.000000 Y:58.000000 BX:73.000000 Y:38.000000),但viewForOverlay從不被稱爲......

回答

1

我通過轉換我的CGPoints回CLLocationCoordinate2D的,然後創建三角形解決了這個問題:

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工作,但它與協調一致。