2012-08-23 2 views
3

MKMapView에서 오버레이를 구현하는 방법에 대한 여러 StackOverflow 게시물과 Apple 설명서를 살펴 보았습니다. 나를 위해 특별히 맵에서 MKPolygon 객체를 표시하는 데 관심이 있습니다.iOS에서 오버레이 표시 방법 MapKit

  • 링크 MapKit와 CoreLocation는
  • 는 MKMapKit 개체에 출구를 확인하고
  • CLLocationCoordinate2D 배열을 선언 대리인으로 뷰 컨트롤러를 선언 프레임 워크 : 나는 기본적으로 프로세스는 다음 귀결 것으로 나타났습니다 다각형의 포인트를 포함하고, 클래스 방법 polygonWithCoordinates 함께 MKPolygon 객체를 생성 :지도 및 파라미터
  • 구현 (MKOverlayView *)지도보기로 새롭게 생성 MKPolygon 객체를 전달 :
  • 통화 addOverlay을 : 수 (MKMapVi ew *) mapView viewForOverlay : (id) 오버레이

나중에 지정된 시간에 20-30 개의 다각형을 표시해야 할 것입니다. 그러나 오버레이 (파일에서 데이터를 읽는 대신 테스트 예제를 하드 코딩)를 표시하는 방법을 탐색하면서 일부 오버레이를 표시 할 수 있지만 다른 것은 표시 할 수없는 것으로 나타났습니다. Apple에 의한 Location Awareness Programming Guide를 읽으면서 콜로라도 주 위에 겹쳐진 다각형의 예를 보았습니다. 그게 효과가 있었어. 그러나 캔자스를 덮은 다각형을 만들려고 할 때, 나는 그것을 작동시키지 못했습니다. 그것은 내 자신의 (Embry-Riddle Aeronautical University 다각형과 Kansas 다각형)으로 만들려고 한 모든 다각형이 표시되지 않지만, 내가 온라인으로 얻은 것들은 완벽하게 작동합니다. Google 어스를 사용하여 폴리곤을 만든 다음 좌표를 가져 오기 위해 KML 파일로 내 보냈습니다.

내 ViewController 구현을위한 코드는 다음과 같습니다. 이 문제를 일으키기 위해 의도하지 않게 잘못되었을 수도있는 것을 찾으려고 노력합니다. 도움에 미리 감사드립니다.

#import "ViewController.h" 
#import <CoreLocation/CoreLocation.h> 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize mapView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    // Array of coordinates for polygon covering state of Colorado ... DISPLAYS PERFECTLY 

    CLLocationCoordinate2D points[4]; 

    points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116); 
    points[1] = CLLocationCoordinate2DMake(36.99892, -109.045267); 
    points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981); 
    points[3] = CLLocationCoordinate2DMake(41.002371, -102.052066); 

    MKPolygon *polygon = [MKPolygon polygonWithCoordinates:points count:4]; 
    [mapView addOverlay:polygon]; 
    [polygon release]; 

    // Array of coordinates for polygon covering state of Kansas ... DOESN'T DISPLAY 

    CLLocationCoordinate2D kansasPoints[9]; 

    kansasPoints[0] = CLLocationCoordinate2DMake(-102.0595440241806, 39.99774930940907); 
    kansasPoints[1] = CLLocationCoordinate2DMake(-102.0424467175215, 36.99846609483674); 
    kansasPoints[2] = CLLocationCoordinate2DMake(-94.62550551403953, 36.98936020770036); 
    kansasPoints[3] = CLLocationCoordinate2DMake(-94.58798745384412, 39.11683771419185); 
    kansasPoints[4] = CLLocationCoordinate2DMake(-94.79955391183, 39.21290793052091); 
    kansasPoints[5] = CLLocationCoordinate2DMake(-95.13489191971419, 39.51613476830012); 
    kansasPoints[6] = CLLocationCoordinate2DMake(-94.86553124171813, 39.78380472206268); 
    kansasPoints[7] = CLLocationCoordinate2DMake(-95.02618283417986, 39.89072859904893); 
    kansasPoints[8] = CLLocationCoordinate2DMake(-95.31904155494097, 39.99390420513669); 

    MKPolygon *kansasPolygon = [MKPolygon polygonWithCoordinates:kansasPoints count:9]; 
    [mapView addOverlay:kansasPolygon]; 
    [kansasPolygon release]; 

    // Array of coordinates for polygon covering part of Daytona Beach, FL campus 
    // of Embry-Riddle Aeronautical University... DOESN'T DISPLAY 

    CLLocationCoordinate2D erauPoints[7]; 

    erauPoints[0] = CLLocationCoordinate2DMake(-81.05176, 29.18492); 
    erauPoints[1] = CLLocationCoordinate2DMake(-81.04409, 29.18801); 
    erauPoints[2] = CLLocationCoordinate2DMake(-81.05166, 29.19293); 
    erauPoints[3] = CLLocationCoordinate2DMake(-81.05365, 29.19536); 
    erauPoints[4] = CLLocationCoordinate2DMake(-81.05465, 29.19493); 
    erauPoints[5] = CLLocationCoordinate2DMake(-81.05376, 29.19323); 
    erauPoints[6] = CLLocationCoordinate2DMake(-81.05506, 29.19188); 

    MKPolygon *erauPolygon = [MKPolygon polygonWithCoordinates:erauPoints count:7]; 
    [mapView addOverlay:erauPolygon]; 
    [erauPolygon release]; 

    // Array of coordinates taken from http://www.shawngrimes.me/2011/04/adding-polygon-map-overlays/ 
    // for commuter parking lot at Capitol College in Maryland ... DISPLAYS PERFECTLY 

    CLLocationCoordinate2D commuterLotCoords[5]={ 
     CLLocationCoordinate2DMake(39.048019,-76.850535), 
     CLLocationCoordinate2DMake(39.048027,-76.850234), 
     CLLocationCoordinate2DMake(39.047407,-76.850181), 
     CLLocationCoordinate2DMake(39.047407,-76.8505), 
     CLLocationCoordinate2DMake(39.048019,-76.850535) 
    }; 

    MKPolygon *commuterPoly1 = [MKPolygon polygonWithCoordinates:commuterLotCoords count:5]; 
    [mapView addOverlay:commuterPoly1]; 
    [commuterPoly1 release]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay { 
    if ([overlay isKindOfClass:[MKPolygon class]]) { 
     MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithOverlay:overlay] autorelease]; 
     polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.3f]; 
     polygonView.strokeColor = [UIColor redColor]; 
     polygonView.lineWidth = 1.0f; 

     return polygonView; 
    } 

    return nil; 
} 

@end 

답변

2

표시되지 않는 다각형에 대한 좌표의 위도 및 경도 매개 변수가 거꾸로되어있는 것처럼 보입니다.

예를 들어

이 :

kansasPoints[0] = CLLocationCoordinate2DMake(-102.0595440241806, 39.99774930940907); 

는 또한

kansasPoints[0] = CLLocationCoordinate2DMake(39.99774930940907, -102.0595440241806); 


해야합니다, 당신은 그들이 오토 릴리즈되기 때문에 당신이 polygonWithCoordinates를 사용하여 작성중인 MKPolygon 객체에 release를 호출 할 수 없습니다.

+0

음, 그건 어리석은 실수였습니다. :) 나는 내가 너무 오랫동안 이것을보고 있었기 때문에 매개 변수 역전에 주목하지 않았다고 생각한다. 당신의 도움을 주셔서 감사합니다. 매력처럼 일했습니다! – user1195536

관련 문제