当前位置: 首页 > article >正文

OpenLayers 加载ArcGIS瓦片数据

注:当前使用的是 ol 5.3.0 版本,天地图使用的key请到天地图官网申请,并替换为自己的key

随着GIS应用的不断发展,Web地图也越来越丰富,除了像ESRI、超图、中地数码这样GIS厂商有各自的数据源格式,也有Google、百度、高德、腾讯提供的GIS资源,如何加载各种GIS数据源,是WebGIS开发要解决的一个关键问题。

本节主要介绍加载百度地图数据。

1. 如何加载百度地图数据

可以参照瓦片地图的方法来加载百度地图,在OpenLayer 5地图库中没有加载百度地图数据源的方法,所以需要根据百度地图瓦片请求格式拓展数据源来加载。

// 分辨率数组
const resolutions = []
for (let i = 0; i < 19; i++) {resolutions[i] = Math.pow(2, 18 - i)
}const tileGrid = new ol.tilegrid.TileGrid({origin: [0, 0],resolutions: resolutions
})
// 百度地图数据源
const baiduSource = new ol.source.TileImage({projection: projection, // 坐标参考tileGrid: tileGrid, // 瓦片请求方式tileUrlFunction: function (tileCoord, pixelRatio, proj) {if (!tileCoord) {return ""}const z = tileCoord[0]let x = tileCoord[1]let y = tileCoord[2]if (x < 0) {x = "M" + (-x)}if (y < 0) {y = "M" + (-y)}return "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20151021&scaler=1&p=1";}
})
  • tileUrlFunction

拼接瓦片地图URL,{x}{y}{z}对应瓦片行列号和级数。

  • projection

地图投影坐标系,百度地图才用EPSG:3857坐标系。

  • tileGrid

切片网格,地图切片原点为[0,0]

2. 完整代码2

其中libs文件夹下的包需要更换为自己下载的本地包或者引用在线资源。

<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>加载Baidu数据</title><meta charset="utf-8" /><script src="../libs/js/ol-5.3.3.js"></script><script src="../libs/js/jquery-2.1.1.min.js"></script><link rel="stylesheet" href="../libs/css//ol.css"><style>* {padding: 0;margin: 0;font-size: 14px;font-family: '微软雅黑';}html,body {width: 100%;height: 100%;}#map {position: absolute;width: 100%;height: 100%;}.ol-mouse-position {padding: 5px;top: 10px;height: 40px;line-height: 40px;background: #060505ba;text-align: center;color: #fff;border-radius: 5px;}</style></head><body><div id="map" title="地图显示"></div></body></html><script>//地图投影坐标系const projection = ol.proj.get('EPSG:3857');//==============================================================================////============================天地图服务参数简单介绍============================////================================vec:矢量图层=================================////================================img:影像图层=================================////================================cva:注记图层=================================////======================其中:_c表示经纬度投影,_w表示球面墨卡托投影============////==============================================================================//const TDTImgLayer = new ol.layer.Tile({title: "天地图影像图层",source: new ol.source.XYZ({url: "http://t0.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=2a890fe711a79cafebca446a5447cfb2",attibutions: "天地图注记描述",crossOrigin: "anoymous",wrapX: false})})const TDTImgCvaLayer = new ol.layer.Tile({title: "天地图影像注记图层",source: new ol.source.XYZ({url: "http://t0.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=2a890fe711a79cafebca446a5447cfb2",attibutions: "天地图注记描述",crossOrigin: "anoymous",wrapX: false})})const map = new ol.Map({target: "map",loadTilesWhileInteracting: true,view: new ol.View({center: [11421771, 4288300],zoom: 5,worldsWrap: true,minZoom: 1,maxZoom: 20,projection: projection}),// 鼠标控件:鼠标在地图上移动时显示坐标信息。controls: ol.control.defaults().extend([// 加载鼠标控件new ol.control.MousePosition()])})// map.addLayer(TDTImgLayer)// map.addLayer(TDTImgCvaLayer)const resolutions = []for (let i = 0; i < 19; i++) {resolutions[i] = Math.pow(2, 18 - i)}const tileGrid = new ol.tilegrid.TileGrid({origin: [0, 0],resolutions: resolutions})// 百度地图数据源const baiduSource = new ol.source.TileImage({projection: projection,tileGrid: tileGrid,tileUrlFunction: function (tileCoord, pixelRatio, proj) {if (!tileCoord) {return ""}const z = tileCoord[0]let x = tileCoord[1]let y = tileCoord[2]if (x < 0) {x = "M" + (-x)}if (y < 0) {y = "M" + (-y)}return "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20151021&scaler=1&p=1";}})const baiduLayer = new ol.layer.Tile({source: baiduSource})map.addLayer(baiduLayer)
</script>

OpenLayers示例数据下载,请回复关键字:ol数据

全国信息化工程师-GIS 应用水平考试资料,请回复关键字:GIS考试

【GIS之路】 已经接入了智能助手,欢迎关注,欢迎提问。

欢迎访问我的博客网站-长谈GIShttp://shanhaitalk.com

都看到这了,不要忘记点赞、收藏 + 关注

本号不定时更新有关 GIS开发 相关内容,欢迎关注 !

http://www.lryc.cn/news/2387280.html

相关文章:

  • 2025蓝桥杯WP
  • 数字人教师:开启教育智慧革新之旅
  • Linux中Java开发、部署和运维常用命令
  • 详解srs流媒体服务器的集群
  • ubuntu22.04 安装 SecureCRT8.7.3
  • Day 37
  • libvirt设置虚拟机mtu实现原理
  • AstroNex空间任务智能控制研究与训练数据集
  • 汽车副水箱液位传感器介绍
  • Docker+MobaXterm+x11实现容器UI界面转发本地
  • IEEE出版|2025年智能制造、机器人与自动化国际学术会议 (IMRA2025)
  • EasyRTC嵌入式SDK音视频实时通话助力WebRTC技术与智能硬件协同发展
  • Higress MCP Server 安全再升级:API 认证为 AI 连接保驾护航
  • 多个vue2工程共享node_modules
  • 蓝桥杯178 全球变暖
  • 多模态理解大模型高性能优化丨前沿多模态模型开发与应用实战第七期
  • mysql 合集
  • Zustand V5教程:Vanilla Store 与 useStore 使用详解 + 实战 Demo
  • docker 搭建php 开发环境 添加扩展redis、swoole、xdebug(1)
  • 人脸识别技术合规备案最新政策详解
  • (16)高性能风控系统设计
  • AStar低代码平台-脚本调用C#方法
  • 企业级RAG技术实战指南:从理论到落地的全景解析
  • getline()跳过输入
  • 【八股战神篇】RabbitMQ高频面试题
  • 高阶数据结构——红黑树实现
  • 互联网大厂Java求职面试:AI与大模型应用集成中的架构难题与解决方案
  • 安卓学习笔记-声明式UI
  • AI天气预报进入“大模型时代“:如何用Transformer重构地球大气模拟?
  • 本地项目如何设置https(2)——2025-05-19