098:vue+openlayers的set extent 和 fit extent功能(示例代码)

本文由高级前端工程师分享,通过vue+openlayers实现地图的set extent和fit extent功能,提供详细配置步骤及87行源代码示例,涵盖地图加载、控件展示、数据加载、图形绘制、事件交互、测量等多个应用场景。

作者: 还是大剑师兰特 ,曾为美国某知名大学计算机专业研究生,现为国内GIS领域高级前端工程师,CSDN知名博主,深耕openlayers、leaflet、mapbox、cesium,canvas,echarts等技术开发,欢迎加微信(gis-dajianshi),一起交流。

查看本专栏目录 - 本文是第 098个示例

一、示例效果图

二、示例简介

本示例介绍如何在vue+openlayers中使用extent,演示 set extent 和 fit extent等内容。

直接复制下面的 vue+openlayers源代码,操作2分钟即可运行实现效果

三、配置方式

1)查看基础设置:https://xiaozhuanlan.com/topic/1378924560
或者同样查看:https://blog.csdn.net/cuclife/article/details/126195754
2)将上述的源代码,粘贴到src/views/Home.vue中,npm run serve 运行即可。

四、示例源代码(共87行)


/*
* @Author: 大剑师兰特(xiaozhuanlan),还是
Vue.js OpenLayers 结合可以创建复杂的地图应用,尤其是处理大量数据时。OpenLayers 的 WebGLPoints 方法允许你渲染大型点集,它通过 WebGL 提供了高性能的图形渲染能力。 以下是一个简单的 Vue 组件示例,展示了如何使用 `WebGLPoints` 来显示大量的点: ```html <template> <div ref="map" :style="{ height: '400px' }"></div> </template> <script> import { Map, VectorSource, Point, Layer } from 'ol'; import 'ol/webgl'; export default { data() { return { points: [], // 这里需要你自己填充海量点的数据 map: null, }; }, mounted() { const canvas = this.$refs.map.canvas; // 初始化地图 this.map = new Map({ target: canvas, view: new View(), layers: [ new Layer({ source: new VectorSource({ type: 'geojson', url: 'your_data.json', // 你的点数据源,可能是文件或API format: new GeoJSON(), loader: () => { return fetch('your_data.json') .then(response => response.json()) .then(data => ({ features: data.features })); }, }), style: function(feature) { return { image: new ImageMarker( new Point(feature.getGeometry().getCoordinates()), { size: [8, 8] } ), }; }, }), ], }); // 使用WebGLPoints渲染点 const webGLPointsLayer = new ol.layer.Vector({ source: new ol.source.Vector({ wrapX: false, useSpatialIndex: false, format: new ol.format.GeoJSON(), loader: () => { // 加载并解析GeoJSON数据 return fetch('your_data.json') .then(response => response.json()) .then(data => { const features = data.features.map(f => f.geometry); return { type: 'FeatureCollection', features: features, }; }); }, strategy: ol.loadingstrategy.tileVector({ tileGrid: this.map.getView().getResolutionScale(512), extent: this.map.getSize().tonormalizedUnits(), reprojection: function(extent, resolution) { return transformExtent(extent, this.source.projection, this.view.getProjection()); }, }), strategies: [new ol.loadingstrategy.BBOX(), new ol.loadingstrategy.ZoomLevels()], }), // 使用WebGLPoints样式 style: function(feature) { return [ new ol.style.Style({ image: new ol.style.WebGLPoints({ size: 1, fill: new ol.style.Fill({ color: [255, 255, 255, 0.7] }), stroke: new ol.style.Stroke({ color: [0, 0, 0, 1], width: 1 }), radius: 6, }), geometry: feature.getGeometry(), }) ]; }, }); this.map.add(webGLPointsLayer); }, }; </script> ``` 在这个例子中,首先从GeoJSON数据加载点,并在`mounted`生命周期钩子函数中初始化地图`WebGLPointsLayer`。注意,由于WebGL对性能的影响,你可能需要根据实际需求调整`loader`策略`style`设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还是大剑师兰特

打赏一杯可口可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值