高德地图按行政区划分填充色块

实现的效果:
在这里插入图片描述
项目地址:https://gitee.com/ling-xu/gaud-map-vue

关键代码:

<template>
  <div
    :id="idHash"
    class="container"
    style="z-index:1"
    @click="test()"
  />
</template>
<script>
import pin1 from '../assets/pin1.png'
import pin2 from '../assets/pin2.png'
import pin3 from '../assets/pin3.png'
import pin4 from '../assets/pin4.png'
import pin5 from '../assets/pin5.png'
import axios from 'axios'
export default {
  name: 'MapChart',
  props: {
    year: { type: String, default: '2021' },
    zoom: { type: Number, default: 4 },
    showData:{type:Boolean,default:false},
    locationList:Array,//产业数据-面店分布的地图显示传入数据
    getLocation:{type:Boolean,default:false}//首页-面店分布的图片显示
  },
  data: () => ({
    loading: false,
    idHash: 'MapContainer' + new Date().getTime(),
    disProvinces:'',
    map:{},
    depFeatures: ['bg', 'road', 'building', 'point']
  }),
  watch: {
    year() {
      this.refresh()
    },
    locationList(){
      this.refresh()
    },
  },
  mounted() {
    if (window.AMap === undefined) {
      const script = document.createElement('script')
      script.src = 'https://webapi.amap.com/maps?v=1.4.15&plugin=AMap.MarkerClusterer,Map3D,AMap.DistrictLayer,AMap.DistrictSearch&callback=initAMap&key=de45c1a0e7ea44bea49388cea9cca2f7'
      document.head.appendChild(script)
      window.initAMap = () => {
        this.refresh()
      }
    } else {
      this.refresh()
    }
  },
  methods: {
    refresh() {
      let that=this
      if (!window.AMap) {
        return
      }
      const opts = {
        subdistrict: 0,
        extensions: 'all',
        level: 'province'
      };
      //直接通过经纬度构建mask路径
      const district = new AMap.DistrictSearch(opts);
      district.search('青海省', function(status, result) {
          const bounds = result.districtList[0].boundaries;
          const mask = []
          for(let i =0;i<bounds.length;i+=1){
              mask.push([bounds[i]])
          }
          const Map = new AMap.Map(that.idHash, {
              mask:mask,//只显示包裹起来的区域
              resizeEnable: false,
              center: [96.475745,35.841084],
              viewMode:'3D',
              // pitch:5,
              zoom:that.zoom,
              features: [],//初始色块模式下,不显示标注等信息
              mapStyle: 'amap://styles/021981e1781074e215441507a954df4b', //设置地图的显示样式
          });

          that.map=Map//把这里面创建的地图对象存起来,让这个指针指向它,后续要使用

          //点聚合数据处理
          if(that.showData){
            // 地图的数据由父组件传入
            that.updateMark(Map, that.locationList)
          }else if(that.getLocation){
            // 首页-面店分布的图片显示
            axios({
                url: 'diagram/getShopDistribute2',
                method: 'get', // default
                baseURL: 'http://47.115.140.114:5001/api/report/',
                headers: {'x-user-token': 'eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiYW5rZm9ydGVzdDAwMSIsInN1YiI6ImxvZ2luLmxvZ2luIiwiaWF0IjoxNjI0NDY0MzkwLCJhdXRob3JpemF0aW9uIjp7fSwiYWFhIjoiYWFhIiwiZGVwdE5hbWUiOiLpnZLmtbfmi4npnaLkuqfkuJrnu7zlkIjmnI3liqHlubPlj7AiLCJsb2dpblRpbWUiOiIyMDIxLTA2LTI0IDAwOjA2OjMwIiwidW5pdE5hbWUiOiLpnZLmtbfmi4npnaLkuqfkuJrnu7zlkIjmnI3liqHlubPlj7AiLCJwcm92aW5jZSI6IjQ0MDAwMCIsImNpdHkiOiI0NDAxMDAiLCJ1bml0Q29kZSI6ImxhbWlhbiIsInJlZGlzVG9rZW4iOiI2NmExOTA4Yy0yYjcwLTRjYmQtYTI0ZS0xMjFiODBiZTEzMzQiLCJ0ZW5hbnRJZCI6ImxhbWlhbiIsInN0YWZmVHlwZTIiOiI0IiwiZGVwdENvZGUiOiJsYW1pYW4iLCJleHAiOjE2MjQ0NjYxOTAsInVzZXJuYW1lIjoiYmFua2ZvcnRlc3QwMDEifQ.2H3Mcjyqqmeqs2WRbfugmZQQGmzJw5cRnGDND_NKmAM'},
            }).then(res => {
              that.updateMark(Map, res.data.locationList)
              console.log("aaa",res.data.locationList)
            })
          }
          //按行政区填充色块
          that.initPro(Map)
          //按行政区描边
          that.borderLine('海西蒙古族藏族自治州',Map)
          that.borderLine('海东市',Map)
          that.borderLine('海南藏族自治州',Map)
          that.borderLine('海北藏族自治州',Map)
          that.borderLine('果洛藏族自治州',Map)
          that.borderLine('黄南藏族自治州',Map)
          that.borderLine('玉树藏族自治州',Map)
          that.borderLine('西宁市',Map)
          
      });
    },

    // 创建市区的颜色块
    initPro(map) {
        const that=this
        const code = 630000;//青海省代码
        const dep = 1;  //按市区划分
        var disProvince=this.disProvinces
        disProvince && disProvince.setMap(null);
        disProvince = new AMap.DistrictLayer.Province({
            zIndex: 12,
            adcode: [code],
            depth: dep,
            styles: {
                'fill': function (properties) {
                    // properties为可用于做样式映射的字段,包含
                    // NAME_CHN:中文名称
                    // adcode_pro
                    // adcode_cit
                    // adcode
                    var adcode = properties.adcode;
                    return that.getColorByAdcode(adcode);
                },
                // 'province-stroke': 'cornflowerblue',
                'city-stroke': '#3078AC', // 中国地级市边界
                // 'county-stroke': 'rgba(255,255,255,0.5)' // 中国区县边界
            }
        });
        disProvince.setMap(map);
    },

    // 颜色辅助方法
    getColorByAdcode(adcode) {
        const colors={
          630100: "#2c54cf",
          630200: "#17307c",
          632200: "#204699",
          632300: "#1c3077",
          632500: "#2b47ac",
          632600: "#17307c",
          632700: "#204699",
          632800: "#17307c",
        }
        return colors[adcode];
    },

    /**
     * 更新标记点
     */
    updateMark(Map, points) {
      // 位置标记
      const Markers = []
      for (var i = 0; i < points.length; i += 1) {
        if (points[i]) {
          Markers.push(new AMap.Marker({
            position: points[i].split(','),
            offset: new AMap.Pixel(-15, -15)
          }))
        }
      }
      let str=[{
        url: pin5,
        size: new AMap.Size(100, 100),
        offset: new AMap.Pixel(-100, -100),
        textSize:40,
        textColor:'#353535'
      }, {
        url: pin4,
        size: new AMap.Size(100, 100),
        offset: new AMap.Pixel(-100, -100),
        textSize:40,
        textColor:'#353535'
      }, {
        url: pin3,
        size: new AMap.Size(100, 100),
        offset: new AMap.Pixel(-100, -100),
        textSize:40,
        textColor:'#353535'
      }, {
        url: pin2,
        size: new AMap.Size(100, 100),
        offset: new AMap.Pixel(-100, -100),
        textSize:40,
        textColor:'#353535'
      }, {
        url: pin1,
        size: new AMap.Size(100, 100),
        offset: new AMap.Pixel(-100, -100),
        textSize:40,
        textColor:'#353535'
      }]
      new AMap.MarkerClusterer(Map, Markers, {
        styles: str,
        gridSize: 60,
        minClusterSize:1
      })
    },
    //行政区描边的功能
    borderLine(city,Map){
      const opts = {
        subdistrict: 0,
        extensions: 'all',
        level: 'city'
      };
       //直接通过经纬度构建mask路径
      const district = new AMap.DistrictSearch(opts);
      district.search(city, function(status, result) {
          const bounds = result.districtList[0].boundaries;
           //添加描边
          for(let i =0;i<bounds.length;i+=1){
              new AMap.Polyline({
                  path:bounds[i],
                  strokeColor:'#1a77aa',
                  strokeWeight:10,
                  map:Map
              })
          }
      })
    },
    //重新给地图增加标注信息
    setFeature(Map){
      const _that=this
      Map.setFeatures(_that.depFeatures);
    },
    test(){
      this.setFeature(this.map)
    }
  }
}
</script>
<style lang='less' scoped>
.container {
  height: 100%;
}
</style>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 高德地图行政区划集合是指在 Vue 框架中使用高德地图 API 来展示和管理行政区划相关的功能和数据。 首先,在使用 Vue 框架中可以通过安装高德地图相关的插件来引入高德地图 API ,可以使用 npm 或者 yarn 来进行安装。然后在 Vue 组件中使用 import 引入地图相关的组件。 其次,可以使用高德地图提供的行政区划 API 来获取各级行政区划的数据。可以根据需要选择完整的行政区划数据还是特定范围的行政区划数据。获取到数据后,可以在 Vue 组件中进行处理和展示,例如使用 v-for 指令循环遍历数据,生成相应的列表或者地图区域。 在展示行政区划数据的过程中,可以利用高德地图的相关功能进行交互和样式定制。比如可以使用高德地图提供的 GeoJSON 数据和样式进行地图的绘制,根据行政区划的不同级别设置不同的显示效果。 对于行政区划数据的操作,可以使用高德地图提供的方法进行搜索、过滤和排序等功能。可以根据用户的搜索条件来展示特定的行政区划数据,并将结果反馈给用户。 除此之外,还可以结合 Vue 的双向绑定和事件机制来实现一些业务逻辑。比如根据用户的选择显示相应的行政区划信息,或者在地图上通过点击或者拖动来交互地选择特定的行政区划。 总结来说,Vue 高德地图行政区划集合是通过结合 Vue 框架和高德地图 API 来展示和管理行政区划相关功能和数据的一个解决方案。通过使用相关的插件和方法,可以实现用户界面的展示和交互效果,以及对行政区划数据的操作和处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值