前端 vue 使用高德地图组件:(一)加载地图并设置自定义覆盖物图标

博主这里用了vue-admin-template作为前台模板做测试...

这里使用的是外引高德css和js的方式,因为是spa单页面应用,所以只有一个index.html,在其中添加上相关应用即可:

引入(那个key换成自己在高德申请的):

<link rel="stylesheet" href="https://cache.amap.com/lbs/static/main.css" />
    <script
      type="text/javascript"
      src="https://webapi.amap.com/maps?v=1.4.15&key=0000000key0000000&plugin=AMap.ToolBar,AMap.Marker,AMap.Driving,AMap.DistrictSearch,AMap.Autocomplete,AMap.PlaceSearch"
    ></script>

index.html 完整页面代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
    />
    <link rel="icon" href="<%= BASE_URL %>favicon.ico" />
    <link rel="stylesheet" href="https://cache.amap.com/lbs/static/main.css" />
    <script
      type="text/javascript"
      src="https://webapi.amap.com/maps?v=1.4.15&key=9022bbbcf0eecf156c3e3a8592f73330&plugin=AMap.ToolBar,AMap.Marker,AMap.Driving,AMap.DistrictSearch,AMap.Autocomplete,AMap.PlaceSearch"
    ></script>
    <title><%= webpackConfig.name %></title>
  </head>
  <body>
    <noscript>
      <strong
        >We're sorry but <%= webpackConfig.name %> doesn't work properly without
        JavaScript enabled. Please enable it to continue.</strong
      >
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

创建相关页面,并路由至此,地图页面代码:

<template>
  <div class="app-container">
    <div id="title">高德地图组件展示</div>
    <div id="mapsearch"></div>
    <div id="mapcontainer"></div>
    <div id="footer"></div>
  </div>
</template>
<script>
import location from "@/assets/image/location.png";
export default {
  data() {
    return {
      mapMain: null,
      lngMain: 0,
      latMain: 0,
      markerMain: null
    };
  },
  created() {},
  mounted() {
    this.initMapMain();
  },
  methods: {
    initMapMain() {
      //地图组件绑定div进行初始化并赋值给vue的对象
      this.mapMain = new AMap.Map("mapcontainer", {
        center: [this.lngMain, this.latMain], //设置显示的地图中心坐标
        zoom: 8 //设置地图缩放等级
      });
      //生成自定义的图标
      let icon = new AMap.Icon({
        image: location,
        size: new AMap.Size(25, 35),
        imageSize: new AMap.Size(25, 35)
      });
      //初始化覆盖物对象并赋值给vue对象
      this.markerMain = new AMap.Marker({
        icon: icon, //使用自定义的图标
        position: [this.lngMain, this.latMain] //覆盖物位置坐标
      });
      this.mapMain.add(this.markerMain); //添加覆盖物到地图,与下方法同效
      // this.markerMain.set(this.mapMain) //添加覆盖物到地图,与上方法同效
      this.mapMain.setFitView(); //地图缩放等级和位置等自适应,可以放置折线对象等作为形参也可以为空
    }
  }
};
</script>
<style scoped>
#mapcontainer {
  height: 300px;
  width: 500px;
  font-size: 13px;
}
</style>

使用的自定义图标:

成功加载后:

 地图展示因为坐标是0,0,加上自动缩放,所以显示的好像一片空白。其实没问题的,调节缩放后:

 地图组件的基本引用就是这样了,详细的功能都备注在了页面代码中,需要注意的是地图绑定的div一定要有宽高样式否则不能正常加载!

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值