返回

让地图更丰满的腾讯地图行政区划边界绘制教程

闲谈

新版腾讯地图带来行政区划轮廓点串

最近腾讯地图iOS SDK更新了4.4.0版本,推出了许多实用的功能,其中就包括行政区划轮廓点串,终于我们可以在地图上绘制行政区划的边界啦!

行政区划检索可以搭配定位功能,让用户清晰地看到他们当前所在的区域。与之相伴的还有行政区划展示功能。不多说,上教程!

如何绘制行政区划边界?

1. 引入地图SDK

@import TencentLBS;

2. 创建地图视图

TMapView *mapView = [[TMapView alloc] initWithFrame:self.view.bounds];

3. 设置地图视图的中心点和缩放级别

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(39.90866, 116.39748);
mapView.centerCoordinate = center;
mapView.zoomLevel = 10;

4. 创建行政区划检索对象

TMapPOISearch *search = [[TMapPOISearch alloc] init];

5. 设置行政区划检索参数

TMapPOISearchOption *option = [[TMapPOISearchOption alloc] init];
option.keyword = @"北京市";
option.adcode = @"110100";
option.radius = 5000;

6. 发起行政区划检索

[search poiSearchWithOption:option completionHandler:^(NSArray<TMapPOI *> * _Nullable pois, NSError * _Nullable error) {
    if (!error) {
        TMapPOI *poi = [pois firstObject];
        TMapBoundaryInfo *boundary = poi.boundaryInfo;
        for (TMapBoundary *boundaryPart in boundary.boundaryParts) {
            TMapPolyline *polyline = [[TMapPolyline alloc] init];
            polyline.points = boundaryPart.points;
            polyline.lineWidth = 5;
            polyline.color = [UIColor redColor];
            [mapView addOverlay:polyline];
        }
    }
}];

7. 实现地图视图代理方法

- (TMapOverlayView *)mapView:(TMapView *)mapView viewForOverlay:(id<TMapOverlay>)overlay {
    if ([overlay isKindOfClass:[TMapPolyline class]]) {
        TMapPolylineView *polylineView = [[TMapPolylineView alloc] initWithPolyline:overlay];
        polylineView.lineWidth = 5;
        polylineView.strokeColor = [UIColor redColor];
        return polylineView;
    }
    return nil;
}

结语

经过以上步骤,我们就能在地图上绘制行政区划边界了。大家快去试试吧!

使用此功能时,还有几个需要注意的点:

  • 使用行政区划轮廓点串绘制的边界仅供参考,不能作为精确的边界。
  • 轮廓点串的数量可能会比较多,在地图上绘制时可能会出现卡顿的情况。
  • 轮廓点串是行政区划边界的一部分,不能代表整个行政区划的轮廓。

希望本教程对大家有所帮助,如有任何问题,欢迎在评论区留言。