添加地图覆盖物的逻辑原理
地图覆盖物其实就是在地图上画一些东西,例如路径,范围等等。添加地图覆盖物的逻辑原理其实和添加大头针很相似。首先所有可以成为覆盖物的对象必须遵守MKOverlay这个协议,通过
- (void)addOverlay:(id)overlay;
将覆盖物添加在地图上,然后地图会调用代理方法
-(MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id)overlay;
对覆盖物进行绘制,我们可以在这个方法中设置覆盖物,例如线宽,颜色等,注意,必须实现这个方法,覆盖物才会显示。
1、添加折线覆盖物
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化地图对象
MKMapView * _mapView = [[MKMapView alloc]initWithFrame:self.view.frame]; //设置地图 _mapView.region=MKCoordinateRegionMake(CLLocationCoordinate2DMake(33.23, 113.122), MKCoordinateSpanMake(10, 10));
//设置代理
_mapView.delegate=self;
//下面是C的语法,创建一个结构体数组 CLLocationCoordinate2D *coor;
coor = malloc(sizeof(CLLocationCoordinate2D)*5);
for (int i=0; i<5; i++) { CLLocationCoordinate2D po = CLLocationCoordinate2DMake(33.23+i*0.01, 113.112);
coor[i]=po;
}
//创建一个折线对象
MKPolyline * line = [MKPolyline polylineWithCoordinates:coor count:5]; [_mapView addOverlay:line];
[self.view addSubview:_mapView];
}
//覆盖物绘制的代理
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay{
//折线覆盖物提供类
MKPolylineRenderer * render = [[MKPolylineRenderer alloc]initWithPolyline:overlay];
//设置线宽
render.lineWidth=3;
//设置颜色
render.strokeColor=[UIColor redColor]; return render;
}
2、添加圆形覆盖物
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MKMapView * _mapView = [[MKMapView alloc]initWithFrame:self.view.frame]; _mapView.region=MKCoordinateRegionMake(CLLocationCoordinate2DMake(33.23, 113.122), MKCoordinateSpanMake(10, 10));
_mapView.delegate=self; //创建圆形覆盖物对象
MKCircle * cirle = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(33.23, 113.122) radius:500];
[_mapView addOverlay:cirle];
[self.view addSubview:_mapView];
}
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay{ MKCircleRenderer * render=[[MKCircleRenderer alloc]initWithCircle:overlay]; render.lineWidth=3; //填充颜色 render.fillColor=[UIColor greenColor]; //线条颜色
render.strokeColor=[UIColor redColor]; return render;
}
3、添加多边形覆盖物
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
MKMapView * _mapView = [[MKMapView alloc]initWithFrame:self.view.frame];
_mapView.region=MKCoordinateRegionMake(CLLocationCoordinate2DMake(33.23, 113.122), MKCoordinateSpanMake(10, 10));
_mapView.delegate=self; CLLocationCoordinate2D *coor;
coor = malloc(sizeof(CLLocationCoordinate2D)*6);
for (int i=0; i<5; i++) { CLLocationCoordinate2D po = CLLocationCoordinate2DMake(33.23+i*0.01, 113.112+((i/2==0)?0.01:-0.01)); coor[i]=po;
} coor[5]=CLLocationCoordinate2DMake(33.23, 113.112); MKPolygon * gon = [MKPolygon polygonWithCoordinates:coor count:6];
[_mapView addOverlay:gon];
[self.view addSubview:_mapView];
}
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay{ MKPolygonRenderer * render = [[MKPolygonRenderer alloc]initWithPolygon:overlay]; render.lineWidth=3;
render.strokeColor=[UIColor redColor]; return render;
}