openlayers中国地图省市悬停高亮

目录

1、效果图   

2、核心代码

3、完整代码

1、效果图   

2、核心代码

    // 悬停高亮核心代码
    mouseHover () {
      const selected = new Style({
        fill: new Fill({ color: '#FFFFFF00' }),
        stroke: new Stroke({color: '#e90880', width: 3})
      });
      const selectPointerMove = new Select({
        condition: pointerMove, //鼠标移动
        style: selected,
      });
      this.map.addInteraction(selectPointerMove);
    }

3、完整代码

<!--世界地图 - 使用交互进行悬停高亮-->
<script>
import { ref } from 'vue'
import {Map, View} from 'ol'
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer"
import Feature from "ol/Feature"
import { OSM, Vector as VectorSource, XYZ } from "ol/source"
import { defaults as DefaultsControl } from "ol/control"
import GeoJSON from "ol/format/GeoJSON"
import { fromLonLat, transform } from "ol/proj"
import { MultiPolygon, Polygon } from "ol/geom"
import areaGeo from "../../assets/resource/china.json"
import { Fill, Stroke, Style, Text } from "ol/style"
import { pointerMove } from 'ol/events/condition'
import Select from 'ol/interaction/Select'
export default {
  name: 'tree',
  data () {
    return {
      map: null,
      count: ref(0),
      previousFeature: null
    }
  },
  components: {},
  methods: {
    initMap () {
      this.map = new Map({
        view: new View({
          center: transform([113.304788, 28.013391], "EPSG:4326", "EPSG:3857"), //两个投影之间进行坐标转换
          // center: fromLonLat([112.93, 28.23], "EPSG:3857"), //地图中心坐标
          zoom: 5, //缩放级别
          // rotation: Math.PI / 2
        }),
        layers: [
          new TileLayer({
            title: 'worldLayer',
            source: new XYZ({
              // url: 'https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}'
              // 高德
              url: 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}'
              // 阿里:https://datav.aliyun.com/portal/school/atlas/area_selector#&lat=31.769817845138945&lng=104.29901249999999&zoom=4
            }),
          })
        ],
        target: 'map', //对应绑定地图div的id标签 - map
        controls: DefaultsControl({
          rotate: false,
          zoom: false
        })
      });
    },
    initOverlay () {
      // 获取边界的经纬度数据
      let features = [];
      let routeFeature = null;
      areaGeo.features.forEach(lineData => {
        // 将每一个地域设置为一个feature
        if (lineData.geometry.type === 'MultiPolygon') {
          routeFeature = new Feature({
            geometry: new MultiPolygon(
                lineData.geometry.coordinates
            ).transform("EPSG:4326", "EPSG:3857")
          });
        } else if (lineData.geometry.type === 'Polygon') {
          routeFeature = new Feature({
            geometry: new Polygon(
                lineData.geometry.coordinates
            ).transform("EPSG:4326", "EPSG:3857"),
          });
        }
        const coverStyle = new Style({
          fill: new Fill({ color: '#FFFFFF10' })
        })
        routeFeature.setStyle(coverStyle);
        features.push(routeFeature);
      });

      let routeLayer = new VectorLayer({
        source: new VectorSource({
          features: features
        })
      })
      this.map.addLayer(routeLayer);
    },
    // 悬停高亮核心代码
    mouseHover () {
      const selected = new Style({
        fill: new Fill({ color: '#FFFFFF00' }),
        stroke: new Stroke({color: '#e90880', width: 3})
      });
      const selectPointerMove = new Select({
        condition: pointerMove,
        style: selected,
      });
      this.map.addInteraction(selectPointerMove);
    }
  },
  mounted() {
    this.initMap()
    this.initOverlay()
    this.mouseHover()
  }
}
</script>

<template>
  <div class="content">
    <!--  <button @click="count++">Count is: {{ count }}</button>  -->
    <!--  绑定map地图  -->
    <div id="map" ref="map"></div>
  </div>
</template>

<style scoped>
.content{
  width: 100%;
  height: 100%;
  #map {
    width: calc(100vw);
    height: calc(100vh);
  }
  button {
    font-weight: bold;
  }
}
</style>

参考:

openlayers6【十七】vue VectorLayer矢量图层画地图省市区,多省市区(粤港澳大湾区)效果详解_vue-openlayers根据省份边界贴图-CSDN博客

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值