vue2中openlayers 在廊坊市添加聚合点,鼠标经过聚合点 显示名称

时间: 2023-10-01 11:04:49 浏览: 49
在Vue2中使用OpenLayers添加聚合点并显示名称,可以按照以下步骤进行: 1. 安装OpenLayers和ol-ext库。 ```bash npm install ol ol-ext --save ``` 2. 在Vue组件中引入OpenLayers和ol-ext库。 ```javascript import ol from 'ol' import 'ol/ol.css' import olExt from 'ol-ext' import 'ol-ext/dist/ol-ext.css' ``` 3. 在Vue组件中创建地图容器和地图对象。 ```javascript <template> <div class="map-container"> <div id="map" class="map"></div> </div> </template> <script> export default { data() { return { map: null } }, mounted() { this.initMap() }, methods: { initMap() { this.map = new ol.Map({ target: 'map', view: new ol.View({ center: ol.proj.fromLonLat([116.7, 39.5]), zoom: 10 }) }) } } } </script> <style scoped> .map-container { width: 100%; height: 100%; } .map { width: 100%; height: 100%; } </style> ``` 4. 创建聚合图层和聚合点。 ```javascript initMap() { const clusterSource = new ol.source.Cluster({ distance: 40, source: new ol.source.Vector({ features: [ new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([116.7, 39.5])), name: '廊坊市' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([116.8, 39.5])), name: '广阳区' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([116.9, 39.5])), name: '香河县' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([117.0, 39.5])), name: '大城县' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([117.1, 39.5])), name: '文安县' }) ] }) }) const clusterLayer = new olExt.layer.Cluster({ source: clusterSource, distance: 40, animationDuration: 500, style: feature => { const count = feature.get('features').length const style = new ol.style.Style({ image: new ol.style.Circle({ radius: 15, stroke: new ol.style.Stroke({ color: 'white', width: 2 }), fill: new ol.style.Fill({ color: 'red' }) }), text: new ol.style.Text({ text: count.toString(), fill: new ol.style.Fill({ color: 'white' }), stroke: new ol.style.Stroke({ color: 'black', width: 2 }) }) }) return count === 1 ? this.createFeatureStyle(feature.get('features')[0]) : style } }) this.map.addLayer(clusterLayer) }, createFeatureStyle(feature) { return new ol.style.Style({ image: new ol.style.Circle({ radius: 15, stroke: new ol.style.Stroke({ color: 'white', width: 2 }), fill: new ol.style.Fill({ color: 'blue' }) }), text: new ol.style.Text({ text: feature.get('name'), fill: new ol.style.Fill({ color: 'white' }), stroke: new ol.style.Stroke({ color: 'black', width: 2 }) }) }) } ``` 5. 添加鼠标移动事件,显示聚合点名称。 ```javascript initMap() { const clusterSource = new ol.source.Cluster({ distance: 40, source: new ol.source.Vector({ features: [ new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([116.7, 39.5])), name: '廊坊市' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([116.8, 39.5])), name: '广阳区' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([116.9, 39.5])), name: '香河县' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([117.0, 39.5])), name: '大城县' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([117.1, 39.5])), name: '文安县' }) ] }) }) const clusterLayer = new olExt.layer.Cluster({ source: clusterSource, distance: 40, animationDuration: 500, style: feature => { const count = feature.get('features').length const style = new ol.style.Style({ image: new ol.style.Circle({ radius: 15, stroke: new ol.style.Stroke({ color: 'white', width: 2 }), fill: new ol.style.Fill({ color: 'red' }) }), text: new ol.style.Text({ text: count.toString(), fill: new ol.style.Fill({ color: 'white' }), stroke: new ol.style.Stroke({ color: 'black', width: 2 }) }) }) return count === 1 ? this.createFeatureStyle(feature.get('features')[0]) : style } }) this.map.addLayer(clusterLayer) const overlay = new ol.Overlay({ element: document.getElementById('popup'), autoPan: true, autoPanAnimation: { duration: 250 } }) this.map.addOverlay(overlay) this.map.on('pointermove', evt => { const pixel = this.map.getEventPixel(evt.originalEvent) const feature = this.map.forEachFeatureAtPixel(pixel, feature => feature) if (feature) { const coordinates = feature.getGeometry().getCoordinates() overlay.setPosition(coordinates) document.getElementById('popup-content').innerText = feature.get('name') overlay.getElement().style.display = 'block' } else { overlay.getElement().style.display = 'none' } }) }, createFeatureStyle(feature) { return new ol.style.Style({ image: new ol.style.Circle({ radius: 15, stroke: new ol.style.Stroke({ color: 'white', width: 2 }), fill: new ol.style.Fill({ color: 'blue' }) }), text: new ol.style.Text({ text: feature.get('name'), fill: new ol.style.Fill({ color: 'white' }), stroke: new ol.style.Stroke({ color: 'black', width: 2 }) }) }) } ``` 6. 在模板中添加弹出框元素。 ```html <template> <div class="map-container"> <div id="map" class="map"></div> <div id="popup" class="ol-popup"> <div id="popup-content"></div> </div> </div> </template> <style scoped> .map-container { width: 100%; height: 100%; } .map { width: 100%; height: 100%; } .ol-popup { position: absolute; background-color: white; -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2)); filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2)); padding: 15px; border-radius: 10px; border: 1px solid #cccccc; bottom: 12px; left: -50px; min-width: 100px; display: none; } .ol-popup:after, .ol-popup:before { top: 100%; border: solid transparent; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; } .ol-popup:after { border-top-color: white; border-width: 10px; left: 48%; margin-left: -10px; } .ol-popup:before { border-top-color: #cccccc; border-width: 11px; left: 48%; margin-left: -11px; } </style> ``` 这样就可以在Vue2中使用OpenLayers添加聚合点并显示名称了。

相关推荐

最新推荐

recommend-type

vue使用openlayers实现移动点动画

主要为大家详细介绍了vue使用openlayers实现移动点动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Vue实现鼠标经过文字显示悬浮框效果的示例代码

主要介绍了Vue实现鼠标经过文字显示悬浮框效果,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

vue实现鼠标经过动画

主要为大家详细介绍了vue实现鼠标经过动画的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

在vue中动态添加class类进行显示隐藏实例

今天小编就为大家分享一篇在vue中动态添加class类进行显示隐藏实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

vue-openlayers实现地图坐标弹框效果

主要为大家详细介绍了vue-openlayers实现地图坐标弹框效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。