最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

二、安裝

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) {
            // 將搜索層級設(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)聽事件
          polygon.on("mouseover", () => {
            if (!that.back) {
              polygon.setOptions({
                fillOpacity: 0.7,
              });
            }
          });
          // 添加點(diǎn)擊事件
          polygon.on("click", () => {
            // 判斷是否為市級
            if (!that.back) {
                // 顯示返回按鈕
              that.back = true;
              that.map.setZoom(12);
              // 修改中心位置為區(qū)級中心
              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無法使用生成回調(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, //獲取邊界返回下級行政區(qū)
            extensions: "all", //返回行政區(qū)邊界坐標(biāo)組等具體信息
            level: "city", //查詢行政級別為 市
          });
          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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue計(jì)算屬性computed與方法methods的區(qū)別及說明

    Vue計(jì)算屬性computed與方法methods的區(qū)別及說明

    這篇文章主要介紹了Vue計(jì)算屬性computed與方法methods的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • vue強(qiáng)制刷新組件的方法示例

    vue強(qiáng)制刷新組件的方法示例

    這篇文章主要介紹了vue強(qiáng)制刷新組件的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Vue3使用ref與reactive創(chuàng)建響應(yīng)式對象的示例代碼

    Vue3使用ref與reactive創(chuàng)建響應(yīng)式對象的示例代碼

    這篇文章主要詳細(xì)介紹了Vue3使用ref與reactive創(chuàng)建響應(yīng)式對象的方法步驟,文中通過代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2024-02-02
  • Template?ref在Vue3中的實(shí)現(xiàn)原理詳解

    Template?ref在Vue3中的實(shí)現(xiàn)原理詳解

    這篇文章主要為大家介紹了Template?ref在Vue3中的實(shí)現(xiàn)原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • vue項(xiàng)目引入Iconfont圖標(biāo)庫的教程圖解

    vue項(xiàng)目引入Iconfont圖標(biāo)庫的教程圖解

    這篇文章主要介紹了vue項(xiàng)目引入Iconfont圖標(biāo)庫的相關(guān)知識,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-10-10
  • element-plus 在vue3 中不生效的原因解決方法(element-plus引入)

    element-plus 在vue3 中不生效的原因解決方法(element-plus引入)

    這篇文章主要介紹了element-plus 在vue3 中不生效的原因解決方法(element-plus引入),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • 詳解在vue-cli中使用graphql即vue-apollo的用法

    詳解在vue-cli中使用graphql即vue-apollo的用法

    這篇文章主要介紹了詳解在vue-cli中使用graphql即vue-apollo的用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • 關(guān)于element-ui resetFields重置方法無效問題及解決

    關(guān)于element-ui resetFields重置方法無效問題及解決

    這篇文章主要介紹了關(guān)于element-ui resetFields重置方法無效問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 詳解Vue的異步更新實(shí)現(xiàn)原理

    詳解Vue的異步更新實(shí)現(xiàn)原理

    這篇文章主要介紹了Vue的異步更新實(shí)現(xiàn)原理,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • Vue項(xiàng)目中l(wèi)oading卡死的問題及解決方案

    Vue項(xiàng)目中l(wèi)oading卡死的問題及解決方案

    文章剖析Vue項(xiàng)目中l(wèi)oading卡死問題,揭示其本質(zhì)為異步阻塞與響應(yīng)式渲染沖突,提出分五層解決方案:nextTick強(qiáng)制刷新、任務(wù)分片/Worker解耦、組件封裝、組合式API管理狀態(tài)、性能優(yōu)化與架構(gòu)調(diào)整,強(qiáng)調(diào)需從UI、異步、計(jì)算等多維度優(yōu)化
    2025-07-07

最新評論

牡丹江市| 赣州市| 定兴县| 水城县| 塔河县| 威信县| 宣武区| 洮南市| 洮南市| 洛隆县| 沾益县| 柳河县| 广河县| 扶余县| 横峰县| 安宁市| 沁阳市| 景东| 阳信县| 汉川市| 博白县| 洞头县| 蒙阴县| 康平县| 资阳市| 峨眉山市| 灵石县| 白城市| 修文县| 汨罗市| 肥城市| 荔浦县| 通河县| 手游| 襄垣县| 张家川| 犍为县| 武隆县| 通城县| 沈丘县| 东乡族自治县|