最近,工作中有需要用OpenLayers脚本库调用天地图的WMTS服务接口,由于这两个都是刚开始接触,所以是摸着石头过河,遇到了地图显示不了的问题。

  我也通过用浏览器直接提供的地址打开,来进行参数对比。再看了OpenLayers和天地图的相关文档和网络上的文章,也加入了相关群下载相关资料浏览并询问群友,还是未果。

  虽然官网http://www.zjditu.cn/resource/apihelp 有这么一个例子,但是我替换成提供的正式地址,并没有显示地图(每个层级都是需要的,否则只能通过放大或者缩小才能看到,还有layer的值大小写需要匹配)。

  最后还是让提供方给了个例子,完成了实现(刚开始要,没给,不知道是忙还是什么情况)。

  为了让刚接触这种需求的广大程序猿能够少走点弯路,下面给出示例,有注释。有些参数不懂或者想使用复杂功能的,则查阅下列网址:

  OpenLayers官网:https://openlayers.org/

  扯淡大叔的教程:http://anzhihun.coding.me/ol3-primer/

  示例代码如下:

<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css">
<script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
</head>
<body>
<div>
&nbsp;选中的经纬度:
<input id="Coordinates" type="text" value="" disabled="disabled" style="width: 350px;" />
</div>
<div id="map" class="map" style="height: 493px;"></div>
<div id="div_overlay" style="display: none;">
<div id="marker" title="">
<img src="position_orange.png" />
</div>
<div id="popup" title=""></div>
</div>
<script>
var projection = ol.proj.get('EPSG:4326');//设置坐标系
var projectionExtent = projection.getExtent();
//分辨率
var resolutions = [
1.40625,
0.703125,
0.3515625,
0.17578125,
0.087890625,
0.0439453125,
0.02197265625,
0.010986328125,
0.0054931640625,
0.00274658203125,
0.001373291015625,
0.0006866455078125,
0.00034332275390625,
0.000171661376953125,
0.0000858306884765625,
0.00004291534423828125,
0.000021457672119140625,
0.000010728836059570312,
0.000005364418029785156,
0.000002682209014892578,
0.000001341104507446289
];
//瓦片矩阵
var matrixIds = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; var centerXY = "121.54449462890625,29.875946044921875";//地图中心点,默认宁波市
var center = ReturnCenter(centerXY); var emap_attributions = new ol.Attribution({
html: '<span class="span_switch" onclick="ChangeToImgMap()">点击切换地图类型</span>'
});
function EMap() {
var map = new ol.Map({
controls: ol.control.defaults({
attribution: false
}).extend([
new ol.control.MousePosition()//是否显示鼠标所在地图点的经纬度
]),
layers: [
new ol.layer.Tile({
source: new ol.source.WMTS({
attributions: emap_attributions,
name: "中国矢量1-14级",
url: "http://t{0-6}.tianditu.com/vec_c/wmts",
layer: "vec",
style: "default",
matrixSet: "c",
format: "tiles",
wrapX: true,//地图缩小后,防止在一个页面出现多个一样的地图
tileGrid: new ol.tilegrid.WMTS({
origin: ol.extent.getTopLeft(projectionExtent),
resolutions: resolutions.slice(0, 15),//slice方法不清楚的请百度
matrixIds: matrixIds.slice(0, 15)
})
}),
maxResolution: resolutions[0],
minResolution: resolutions[14]
}),
new ol.layer.Tile({
source: new ol.source.WMTS({
attributions: emap_attributions,
name: "中国矢量注记1-14级",
url: "http://t{0-6}.tianditu.com/cva_c/wmts",
layer: "cva",
style: "default",
matrixSet: "c",
format: "tiles",
wrapX: true,
tileGrid: new ol.tilegrid.WMTS({
origin: ol.extent.getTopLeft(projectionExtent),
resolutions: resolutions.slice(0, 15),
matrixIds: matrixIds.slice(0, 15)
})
}),
maxResolution: resolutions[0],
minResolution: resolutions[14]
}),
new ol.layer.Tile({
source: new ol.source.WMTS({
attributions: emap_attributions,
name: "浙江矢量15-17级",
url: "http://srv{0-6}.zjditu.cn/ZJEMAP_2D/wmts",
layer: "TDT_ZJEMAP",
style: "default",
matrixSet: "TileMatrixSet0",
format: "image/png",
wrapX: true,
tileGrid: new ol.tilegrid.WMTS({
origin: ol.extent.getTopLeft(projectionExtent),
resolutions: resolutions.slice(15, 18),
matrixIds: matrixIds.slice(15, 18)
})
}),
maxResolution: resolutions[14],//指定当前级数的上一级来平滑过渡,否则滚轮缩放当前级数会显示空白
minResolution: resolutions[17]
}),
new ol.layer.Tile({
source: new ol.source.WMTS({
attributions: emap_attributions,
name: "浙江矢量注记15-17级",
url: "http://srv{0-6}.zjditu.cn/ZJEMAPANNO_2D/wmts",
layer: "ZJEMAPANNO",
style: "default",
matrixSet: "TileMatrixSet0",
format: "image/png",
wrapX: true,
tileGrid: new ol.tilegrid.WMTS({
origin: ol.extent.getTopLeft(projectionExtent),
resolutions: resolutions.slice(15, 18),
matrixIds: matrixIds.slice(15, 18)
})
}),
maxResolution: resolutions[14],//指定当前级数的上一级来平滑过渡,否则滚轮缩放当前级数会显示空白
minResolution: resolutions[17]
}),
new ol.layer.Tile({
source: new ol.source.WMTS({
attributions: emap_attributions,
name: "XX县矢量18-20级",
url: "替换成你需要的wmts服务接口地址",
layer: "jsemap",
style: "default",
matrixSet: "TileMatrixSet0",
format: "image/png",
wrapX: true,
tileGrid: new ol.tilegrid.WMTS({
origin: ol.extent.getTopLeft(projectionExtent),
resolutions: resolutions.slice(18, 21),
matrixIds: matrixIds.slice(18, 21)
})
}),
maxResolution: resolutions[17],//指定当前级数的上一级来平滑过渡,否则滚轮缩放当前级数会显示空白
minResolution: resolutions[20]
}),
new ol.layer.Tile({
source: new ol.source.WMTS({
attributions: emap_attributions,
name: "XX县矢量注记18-20级",
url: "替换成你需要的wmts服务接口地址",
layer: "jsemapanno",
style: "default",
matrixSet: "TileMatrixSet0",
format: "image/png",
wrapX: true,
tileGrid: new ol.tilegrid.WMTS({
origin: ol.extent.getTopLeft(projectionExtent),
resolutions: resolutions.slice(18, 21),
matrixIds: matrixIds.slice(18, 21)
})
}),
maxResolution: resolutions[17],//指定当前级数的上一级来平滑过渡,否则滚轮缩放当前级数会显示空白
minResolution: resolutions[21]
})
],
target: "map",
view: new ol.View({
center: center,//地图中心点
projection: projection,//投影类别
zoom: 10,//默认缩放级别
maxZoom: 20,//最大缩放级别
minZoom: 1//最小缩放级别
})
}); //鼠标点击时设置中心点覆盖物
GetElementId("map").onclick = function () {
coordinates = GetElementsByClassName('ol-mouse-position')[0].innerHTML;
SetPosition(map, coordinates);
};
} EMap();//显示E电子地图 function GetElementId(id) {
return document.getElementById(id);
}
function GetElementsByClassName(className) {
return document.getElementsByClassName(className);
}
//中心点处理
function ReturnCenter(centerXY) {
var centerObj = centerXY.split(',');
var centerX = centerObj[0];
var centerY = centerObj[1];
return [parseFloat(centerX), parseFloat(centerY)];//一定要转换下类型,否则拖拽后,地图就消失了
}
//设置中心点覆盖物
function SetPosition(map, coordinates) {
GetElementId("Coordinates").value = coordinates; var newcenter = ReturnCenter(coordinates);
// Position marker
var marker = new ol.Overlay({
position: newcenter,
positioning: 'bottom-center',
element: document.getElementById('marker'),
stopEvent: false
});
map.addOverlay(marker);
}
</script>
</body>
</html>
  可将上述代码直接复制到txt文件中并保存html,即可成功浏览。点击鼠标可以显示覆盖物并把坐标值显示在上面的文本框中。

  附上坐标覆盖物图片和效果图:

  

  

OpenLayer3调用天地图示例的更多相关文章

  1. OpenLayer3调用天地图,拖拽后,地图消失的问题[已解决]

    拖拽后,地图直接消失了,而且右上角的坐标变成了NaN,NaN 后来经过测试发现,原来是自己封装有问题,坐标点一定要用parseFloat()转换下,但不清楚为什么页面刚开始加载的时候没有问题,总之能解 ...

  2. Javascript调用ActiveX示例

      Javascript调用ActiveX示例   写一个ActiveX控件比如叫做MyNameSpace.SecreteInfo,安装在客户机器上,这样可以通过c++获取到机器的几乎任何信息. 在网 ...

  3. 用VS2005写一个 C 的类库和用 C#来调用的示例

    一.用VS2005写一个 C 的类库的步骤: (1).建立一个空的Visual C++项目 (2).这时候在项目中可以看见 三个空目录 选中 "源文件" 目录,然后点鼠标右键,在弹 ...

  4. EzHttp 流传输调用代码示例

    EzHttp框架提供的内置接口,用于文件流等传输 流传输调用代码示例 内置接口: public interface IEzStreamHandler { Task<byte[]> GetD ...

  5. vc调用dll 示例

    其实,调用dll文件的方法很多,不一定要使用LoadLibrary函数.如果使用的话,你就要预先声明dll中的函数,很麻烦. 下面是我使用dll时的一点技巧,就是引入lib文件,可以参考: 一.Win ...

  6. AXIS2远程调用WebService示例(Eclipse+AXIS)

    转自:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/10/3071584.html 我们将Web Service发布在Tomcat或者其他应 ...

  7. 2019年以后ArcGIS 调用天地图的资源URL

    2019年1月1日起,天地图做出如下变更,导致直接在Arcgis/ArcMap中添加WMTS服务不能用了. 国家天地图解释的很清楚,注册个人用户就可以了. 原有调用方式不变,只要在URL 后添加“&a ...

  8. SpringCloud调用服务示例

    SpringCloud調用服務示例. SpringCloud简介: Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务 ...

  9. [C#/Java] C#中调用Servlet示例

    需求 通用消息接口使用servlet作为服务器端服务接口,第三方应用程序通过http post的方式调用servlet,实现与通用消息接口的调用连接. 参数说明如下: msgTitle:消息标题,描述 ...

随机推荐

  1. 客户端怎么查看SVN的代码库

    安装SVN客户端,比如TortoiseSVN,然后将代码库checkout到本地,或者通过客户端的版本库浏览器直接连接SVN服务器查看代码库的目录结构. 如果SVN服务器端安装的时候是和Apache集 ...

  2. 一个php技术栈后端猿的知识储备大纲

    <h1 align="center">Easy Tips</h1><p align="center"><a href= ...

  3. Yourphp系统发生错误

    今天早用YP更新缓存,弹出如下错误: 随之而来的是错误信息是: [php] view plain copy [14-12-03 10:48:12] E:\wwwweb\xmwszt360\Core\L ...

  4. Asp.net mvc 中的路由

    在 Asp.net mvc 中,来自客户端的请求总是针对某个 Controller 中的 Action 方法,因此,必须采用某种机制从请求的 URl 中解析出对应的 Controller 和 Acti ...

  5. Apache Commons介绍(转载)

    一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 二.Commons CLI说明:这 ...

  6. 源码讲解 node+mongodb 建站攻略(一期)第二节

    源码讲解 node+mongodb 建站攻略(一期)第二节 上一节,我们完成了模拟数据,这次我们来玩儿真正的数据库,mongodb. 代码http://www.imlwj.com/download/n ...

  7. Pandas(python)数据处理:只对某一列DataFrame数据进行归一化

    处理数据要用到Pandas,但是没有学过,不知道是否有直接对某一列归一化的方法调用.自己倒弄了下.感觉还是比较麻烦. 使用Pandas读取到数组之后想把其中的'MonthlyIncome'一列进行归一 ...

  8. linux_NFS

    NFS是什么? 网络文件系统,又叫共享存储,通过网络连接让不同主机之间实现共享存储. 应用于存放图片.附件.视频等用户上传文件 相关同类应用:大型网站nfs有压力,使用moosefs(mfs),Ghu ...

  9. PHP获取字符串编码与转码

    (⊙o⊙)-编辑器保存的是utf8的文档取出来的字符串是gbk编码?问题很多,字符串转换为utf8的话在浏览器输出乱码 $e=mb_detect_encoding($d,array('GB2312', ...

  10. zabbix监控-部署(一)

    zabbix之自动化监控-部署篇(一) 标签(空格分隔): linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 浅谈监控 监控命令 查看硬件的温度/风扇转 ...