2011-03-29 39 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”是含有与纬度和经度的键的多个值的字典的数组。 //希望这对你有所帮助。