尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

5分钟ol-ext快速上手:OpenLayers地图扩展

5分钟ol-ext快速上手:OpenLayers地图扩展 5分钟ol-ext快速上手OpenLayers地图扩展【免费下载链接】ol-extCool extensions for Openlayers (ol) - animated clusters, CSS popup, Font Awesome symbol renderer, charts for statistical map (pie/bar), layer switcher, wikipedia layer, animations, canvas filters.项目地址: https://gitcode.com/gh_mirrors/ol/ol-ext给 OpenLayers 地图加一个弹窗得自己写 CSS 锚点、算定位、加关闭按钮一屏样板代码。ol-ext 把这些变成一行配置它是一个 OpenLayers 扩展库内置 CSS 弹窗、动画集群、图层切换器和历史地图图层无论你是 ol 新手还是用了很多年的老手5 分钟就能动手。ol-ext 能干什么CSS 弹窗7 套内置主题自动定位动画集群缩放时点位平滑聚散LayerSwitcher带透明度、排序的切换器Chart 样式在地图上画饼图/柱状图历史地图图层扫描件直接叠上地图需求原生 OpenLayers加 ol-ext弹窗自己拼 HTML 和 CSSCSS 主题 一行定位集群静态圆点缩放平滑动画历史影像叠加自己算投影和范围GeoImage 图层开箱即用⚡ 安装与首次运行npm 方式两条命令ol 是 peerDependency必须先装npm install ol # 先装 peer 依赖 npm install ol-ext # 再装扩展库纯网页方式把 4 个文件指向任意 CDN 的对应版本即可注意 ol 必须在 ol-ext 之前link relstylesheet hrefol.css / script srcol.js/script link relstylesheet hrefol-ext.css / script srcol-ext.js/script用打包器时按模块引入注意样式文件别漏import ol/ol.css; import ol-ext/dist/ol-ext.css; // 样式漏了弹窗就没边框 import LayerSwitcher from ol-ext/control/LayerSwitcher;最小可跑示例11 行建出带图层切换器的地图const map new Map({ target: map, view: new View({ center: [2.35, 48.85], zoom: 6 }) }); map.addLayer(new TileLayer({ source: new OSM(), name: OSM底图 })); // 挂上图层切换器地图上直接开关图层 map.addControl(new LayerSwitcher({ target: document.getElementById(switcher) }));核心能力深潜ol-ext CSS 弹窗 Popup把普通 HTML 框变成带锚点、关闭按钮、主题的弹窗定位交给库自己算const popup new Popup({ popupClass: default, // 主题default / black / tips 等 closeBox: true, // 显示关闭按钮 positioning: auto, // 8 方向自动选位 autoPan: { animation: { duration: 250 } } // 弹近时平移地图 }); map.addOverlay(popup);效果点一点就弹靠近地图边缘自动换方向再引入 anim 样式还有弹跳动画。ol-ext 动画集群 AnimatedCluster标准的 ol.source.Cluster 换个图层就动了缩放时点位平滑聚散const clusterSource new Cluster({ distance: 40, // 聚合半径像素 source: new VectorSource({ features }) }); // 换成 AnimatedCluster缩放即动画 const layer new AnimatedCluster({ source: clusterSource, animationDuration: 700, // 动画时长毫秒 style: getStyle // 按聚簇数量自定义样式 });效果官方示例 2000 个点位不卡配 SelectCluster 交互点击集群会炸开显示每个点。ol-ext 历史地图图层 GeoImage把扫描的历史航片直接叠到地图上不用自己切片、算金字塔const layer new GeoImage({ source: new Static({ url: oldmap.jpg, // 历史影像文件 projection: EPSG:3857, // 影像所属坐标系 imageExtent: extent // 影像覆盖的地理范围 }) });效果拖一下不透明度新旧对比一张 1976 年的巴黎航片就这么叠在现代底图上了。 5 分钟实战历史地图 弹窗场景1976 航片叠在现代底图上能切换图层点哪弹哪。第 1 步建底图 历史图层const map new Map({ target: map, view: new View({ center: [2.47, 48.85], zoom: 12 }) }); map.addLayer(new TileLayer({ source: new OSM(), name: 现代底图 })); map.addLayer(new GeoImage({ source: new Static({ url: oldmap.jpg, projection: EPSG:3857, imageExtent: extent }), name: 1976航片 }));第 2 步加图层切换器map.addControl(new LayerSwitcher({ target: switcher }));第 3 步加点击弹窗const popup new Popup({ positioning: auto, closeBox: true }); map.addOverlay(popup); map.on(singleclick, e { popup.at(e.coordinate, bottom-center); // 在点击处弹出 popup.show(b1976/bbr同一个位置50 年前); });第 4 步打开页面右侧切换新旧图层点地图看弹窗。完事总共不到 20 行业务代码。生产级注意事项大数据量先 Cluster 减点再谈动画官方 2000 点示例 distance40 帧率稳定别把半径设太小。版本匹配ol-ext 声明 peerDependency ol 5.3.0项目里 ol 太旧会导致部分控件挂不上。样式冲突弹窗主题都在 .ol-popup 前缀下项目里有全局 resetnormalize 之类影响锚点箭头时用更高特异性 CSS 覆盖。资源地图与下一步API 文档本地 HTMLdoc/doc-pages/示例页examples/control/、animation/、layer/、popup/ 等子目录每个示例都是独立单文件弹窗源码src/overlay/Popup.js图层扩展源码src/layer/控件源码src/control/今天就能做的一步git clone https://gitcode.com/gh_mirrors/ol/ol-ext cd ol-ext npm install npm start # 起本地 live server打开 localhost:8181所有示例都挂在首页上随便点一个跑通再回来对照 src/ 看实现。【免费下载链接】ol-extCool extensions for Openlayers (ol) - animated clusters, CSS popup, Font Awesome symbol renderer, charts for statistical map (pie/bar), layer switcher, wikipedia layer, animations, canvas filters.项目地址: https://gitcode.com/gh_mirrors/ol/ol-ext创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表