绘制多边形

通过 FMGLPolygon 类绘制多边形,FMGLPolyline类绘制多边形的线。

在地图绘制多边形的步骤如下:

(1)在ViewController.mviewDidLoad方法中构造多边形和折线数据对象(一组经纬度坐标点)。

CLLocationCoordinate2D coordinates[5];
coordinates[0].latitude = 39.781892;
coordinates[0].longitude = 116.293413;

coordinates[1].latitude = 39.787600;
coordinates[1].longitude = 116.391842;

coordinates[2].latitude = 39.733187;
coordinates[2].longitude = 116.417932;

coordinates[3].latitude = 39.704653;
coordinates[3].longitude = 116.338255;

coordinates[4].latitude = 39.781892;
coordinates[4].longitude = 116.293413;

// 构造多边形
FMGLPolygon *polygon = [FMGLPolygon polygonWithCoordinates:coordinates count:5];
// 在地图上添加多边形
[self.mapView addOverlay:polygon];

// 构造线
FMGLPolyline *line = [FMGLPolyline polylineWithCoordinates:coordinates count:5];
// 在地图上添加线
[self.mapView addOverlay:line];

(2) 继续在ViewController.m文件中,实现 <FMGLMapViewDelegate> 协议中的多个回调函数,设置多边形和线的样式。示例代码如下:

// 多边形填充色
- (UIColor *)mapView:(FMGLMapView *)mapView fillColorForPolygonAnnotation:(FMGLPolygon *)annotation
{
    return [UIColor yellowColor];
}
// 绘制透明度
- (CGFloat)mapView:(FMGLMapView *)mapView alphaForShapeAnnotation:(FMGLShape *)annotation
{
    if ([annotation isKindOfClass:[FMGLPolyline class]]) {
        return 1.;
    }else
    {
        return 0.5;
    }
}
// 线颜色
- (UIColor *)mapView:(FMGLMapView *)mapView strokeColorForShapeAnnotation:(FMGLShape *)annotation
{
    if ([annotation isKindOfClass:[FMGLPolyline class]]) {
        return [UIColor redColor];
    }else
    {
        return [UIColor yellowColor];
    }
}
// 线宽度
- (CGFloat)mapView:(FMGLMapView *)mapView lineWidthForPolylineAnnotation:(FMGLPolyline *)annotation
{
    if ([annotation isKindOfClass:[FMGLPolyline class]]) {
        return 5.;
    }else
    {
        return 0.;
    }
}

运行程序,效果如下所示:

MapDraw_image6

results matching ""

    No results matching ""