前端Vue如何套入高德地图、并触发实现多个标点

1、 在public下的index.html中引入地图

<link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>
<script src="https://webapi.amap.com/maps?v=1.4.15&key=申请的key"></script>

2、引入组件设置宽高100%

<template>
  <div>
    <div id="container" style="width: 100%;height: 550px"></div>
  </div>
</template>

3、数组形式数据固定(一)

<script>
export default {
  data() {
    return {
    //要标记的所有点的经纬度
      lnglats: [
        [108.909074, 34.254225],
        [108.910766, 34.254348],
        [108.910495, 34.253531],
        [108.909502, 34.253571],
      ],
    }
  },
  mounted() {
    this.carGPSIP()
  },
  methods: {
    carGPSIP() {
      var map = new AMap.Map("container", {resizeEnable: true});//初始化地图
      //信息窗口实例
      var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
      //遍历生成多个标记点
      for (var i = 0, marker; i < this.lnglats.length; i++) {
        var marker = new AMap.Marker({
          position: this.lnglats[i],//不同标记点的经纬度
          map: map
        });
        marker.content = '我是第' + (i + 1) + '个Marker';
        marker.on('click', markerClick);
        marker.emit('click', {target: marker});//默认初始化不出现信息窗体,打开初始化就出现信息窗体
      }
      function markerClick(e) {
        infoWindow.setContent(e.target.content);
        infoWindow.open(map, e.target.getPosition());
      }
      map.setFitView();
      }
    },
}
</script>

4、用ajax请求后端真是接口(二)

<template>
      <div id="container" style="width: 100%;height: 550px"></div>  
      <!-- 设置宽和高 -->
</template>

<script>
export default {
  data() {
    return {
    //要标记的所有点的经纬度
      Coordinate:[]
      //  Coordinate:[
      //    {
      //      lng:"54.323243",
      //      lat:"43.654322"
      //    }
      //  ]  //后端返回的数据格式

    }
  },
  mounted() {
    this.carGPSIP()
  },
  methods: {
    carGPSIP() {
      var map = new AMap.Map("container", {resizeEnable: true});//初始化地图
      //信息窗口实例
      var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
      //遍历生成多个标记点 因后端返回是map格式因此需要判断code
    $ajax.positionType({}, ({ code, data }) => {
        if (code == 200) {
          console.log(data);
          this. Coordinate = data.deviceList; //拿到数据
          let  Coordinate = data.deviceList;  //定义Coordinate
          for (var i = 0; i < this. Coordinate.length; i++) {
            var marker = new AMap.Marker({
              position: new AMap.LngLat( Coordinate[i].lng,  Coordinate[i].lat), //不同标记点的经纬度
              map: map,
            });
             marker.content = '我是第' + (i + 1) + '个Marker';
            marker.on("click", markerClick);
            marker.emit("click", { target: marker }); //默认初始化不出现信息窗体,打开初始化就出现信息窗体
          }
          function markerClick(e) {
            infoWindow.setContent(e.target.content);
            infoWindow.open(map, e.target.getPosition());
          }
          map.setFitView();
        }
      });
      function markerClick(e) {
        infoWindow.setContent(e.target.content);
        infoWindow.open(map, e.target.getPosition());
      }
      map.setFitView();
      }
    },
}
</script>


<style>

</style>

5、其他需求请看文档请看官方文档

https://lbs.amap.com/api/javascript-api/summary/

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张清悠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值