iOS mapView添加多边形覆盖物层

该博客主要围绕iOS的mapView展开,涉及多边形覆盖物、圆形覆盖物以及画线等操作,这些内容属于iOS开发中地图相关的信息技术范畴。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mapView.delegate = self;
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(40.161613, 116.672968) animated:YES];
    MKCoordinateSpan span = MKCoordinateSpanMake(self.mapView.region.span.latitudeDelta * 0.01, self.mapView.region.span.longitudeDelta * 0.01);
    //设置地图范围
    [self.mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(40.161613, 116.672968), span) animated:YES];
    //任意多边形
    [self addPolygonOverlay];
    //圆形
    [self addCircleOverlay];
    //绘制线的方法
    [self pathOverLay];
}

//任意多边形
- (void)addPolygonOverlay {
    const int allNum = 12;
    CLLocationCoordinate2D ploycoords[allNum]  = {
        CLLocationCoordinate2DMake(40.2283333f, 116.5283333f),
        CLLocationCoordinate2DMake(40.2366667f, 116.6133333f),
        CLLocationCoordinate2DMake(40.1533333f, 116.6100000f),
        CLLocationCoordinate2DMake(40.0983333f, 116.6783333f),
        CLLocationCoordinate2DMake(40.0616667f, 116.6850000f),
        CLLocationCoordinate2DMake(39.9966667f, 116.6366667f),
        CLLocationCoordinate2DMake(39.9166667f, 116.6666667f),
        CLLocationCoordinate2DMake(39.9083333f, 116.5816667f),
        CLLocationCoordinate2DMake(39.9916667f, 116.5850000f),
        CLLocationCoordinate2DMake(40.0466667f, 116.5166667f),
        CLLocationCoordinate2DMake(40.0800000f, 116.5116667f),
        CLLocationCoordinate2DMake(40.1483333f, 116.5583333f)
    };
    MKPolygon *polygonOverlay = [MKPolygon polygonWithCoordinates:ploycoords count:allNum];
    [self.mapView addOverlay:polygonOverlay];
}

//圆形
- (void)addCircleOverlay {
    MKCircle *circleOverlay = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(39.8166667f, 116.5666667f) radius:10000.0f];
    [self.mapView addOverlay:circleOverlay];
}

//绘制线的方法
- (void)pathOverLay {
    CLLocationCoordinate2D pathCoords[6] = {
         CLLocationCoordinate2DMake(40.2366667f, 116.6850000f),
         CLLocationCoordinate2DMake(40.2366667f, 116.7250000f),
         CLLocationCoordinate2DMake(40.1766667f, 116.8850000f),
         CLLocationCoordinate2DMake(40.0366667f, 116.7850000f),
         CLLocationCoordinate2DMake(39.9366667f, 116.8850000f),
         CLLocationCoordinate2DMake(39.8366667f, 116.9850000f)
     };
    MKPolyline *pathOverlay = [MKPolyline polylineWithCoordinates:pathCoords count:6];
    [self.mapView addOverlay:pathOverlay];
}

#pragma mark - MKMapViewDelegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay {
    MKPolygonRenderer *poly = nil;
    if ([overlay isKindOfClass:[MKPolygon class]] == YES) {
        poly = [[MKPolygonRenderer alloc] initWithOverlay:overlay];
        poly.lineWidth = 1.0;
        CGFloat alphaFloat = 0.2;
        poly.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1.0];
        poly.fillColor = [[UIColor greenColor] colorWithAlphaComponent:alphaFloat];
    }
    else if ([overlay isKindOfClass:[MKCircle class]] == YES) {
        MKCircleRenderer *circle = [[MKCircleRenderer alloc] initWithOverlay:overlay];
        circle.lineWidth = 1.0;
        CGFloat alphaFloat = 0.2;
        circle.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1.0];
        circle.fillColor = [[UIColor greenColor] colorWithAlphaComponent:alphaFloat];
        return circle;
    }
    else if ([overlay isKindOfClass:[MKPolyline class]] == YES) {
        MKPolylineRenderer *line = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        line.lineWidth = 2;
        line.strokeColor = [UIColor orangeColor];
        return line;
    }
    return poly;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值