openlayers 添加geojson并设置鼠标悬浮高亮

该代码段展示了一个在Vue组件中加载GeoJSON数据到OpenLayers地图的过程。它创建了一个矢量图层,设置了样式,并添加了交互功能,特别是选择高亮功能。
摘要由CSDN通过智能技术生成
<template>
  <div class="container"></div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { api } from '@/axios/api.ts'
import { MapObj } from '@/components/openlayer_map/index.js'
let vectorLayer = null
const addLayer = () => {
    api.map_cesium.getCommonData(null, '/data/wuhan_bounds.geojson').then(res => {
        let geojsonObject = res.data
        const vectorSource = new ol.source.Vector({
            features: new ol.format.GeoJSON().readFeatures(geojsonObject)
        })
        vectorLayer = new ol.layer.Vector({
            source: vectorSource,
            style: MapObj.getVectorStyle
        })
        MapObj.currentMap.addLayer(vectorLayer)
        // MapObj.currentMap.on('pointermove', (e) => {
        //     console.log(e)
        // })
        //交互时高亮
        let selected = {
            fill: new ol.style.Fill({               //填充样式
                color: 'rgba(255, 255, 255, 0.5'
            }),
            stroke: new ol.style.Stroke({           //线样式
                color: '#ffcc33',
                width: 5
            }),
            image: new ol.style.Circle({             //点样式
                radius: 10,
                fill: new ol.style.Fill({
                    color: '#e67e22'
                })
            })
        }
        const selectStyle = (feature) => {
            selected.text = new ol.style.Text({
                font: '16px sans-serif',
                text: feature.values_.name,
                fill: new ol.style.Fill({
                    color: [255, 255, 255, 1],
                }),
                backgroundFill: new ol.style.Fill({
                    color: [168, 50, 153, 0.6],
                }),
                padding: [2, 2, 2, 2],
            })
            let result = new ol.style.Style(selected)
            return result;
        }
        var select_move = new ol.interaction.Select({
            // ol.events.condition.鼠标交互方式
            condition: ol.events.condition.pointerMove,//设置监听事件
            style: selectStyle
        })
        MapObj.currentMap.addInteraction(select_move)
    })
}
const removeLayer = () => {
    if (vectorLayer) {
        MapObj.currentMap.removeLayer(vectorLayer)
    }
}
onMounted(() => {
    addLayer()
})
onUnmounted(() => {
    removeLayer()
})
</script>

<style scoped></style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要加载GeoJSON数据,可以使用OpenLayers的Vector图层。首先,需要创建一个Vector图层对象,然后使用OpenLayers的Format.GeoJSON类来解析GeoJSON数据,并将其添加到Vector图层中。最后,将Vector图层添加地图中即可。 以下是一个示例代码: ``` var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ url: 'path/to/your/geojson/file.geojson', format: new ol.format.GeoJSON() }) }); map.addLayer(vectorLayer); ``` 其中,`url`属性指定GeoJSON文件的路径,`format`属性指定使用OpenLayersGeoJSON解析器来解析数据。最后,将Vector图层添加地图中即可。 注意:在使用OpenLayers加载GeoJSON数据时,需要确保GeoJSON文件的格式正确,否则可能会导致加载失败。 ### 回答2: Openlayers是一种开源地图框架,支持多种地图底图和图层。其中,加载GeoJSONOpenlayers的一项重要功能。 要加载GeoJSON,首先要创建一个新图层。新图层需要绑定一个数据源,可以是本地的一个GeoJSON文件,也可以是在线的GeoJSON链接。在创建图层之后,使用Openlayers的ol.source.Vector创建一个新的矢量数据源,并将其绑定到新图层。 然后,使用Openlayers的ol.format.GeoJSON将数据转换为矢量要素,并将其添加到矢量数据源中。最后,将新图层添加Openlayers地图中。 下面是一个加载GeoJSON的代码示例: ```javascript // 创建一个新的地图对象 var map = new ol.Map({ target: 'map', // 地图显示的容器 layers: [ // 地图图层 new ol.layer.Tile({ // 街道地图 source: new ol.source.OSM() }) ], view: new ol.View({ // 地图视图 center: ol.proj.fromLonLat([119.306239, 26.080407]), // 地图中心点 zoom: 14 // 地图缩放级别 }) }); // 创建一个新的矢量数据源 var vectorSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), // 数据源格式 url: '/path/to/geojson/file.geojson' // GeoJSON文件路径 }); // 创建一个新的矢量图层 var vectorLayer = new ol.layer.Vector({ source: vectorSource // 图层数据源 }); // 将图层添加地图中 map.addLayer(vectorLayer); ``` 在上面的代码中,我们创建了一个地图对象,并在地图图层中添加了一个OSM街道地图。然后,我们创建了一个新的矢量数据源,并将其绑定到一个新图层中。最后,我们将图层添加地图中。 值得注意的是,在GeoJSON文件路径中使用了绝对路径。为了方便测试,我们可以将文件放在服务器的根目录下,然后使用绝对路径访问。在实际项目中,我们可能需要使用相对路径或者动态生成路径。 总之,Openlayers提供了简单易用的API,使得加载GeoJSON变得非常容易。在实际应用中,我们可以将GeoJSON数据和地图交互来打造各种应用,例如展示数据统计、可视化、分析等。 ### 回答3: OpenLayers是一种用于Web GIS开发的JavaScript框架,它支持多种数据格式的加载和显示,包括GeoJSONGeoJSON是一个用于地理数据交换的标准数据格式,它可以轻松地在WebGIS应用中实现地图数据的可视化。 在OpenLayers加载GeoJSON数据,需要使用ol.source.Vector类,该类是OpenLayers中用于加载和显示矢量数据的核心类。下面是一个加载GeoJSON数据的简单示例: ```javascript var vectorSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: 'path/to/file.geojson' }); var vectorLayer = new ol.layer.Vector({ source: vectorSource }); var map = new ol.Map({ layers: [vectorLayer], target: 'map', view: new ol.View({ center: [0, 0], zoom: 2 }) }); ``` 在这个示例中,我们首先创建了一个vectorSource对象,其中包含format和url两个属性,分别表示GeoJSON数据的格式和数据的路径。接着,我们使用vectorSource创建一个vectorLayer对象,并将其添加地图上。最后,我们创建一个map对象,并将vectorLayer添加到图层数组中,实现了GeoJSON数据的加载和显示。 除了使用url加载GeoJSON数据外,还可以直接使用JavaScript对象或字符串加载数据。这种方式需要将数据转换为GeoJSON格式,然后使用vectorSource.addFeatures(features)方法将数据添加到vectorSource中。 总之,OpenLayers提供了简单而又强大的方法来加载和显示GeoJSON数据,使得开发WebGIS应用变得更加容易和高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值