返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue+Openlayer中使用select选择要素的实现代码
  • 634
分享到

Vue+Openlayer中使用select选择要素的实现代码

2024-04-02 19:04:59 634人浏览 泡泡鱼
摘要

效果图:    实现代码: <template> <div id="map" ref="map" style="width: 100vw

效果图:

  

实现代码:


<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选择要素内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Vue+Openlayer中使用select选择要素的实现代码

本文链接: https://lsjlt.com/news/133728.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作