iOS原生地圖開發指南再續——地圖覆蓋物的應用

iOS原生地圖開發指南再續——地圖覆蓋物的應用

1、引言

在前兩篇博客中,將iOS系統的地圖框架MapKit中地圖的設置與應用以及關於添加大頭針和自定義大頭針的相關操做作了詳細的介紹。連接以下:http://my.oschina.net/u/2340880/blog/415360http://my.oschina.net/u/2340880/blog/415441。這篇博客中將進一步討論關於地圖添加覆蓋物的使用方法。數組

2、添加地圖覆蓋物的邏輯原理

地圖覆蓋物其實就是在地圖上畫一些東西,例如路徑,範圍等等。添加地圖覆蓋物的邏輯原理其實和添加大頭針很類似。首先全部能夠成爲覆蓋物的對象必須遵照MKOverlay這個協議,經過框架

- (void)addOverlay:(id <MKOverlay>)overlay;學習

將覆蓋物添加在地圖上,而後地圖會調用代理方法spa

 

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay;.net

對覆蓋物進行繪製,咱們能夠在這個方法中設置覆蓋物,例如線寬,顏色等,注意,必須實現這個方法,覆蓋物纔會顯示。代理

一、添加折線覆蓋物

- (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<MKOverlay>)overlay{
    //折線覆蓋物提供類
    MKPolylineRenderer * render = [[MKPolylineRenderer alloc]initWithPolyline:overlay];
    //設置線寬
    render.lineWidth=3;
    //設置顏色
    render.strokeColor=[UIColor redColor];
    return render;
}

效果以下:code

二、添加圓形覆蓋物

- (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<MKOverlay>)overlay{
    MKCircleRenderer * render=[[MKCircleRenderer alloc]initWithCircle:overlay];
    render.lineWidth=3;
    //填充顏色
    render.fillColor=[UIColor greenColor];
    //線條顏色
    render.strokeColor=[UIColor redColor];
    return render;
}

效果以下:對象

三、添加多邊形覆蓋物

- (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<MKOverlay>)overlay{
    MKPolygonRenderer * render = [[MKPolygonRenderer alloc]initWithPolygon:overlay];
    render.lineWidth=3;
    render.strokeColor=[UIColor redColor];
    return render;
}

效果以下:blog

 

疏漏之處 歡迎指正ci

學習使用 歡迎轉載

專一技術,熱愛生活,交流技術,也作朋友。

——琿少 QQ羣:203317592

相關文章
相關標籤/搜索