当前位置: 移动技术网 > 移动技术>移动开发>IOS > 地图 SDK 系列教程-在地图上展示指定区域(转载)

地图 SDK 系列教程-在地图上展示指定区域(转载)

2019年09月11日  | 移动技术网移动技术  | 我要评论
腾讯位置服务地图sdk是一套提供多种地理位置服务的应用程序接口。通过调用该接口,开发者可以在自己的应用中加入地图相关的功能(如地图展示、标注、绘制图形等),轻松访问腾讯地图服务和数据,构建功能丰富、交互性强、符合各种行业场景的地图类应用程序。

 

以下内容转载自ios 工程师genosage的文章《地图 sdk 系列教程-在地图上展示指定区域》
作者:genosage
链接:来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

地图sdk系列教程-在地图上展示指定区域

在使用腾讯 ios 地图 sdk 的过程中,经常会遇到需要地图展示指定区域的场景,相信大家也会遇到类似的情况,地图 sdk 提供了许多与之相关的接口,本篇文章将对这些接口进行整合,并提供示例代码来实现多个场景下展示指定区域的需求。

需要注意,本篇文章适用于地图未发生旋转与俯仰角的场景。

下载腾讯 ios 地图 sdk 请前往:ios 地图 sdk

指定区域包含单个坐标点

在地图上显示某个固定的坐标点是地图 sdk 最为基础的功能之一。

举例来说,我们根据 sdk 的检索功能得到了天坛公园的坐标 (39.881190,116.410490),接下来,我们可以通过设置地图的中心点 centercoordinate 来让地图显示这个坐标,同时我们还可以设置 zoomlevel 来指定缩放级别:

// 设置中心点
self.mapview.centercoordinate = cllocationcoordinate2dmake(39.881190,116.410490);    

// 设置缩放级别
self.mapview.zoomlevel = 15;

 

显示效果如下:

 

 

 

如果想展示墨卡托坐标点 qmappoint 则需要先通过方法 qcoordinateformappoint(qmappoint mappoint) 将墨卡托坐标转换为经纬度再进行设置。

指定区域包含多个坐标点

现在,假如我们想把天坛公园的搜索结果都显示在地图上,应该如何实现呢?

首先,我们通过检索功能搜索天坛公园,取搜索结果的前九个坐标点,接下来,应该使我们的地图视野包含这九个坐标点,地图 sdk 提供了方法 qboundingcoordinateregionwithcoordinates(cllocationcoordinate2d *coordinates, nsuinteger count) 来计算多个经纬度坐标点的最小外接矩形 qcoordinateregion 在得到了外接矩形之后,我们可以直接设置地图的 region 来使其显示我们想要的区域,完整代码如下:

cllocationcoordinate2d coordinates[9];

// 天坛公园检索结果坐标
coordinates[0] = cllocationcoordinate2dmake(39.881190,116.410490);
coordinates[1] = cllocationcoordinate2dmake(39.883247,116.400063);
coordinates[2] = cllocationcoordinate2dmake(39.883710,116.412900);
coordinates[3] = cllocationcoordinate2dmake(39.883654,116.412863);
coordinates[4] = cllocationcoordinate2dmake(39.883320,116.400040);
coordinates[5] = cllocationcoordinate2dmake(39.876980,116.413190);
coordinates[6] = cllocationcoordinate2dmake(39.878160,116.413140);
coordinates[7] = cllocationcoordinate2dmake(39.878980,116.407080);
coordinates[8] = cllocationcoordinate2dmake(39.878560,116.413160);
    
// 计算区域外接矩形
qcoordinateregion region = qboundingcoordinateregionwithcoordinates(coordinates, 9);
    
// 设置区域
self.mapview.region = region;

 

显示效果如下:

 


 

 

指定中心点

如果我们想显示这九个坐标点的同时指定某个坐标点为地图中心点,可以使用方法 qboundingcoordinateregionwithcoordinatesandcenter(cllocationcoordinate2d *coordinates, nsuinteger count, cllocationcoordinate2d centercoordinate) 来计算最小外接矩形,以天坛公园为中心点举例,相关代码如下:

// 中心点坐标
cllocationcoordinate2d centercoordinate = cllocationcoordinate2dmake(39.881190,116.410490);

// 计算以中心点为中心的区域外接矩形
qcoordinateregion region = qboundingcoordinateregionwithcoordinatesandcenter(coordinates, 9, centercoordinate);
    
// 设置区域
self.mapview.region = region;

 

显示效果如下:

 


 

 

嵌入四周边界

如果需要在地图视野四周嵌入一些边界,可以使用方法 - (void)setregion:(qcoordinateregion)region edgepadding:(uiedgeinsets)insets animated:(bool)animated 对地图的 region 进行设置,代码如下:

// 计算区域外接矩形
qcoordinateregion region = qboundingcoordinateregionwithcoordinates(coordinates, 9);
    
// 设置区域与边界
[self.mapview setregion:region edgepadding:uiedgeinsetsmake(20, 20, 20, 20) animated:no];

 

显示效果如下:

 


 

 

墨卡托坐标点

如果需要展示多个墨卡托坐标点,可以使用地图 sdk 提供的与上述方法对应的 qboundingmaprectwithpoints(qmappoint *points, nsuinteger count) 和 - (void)setvisiblemaprect:(qmaprect)maprect edgepadding:(uiedgeinsets)insets animated:(bool)animated 等方法。

折线与覆盖物

当我们想在地图中展示路线或者覆盖物时,即地图 sdk 中的 qpolyline qpolygon 和 qcircle,我们可以直接获得他们的属性 boundingmaprect 再进行设置即可。

指定区域包含多个标注

在很多场景下,我们需要在地图上添加标注点 (annotation) 并且自定义这些标注的 image,如下所示:

 

 

 

我们可以通过下面的代码来使这些标注刚好显示在地图视野内:

// 计算包含 annotation 与 maprect 的外接矩形
qmaprect rect = [self.mapview maprectthatfits:qmaprectnull containscalloutview:no annotations:self.annotations edgepadding:uiedgeinsetszero];
    
// 设置区域
self.mapview.visiblemaprect = rect;

 

显示效果如下:

 


 

 

当标注显示 callout view 时,我们可以通过传入参数 bcontainscalloutview 为 yes 来将 callout view 包含在地图视野内,显示效果如下:

 


 

 

有时我们需要指定区域同时包含当前的屏幕视野以及所有的标注,我们可以通过传入第一个参数 maprect 为 self.mapview.visiblemaprect 来达到我们想要的效果。

限制展示指定的区域

当我们需要限制地图视野,使其只显示我们指定的区域时,以故宫举例,可以通过如下的代码进行设置:

cllocationcoordinate2d coordinates[4];

// 故宫范围矩形的四个顶点的经纬度坐标
coordinates[0] = cllocationcoordinate2dmake(39.922878,116.391547);
coordinates[1] = cllocationcoordinate2dmake(39.912917,116.392100);
coordinates[2] = cllocationcoordinate2dmake(39.913312,116.402507);
coordinates[3] = cllocationcoordinate2dmake(39.923277,116.402024);
    
// 计算区域外接矩形
qcoordinateregion region = qboundingcoordinateregionwithcoordinates(coordinates, 4);
    
// 计算平面投影矩形
qmaprect rect = qmaprectforcoordinateregion(region);
    
// 限制展示区域
[self.mapview setlimitmaprect:rect mode:qmaplimitrectfitwidth];

 

显示效果如下:

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网