欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3+高德地圖只展示指定市、區(qū)行政區(qū)域的地圖以及遮罩反向鏤空其他地區(qū)

 更新時(shí)間:2024年02月01日 15:04:05   作者:Fantasywt  
vue大屏項(xiàng)目開發(fā),客戶覺(jué)得地圖上的文字標(biāo)注太多了,要求地圖上只顯示省市等主要城市的標(biāo)注,這篇文章主要給大家介紹了關(guān)于vue3+高德地圖只展示指定市、區(qū)行政區(qū)域的地圖以及遮罩反向鏤空其他地區(qū)的相關(guān)資料,需要的朋友可以參考下

實(shí)現(xiàn)效果:

例如像這樣,只展示廈門的地圖,隱藏其他地方,點(diǎn)擊區(qū)域,在單獨(dú)展示區(qū),點(diǎn)擊返回回到總覽。 

一、獲取高德地圖的key,和密鑰

地址:高德控制臺(tái)具體的獲取流程就不教授了,百度很多

二、安裝

npm i @amap/amap-jsapi-loader --save

三、引入使用

要注意的是,因?yàn)槭褂昧诵姓?wù)搜索的api,它的search()方法要有回調(diào),就必須配置密鑰

點(diǎn)擊查看官方推薦的密鑰使用方式

      window._AMapSecurityConfig = {
        securityJsCode: "你的密鑰",
      };

主要使用的api: 行政區(qū)查詢服務(wù)(AMap.DistrictSearch)

完整代碼

<template>
  <div class="home_div">
    <div class="map-title">
      <h3>JSAPI Vue3地圖組件示例</h3>
      <div v-if="back" id="back" @click="showAllMap" style="cursor: pointer;">返回</div>
    </div>
    <div id="container"></div>
  </div>
</template>
<script lang="ts">
import AMapLoader from "@amap/amap-jsapi-loader";
import { shallowRef } from "@vue/reactivity";
import { ref } from "vue";
export default {
  name: "mapComtaint",
  setup() {
    const map = shallowRef(null) as any;
    const AMap = shallowRef(null) as any;
    const back = ref(false);
    const polygons = shallowRef([]) as any; // 區(qū)描邊、遮罩
    const district = shallowRef(null) as any;
    const polygon = shallowRef(null) as any; // 市描邊、遮罩
    return {
      map,
      AMap,
      back,
      polygons,
      district,
      polygon,
    };
  },
  create() {},
  methods: {
    /*  
    返回區(qū)域?qū)?yīng)顏色
    adcode:廈門市	        350200
            廈門市市轄區(qū)	350201
            思明區(qū)	        350203
            海滄區(qū)	        350205
            湖里區(qū)	        350206
            集美區(qū)	        350211
            同安區(qū)	        350212
            翔安區(qū)	        350213
            */
    getColorByAdcode(adcode: string) {
      let colors = {
        "350200": "#111111",
        "350201": "#456aed",
        "350203": "428cced",
        "350205": "#a456e1",
        "350206": "#123fee",
        "350211": "#666eee",
        "350212": "#963qde",
        "350213": "#784eda",
      } as any;
      return colors[adcode];
    },
    // 初始化遮罩
    initArea(city: string, isChildDraw: boolean = false) {
      let that = this;
      this.district.search(city, function (status: string, result: any) {
        // 外多邊形坐標(biāo)數(shù)組和內(nèi)多邊形坐標(biāo)數(shù)組
        var outer = [
          new that.AMap.LngLat(-360, 90, true),
          new that.AMap.LngLat(-360, -90, true),
          new that.AMap.LngLat(360, -90, true),
          new that.AMap.LngLat(360, 90, true),
        ];
        console.log(result);

        // 繪制遮罩
        var holes = result.districtList[0].boundaries;
        var pathArray = [outer];
        pathArray.push.apply(pathArray, holes);
        that.polygon = new that.AMap.Polygon({
          pathL: pathArray,
          strokeColor: "#00eeff",
          strokeWeight: 1,
          fillColor: "#020933",
          fillOpacity: 1,
        });
        that.polygon.setPath(pathArray);
        that.map.add(that.polygon);
        // 判斷是否要繪制子區(qū)域
        if (isChildDraw) {
            // 將搜索層級(jí)設(shè)置為 區(qū)、縣
          that.district.setLevel("district");
          for (let i = 0; i < result.districtList[0].districtList.length; i++) {
            that.areaPolyline(result.districtList[0].districtList[i].adcode);
          }
        }
      });
    },
    // 顯示總覽
    showAllMap() {
      this.back = false;
      if (this.polygon) {
        // 清除遮罩
        this.map.remove(this.polygon);
      }
      this.initArea("廈門市", true);
      this.map.setCenter([118.113994, 24.614998]);
      this.map.setZoom(11);
    },
    // 繪制區(qū)域描邊
    areaPolyline(adcode: string) {
      let that = this;
      if (this.polygons.length) {
        this.map.remove(this.polygons);
        this.polygons = [];
      }
      this.district.search(adcode, function (status: string, result: any) {
        console.log("區(qū)", result);
        //   繪制區(qū)域描邊
        let bounds = result.districtList[0].boundaries;
        for (let i = 0; i < bounds.length; i++) {
          const color = that.getColorByAdcode(result.districtList[0].adcode);
          const polygon = new that.AMap.Polygon({
            path: bounds[i],
            strokeColor: "#784eda",
            strokeWeight: 4,
            fillColor: "#784eda",
            fillOpacity: 0,
          });
          // 添加監(jiān)聽(tīng)事件
          polygon.on("mouseover", () => {
            if (!that.back) {
              polygon.setOptions({
                fillOpacity: 0.7,
              });
            }
          });
          // 添加點(diǎn)擊事件
          polygon.on("click", () => {
            // 判斷是否為市級(jí)
            if (!that.back) {
                // 顯示返回按鈕
              that.back = true;
              that.map.setZoom(12);
              // 修改中心位置為區(qū)級(jí)中心
              that.map.setCenter([
                result.districtList[0].center.lng,
                result.districtList[0].center.lat,
              ]);
            //   繪畫
              that.initArea(result.districtList[0].adcode, false);
            }
          });
          polygon.on("mouseout", () => {
            polygon.setOptions({
              fillOpacity: 0,
            });
          });
          that.polygons.push(polygon);
        }
        that.map.add(that.polygons);
      });
    },
    ininMap() {
      let that = this;
      // 這個(gè)配置很重要,必須設(shè)置,否則你的 行政服務(wù)搜索api無(wú)法使用生成回調(diào)
      window._AMapSecurityConfig = {
        securityJsCode: "密鑰",
      };
      AMapLoader.load({
        key: "你的key", //設(shè)置您的key
        version: "2.0",
        plugins: [
          "AMap.ToolBar",
          "AMap.Driving",
          "AMap.Polygon",
          "AMap.DistrictSearch",
          "AMap.Object3DLayer",
          "AMap.Object3D",
          "AMap.Polyline",
        ],
        AMapUI: {
          version: "1.1",
          plugins: [],
        },
        Loca: {
          version: "2.0.0",
        },
      })
        .then((AMap) => {
          that.AMap = AMap;
          that.map = new AMap.Map("container", {
            viewMode: "3D",
            zoom: 11,
            zooms: [10, 20],
            center: [118.113994, 24.614998],
          });
          that.district = new AMap.DistrictSearch({
            subdistrict: 3, //獲取邊界返回下級(jí)行政區(qū)
            extensions: "all", //返回行政區(qū)邊界坐標(biāo)組等具體信息
            level: "city", //查詢行政級(jí)別為 市
          });
          that.initArea("廈門市", true);
        })
        .catch((e) => {
          console.log(e);
        });
    },
  },
  mounted() {
    //DOM初始化完成進(jìn)行地圖初始化
    this.ininMap();
  },
};
</script>
<style scope>
.home_div {
  height: 100%;
  width: 100%;
  padding: 0px;
  margin: 0px;
  position: relative;
}
#container {
  height: 100vh;
  width: 100%;
  padding: 0px;
  margin: 0px;
}
.map-title {
  position: absolute;
  z-index: 1;
  width: 100%;
  height: 50px;
  background-color: rgba(27, 25, 27, 0.884);
  display: flex;
  justify-content: space-around;
  align-items: center;
}
h3 {
  z-index: 2;
  color: white;
}
</style>

效果圖如文章開頭圖片

總結(jié)

到此這篇關(guān)于vue3+高德地圖只展示指定市、區(qū)行政區(qū)域的地圖以及遮罩反向鏤空其他地區(qū)的文章就介紹到這了,更多相關(guān)vue3 高德地圖只展示指定行政區(qū)域內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue3之父子組件異步props數(shù)據(jù)的傳值方式

    Vue3之父子組件異步props數(shù)據(jù)的傳值方式

    這篇文章主要介紹了Vue3之父子組件異步props數(shù)據(jù)的傳值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 解決vue3項(xiàng)目打包后部署后某些靜態(tài)資源圖片不加載問(wèn)題

    解決vue3項(xiàng)目打包后部署后某些靜態(tài)資源圖片不加載問(wèn)題

    這篇文章主要給大家介紹了如何解決vue3項(xiàng)目打包后部署后某些靜態(tài)資源圖片不加載問(wèn)題,文中通過(guò)圖文結(jié)合的方式講解的非常詳細(xì),有需要的朋友可以參考下
    2024-05-05
  • vue3.0中使用element UI表單遍歷校驗(yàn)問(wèn)題解決

    vue3.0中使用element UI表單遍歷校驗(yàn)問(wèn)題解決

    本文主要介紹了vue3.0中使用element UI表單遍歷校驗(yàn)問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 讓你30分鐘快速掌握vue3教程

    讓你30分鐘快速掌握vue3教程

    這篇文章主要介紹了讓你30分鐘快速掌握vue3,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 詳解基于vue的移動(dòng)web app頁(yè)面緩存解決方案

    詳解基于vue的移動(dòng)web app頁(yè)面緩存解決方案

    這篇文章主要介紹了詳解基于vue的移動(dòng)web app頁(yè)面緩存解決方案,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-08-08
  • vue實(shí)現(xiàn)圖片懶加載的方法分析

    vue實(shí)現(xiàn)圖片懶加載的方法分析

    這篇文章主要介紹了vue實(shí)現(xiàn)圖片懶加載的方法,結(jié)合實(shí)例形式分析了vue.js圖片懶加載插件安裝、使用方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • Vuex與Vue router的使用詳細(xì)講解

    Vuex與Vue router的使用詳細(xì)講解

    在看這篇文章的幾點(diǎn)要求:需要你先知道Vuex與Vue-Router是個(gè)什么東西,用來(lái)解決什么問(wèn)題,以及它的基本使用。如果你還不懂的話,建議上官網(wǎng)了解下Vuex與Vue-Router的基本使用后再回來(lái)看這篇文章
    2022-11-11
  • element UI 中的 el-tree 實(shí)現(xiàn) checkbox 單選框及 bus 傳遞參數(shù)功能

    element UI 中的 el-tree 實(shí)現(xiàn) checkbox&n

    在日常項(xiàng)目開發(fā)中,會(huì)經(jīng)常遇到,樹形結(jié)構(gòu)的查詢方式,為了快速方便開發(fā),常常會(huì)使用到快捷的ui組件去快速搭樹形結(jié)構(gòu),這里我用的是 element ui 中的 el-tree,對(duì)element UI 中的 el-tree 實(shí)現(xiàn) checkbox 單選框及 bus 傳遞參數(shù)的方法感興趣的朋友跟隨小編一起看看吧
    2022-09-09
  • 詳解VUE-地區(qū)選擇器(V-Distpicker)組件使用心得

    詳解VUE-地區(qū)選擇器(V-Distpicker)組件使用心得

    這篇文章主要介紹了詳解VUE-地區(qū)選擇器(V-Distpicker)組件使用心得,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Vue項(xiàng)目全局配置微信分享思路詳解

    Vue項(xiàng)目全局配置微信分享思路詳解

    這篇文章給大家介紹了vue項(xiàng)目全局配置微信分享思路講解,使用vue作為框架,使用vux作為ui組件庫(kù),具體內(nèi)容詳情大家跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05

最新評(píng)論