效果图:

  

实现代码:

<template>
  <div id="map" ref="map" style="width: 100vw; height: 100vh"></div>
</template>
 
<script>
import "ol/ol.css";
import { map, view } from "ol";
import { osm, vector as vectorsource } from "ol/source";
import { vector as vectorlayer, tile as tilelayer } from "ol/layer";
import geojson from "ol/format/geojson";
 
import select from "ol/interaction/select";
import { altkeyonly, click, pointermove } from "ol/events/condition";
 
export default {
  name: "gif",
  data() {
    return {
      map: {},
      layer: {},
      geojsondata: {
        type: "featurecollection",
        features: [
          {
            type: "feature",
            properties: {
              title: "警报1",
            },
            geometry: {
              type: "point",
              coordinates: [91.48879670091165, 37.83814884701121],
            },
          },
          {
            type: "feature",
            properties: {
              title: "警报2",
            },
            geometry: {
              type: "point",
              coordinates: [99.19515576149941, 26.713646654711134],
            },
          },
          {
            type: "feature",
            properties: {
              title: "警报3",
            },
            geometry: {
              type: "point",
              coordinates: [123.74363825288785, 44.363694825734726],
            },
          },
        ],
      },
      select: {},
    };
  },
  mounted() {
    this.initmap();
  },
  methods: {
    // 初始化地图
    initmap() {
      this.layer = new vectorlayer({
        source: new vectorsource({
          features: new geojson().readfeatures(this.geojsondata),
        }),
      });
      this.map = new map({
        target: "map",
        layers: [
          new tilelayer({
            source: new osm(),
          }),
          this.layer,
        ],
        view: new view({
          projection: "epsg:4326",
          center: [104.912777, 34.730746],
          zoom: 4.5,
        }),
      });
 
      this.select = new select({
        condition: click, //单击选择
      });
      this.map.addinteraction(this.select);
      this.select.on("select", (e) => {
        let coordinate = e.mapbrowserevent.coordinate; //获取选择的坐标
        let properties = e.selected[0].getproperties(); //获取当前要素的所有属性
      });
 
      // 设置鼠标划过矢量要素的样式
      this.map.on("pointermove", (e) => {
        const ishover = this.map.hasfeatureatpixel(e.pixel);
        this.map.gettargetelement().style.cursor = ishover ? "pointer" : "";
      });
    },
  },
};
</script>

到此这篇关于vue+openlayer中使用select选择要素的实现代码的文章就介绍到这了,更多相关vue openlayer选择要素内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!