高德地图组件使用总结

最近项目中使用到了高德地图组件,还用到了打点功能,这里总结一下:

效果如下:

在这里插入图片描述
登录进高德开放平台后可以查看相关api文档

<template>
  <div id="mapcontainer"></div>
</template>

<script setup lang="ts">
import { onMounted,  } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";
import emitter from "@/util/bus.js";
      interface pointer{
        lng: number
        lat: number
        name: string
      }
      let markeList = []
const init = () => {
  AMapLoader.load({
    key: "" , // 自己的key
    version: "2.0",
    plugins: ['AMap.DistrictSearch'],
  })
    .then((AMap) => {
     let map = new AMap.Map("mapcontainer", {
        mapStyle: "amap://styles/darkblue",
        center: [120.575642, 27.881179],
        zoom: 9,
      });
    let adCode = 330300;
    let depth = 2;
    let disProvince;
      function initPro(code, dep) {
        dep = typeof dep == 'undefined' ? 2 : dep;
        adCode = code;
        depth = dep;

        disProvince && disProvince.setMap(null);

        disProvince = new AMap.DistrictLayer.Province({
            zIndex: 12,
            adcode: [code],
            depth: dep,
            styles: {
                'fill': function (properties) {
                    var adcode = properties.adcode;
                    return getColorByAdcode(adcode);
                },
                'province-stroke': 'cornflowerblue',
                'city-stroke': 'white', // 中国地级市边界
                'county-stroke': 'rgba(255,255,255,0.5)' // 中国区县边界
            }
        });

        disProvince.setMap(map);
    }
    let colors = {};
    let getColorByAdcode = function (adcode) {
        if (!colors[adcode]) {
            var gb = Math.random() * 155 + 50;
            colors[adcode] = 'rgb(' + gb + ',' + gb + ',255)';
        }

        return colors[adcode];
    };
     initPro(adCode, depth)
      emitter.on("point", (res: pointer[]) => {
        if (res.length) {
          if(markeList.length){
            map.remove(markeList)
            markeList.length = 0
          }

          res.forEach((item, index) => {
            markeList.push(
              new AMap.Marker({
                position: new AMap.LngLat(item.lng, item.lat),
                title: item.name,
                icon: '/img/dot2.png'
              })
            )
          })
          map.add(markeList)
        }
      });
    })
    .catch((e) => {
      console.log(e);
    });
};

onMounted(() => {
  init();
});
</script>

<style lang="less" scoped>
#mapcontainer {
  height: 100%;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值