2011-03-29 50 views
9

我想繪製一個MKPolygon在iOS 4.0中的MKMapView。我有一個NSArray,其中包含包含緯度/經度屬性的自定義對象。下面我有一個代碼示例:iPhone MKMapView - MKPolygon問題

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    dataController = [[DataController alloc] initWithMockData]; 
    coordinateData = [dataController getCordData]; 

    CLLocationCoordinate2D *coords = NULL; 
    NSUInteger coordsLen = 0; 

    /* How do we actually define an array of CLLocationCoordinate2d? */ 

    MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:coordsLen]; 
    [mapView addOverlay: polygon]; 

} 

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
    MKPolygonView *polygonView = [[MKPolygonView alloc] initWithPolygon: routePolygon]; 
    NSLog(@"Attempting to add Overlay View"); 
    return polygonView; 
} 

我理解的方式是:

  1. 我需要創建MKPolygon
  2. DDD覆蓋到的MapView
  3. 這將反過來會觸發MKPolygonView的創建。

我的問題是我如何把我的自定義對象包含在NSArray(coordinateData)中並將這些對象轉換爲CLLocationCoordinate2d的數組,以便多邊形可以解釋並呈現?我不知道CLLocationCoordinate2d甚至是一個數組嗎?有人可以澄清這一點。

回答

17

polygonWithCoordinates方法需要CLLocationCoordinate2D結構的C數組。您可以使用malloc爲陣列分配內存(和free釋放內存)。循環遍歷你的NSArray並將其設置爲struct數組中的每個元素。

例如:

coordsLen = [coordinateData count]; 
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * coordsLen); 
for (int i=0; i < coordsLen; i++) 
{ 
    YourCustomObj *coordObj = (YourCustomObj *)[coordinateData objectAtIndex:i]; 
    coords[i] = CLLocationCoordinate2DMake(coordObj.latitude, coordObj.longitude); 
} 
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:coordsLen]; 
free(coords); 
[mapView addOverlay:polygon]; 

的viewForOverlay方法應該是這樣的:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
    MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease]; 
    polygonView.lineWidth = 1.0; 
    polygonView.strokeColor = [UIColor redColor]; 
    polygonView.fillColor = [UIColor greenColor]; 
    return polygonView; 
} 
1

對於iOS 7.0及以後,我們應該使用MKPolygonRenderer代替MKPolygonView

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay 
{ 
    MKPolygonRenderer * polygonView = [[MKPolygonRenderer alloc] initWithPolygon:overlay]; 
    polygonView.fillColor = [UIColor greenColor]; 
    polygonView.strokeColor = [UIColor redColor] ; 
    polygonView.lineWidth = 1.0; 
    return polygonView; 
} 
0

coordinatesArray ; //你的陣列包含座標

for (int i=0; i <[coordinatesArray count]; i++) 
{ 
    float latitude = [coordinatesArray[i][@"latitude"] floatValue]; 
    float longitude = [coordinatesArray[i][@"longitude"] floatValue]; 
    MKPolygon *polygon; 
    CLLocationCoordinate2D coordinates[[coordinatesArray count]]; 
    coordinates[i] = CLLocationCoordinate2DMake(latitude , longitude); 
    polygon = [MKPolygon polygonWithCoordinates:coordinates count:[coordinatesArray count]]; 
    [self.mapView addOverlay:polygon]; 
} 

//你「coordinatesArray」是含有與緯度和經度的鍵的多個值的字典的數組。 //希望這對你有所幫助。