iPhone MKMapView – MKPolygon问题
我正在试图在iOS 4.0中的MKMapView上绘制MKPolygon。 我有一个NSArray包含自定义对象,其中包括纬度/经度属性。 我有一个代码示例如下:
- (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; }
我的理解是这样的:
- 我需要创buildMKPolygon
- 为MapView叠加一个叠加层
- 这将转变将触发创buildMKPolygonView。
我的问题是我如何把我的自定义对象包含在NSArray(coordinateData),并将这些对象转换为一个CLLocationCoordinate2d数组,使多边形可以解释和呈现? 我不知道如何CLLocationCoordinate2d甚至是一个数组? 有人可以澄清这一点。
polygonWithCoordinates方法需要CLLocationCoordinate2D结构的C数组。 您可以使用malloc
为数组分配内存(并释放内存)。 通过你的NSArray循环,并将其设置在结构数组中的每个元素。
例如:
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; }
对于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; }
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”是一个包含多个纬度和经度值的字典的数组。 / /希望这可以帮助你。
- 如何检查表中存在的数据库sqlite的Xamarin iOS
- UITableView – IndexPath总是返回0,并且需要2次点击才能将正确的数据传递给视图控制器
- 从正在运行的iOS项目导入.CPP文件并重命名为.mm问题
- IOS Box2D – 主体遵循一个特定的path,基于一个波动的速度点arrays
- 本地电话通知
- 使用后端Spring Security Java EE服务器和前端iOS Objective-C客户端进行身份validation
- Xamarin Studio iOS程序集错误
- Objective-C – 检查URL是否存在
- UINavigationBar背景颜色不是我设置的确切的UIColor