vue3 引入高德地图api,自定义地图样式

话不多说,上代码

public/index.html加如下代码

<script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: '你的安全密钥',
    }
  </script>
//现在高德需要安全密钥和key配合使用

业务代码如下

<!-- 高德地图展示 -->
<template>
  <div id="allmap"></div>
</template>

<script>
import { defineComponent, onMounted } from "vue";
export default defineComponent({
  setup() {
    onMounted(() => {
      loadMapScript(); // 加载百度地图资源
    });
    const init = () => {
      let AMap = window.AMap;
      var map = new AMap.Map("allmap", {
        zoom: 11, //级别
        center: [104.04257198566438, 30.556193037336854], //中心点坐标
        mapStyle: "amap://styles/0b32011631047839586faf6390104fd1", //自定义地图的显示样式
      });
      console.log("map", map);
    };
    const loadMapScript = () => {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.className = "loadmap"; // 给script一个类名
      script.src =
        "https://webapi.amap.com/maps?v=2.0&key=您的高德key";
      script.onload = () => {
        init();
      };
      let loadmap = document.getElementsByClassName("loadmap");
      if (loadmap) {
        // 每次append script之前判断一下,避免重复添加script资源标签
        for (var i = 0; i < loadmap.length; i++) {
          document.body.removeChild(loadmap[i]);
        }
      }
      document.body.appendChild(script);
    };
  },
});
</script>
<style lang='less' scoped>
#allmap {
  // 注意给dom宽高,不然地图不出来
  width: 100%;
  height: 100%;
}
</style>

效果图:

 ps:开发建议使用高德,因为在使用自定义地图时,我对比了百度地图和高德地图,百度地图有明细卡顿,放大缩小时,会卡出若干白块,严重影响用户体验,高德体验明显好一些

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue高德地图自定义标记可以通过以下步骤实现: 1. 在Vue组件中引入高德地图API: ``` import AMapLoader from '@amap/amap-jsapi-loader'; ``` 2. 在Vue组件中定义地图容器,并在mounted钩子函数中加载地图: ``` <template> <div id="map-container"></div> </template> <script> export default { data() { return { map: null, }; }, mounted() { AMapLoader.load({ key: 'your-api-key', version: '2.0', plugins: ['AMap.Marker'], }).then(() => { this.initMap(); }); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); }, }, }; </script> ``` 3. 在Vue组件中定义自定义标记的样式,并使用AMap.Marker类创建自定义标记对象: ``` <template> <div id="map-container"></div> </template> <script> export default { data() { return { map: null, markerStyle: { width: '24px', height: '24px', background: 'url("your-marker-icon.png") no-repeat', backgroundSize: '100% 100%', }, }; }, mounted() { AMapLoader.load({ key: 'your-api-key', version: '2.0', plugins: ['AMap.Marker'], }).then(() => { this.initMap(); this.addMarker(); }); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); }, addMarker() { const marker = new AMap.Marker({ position: [116.397428, 39.90923], map: this.map, icon: new AMap.Icon(this.markerStyle), }); }, }, }; </script> ``` 4. 根据需要,可以在自定义标记上添加事件监听器: ``` <template> <div id="map-container"></div> </template> <script> export default { data() { return { map: null, markerStyle: { width: '24px', height: '24px', background: 'url("your-marker-icon.png") no-repeat', backgroundSize: '100% 100%', }, }; }, mounted() { AMapLoader.load({ key: 'your-api-key', version: '2.0', plugins: ['AMap.Marker'], }).then(() => { this.initMap(); this.addMarker(); }); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); }, addMarker() { const marker = new AMap.Marker({ position: [116.397428, 39.90923], map: this.map, icon: new AMap.Icon(this.markerStyle), }); marker.on('click', () => { alert('Marker clicked!'); }); }, }, }; </script> ``` 以上就是Vue高德地图自定义标记的实现方法。需要注意的是,自定义标记的样式需要根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值