绘制折线

折线类为 FMGLPolyline ,由一组经纬度坐标组成,并以有序序列形式建立一系列的线段。

(1) 修改ViewController.m文件,在viewDidLoad方法中构造折线数据对象(一组经纬度坐标点)。

CLLocationCoordinate2D coords[10];
coords[0] = CLLocationCoordinate2DMake(38.8652123, 121.5315123);
coords[1] = CLLocationCoordinate2DMake(38.8662123, 121.5345123);
coords[2] = CLLocationCoordinate2DMake(38.8673123, 121.5349123);
coords[3] = CLLocationCoordinate2DMake(38.8691123, 121.5365123);
coords[4] = CLLocationCoordinate2DMake(38.8715123, 121.5361123);
coords[5] = CLLocationCoordinate2DMake(38.8750123, 121.5317123);
coords[6] = CLLocationCoordinate2DMake(38.8725123, 121.5266123);
coords[7] = CLLocationCoordinate2DMake(38.8698123, 121.5234123);
coords[8] = CLLocationCoordinate2DMake(38.8678123, 121.5225123);
coords[9] = CLLocationCoordinate2DMake(38.8647123, 121.5303123);

// 构造折线对象
FMGLPolyline *line = [FMGLPolyline polylineWithCoordinates:coords count:10];
// 在地图上添加折线对象
[self.mapView addOverlay:line];

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

// 折线颜色
- (UIColor *)mapView:(FMGLMapView *)mapView strokeColorForShapeAnnotation:(FMGLShape *)annotation
{
    return [UIColor redColor];
}
// 折线宽度
- (CGFloat)mapView:(FMGLMapView *)mapView lineWidthForPolylineAnnotation:(FMGLPolyline *)annotation
{
    return 5.;
}
// 折线透明度
- (CGFloat)mapView:(FMGLMapView *)mapView alphaForShapeAnnotation:(FMGLShape *)annotation
{
    return 0.5;
}

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

MapDraw_image5

results matching ""

    No results matching ""