iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue引入高德地图并触发实现多个标点的示例详解
  • 200
分享到

Vue引入高德地图并触发实现多个标点的示例详解

2024-04-02 19:04:59 200人浏览 八月长安
摘要

目录1、 在public下的index.html中引入地图2、引入组件设置宽高100%3、数组形式数据固定(一)4、用ajax请求后端真是接口(二)5、其他需求请看文档请看官方文档1

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

<link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.CSS" rel="external nofollow"   />
<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、其他需求请看文档请看官方文档

lbs.amap.com/api/javascr…

综上就是简答使用高德地图分全部过程,具体需求请参照高德官方api

到此这篇关于Vue引入高德地图并触发实现多个标点的文章就介绍到这了,更多相关vue高德地图触发多个标点内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Vue引入高德地图并触发实现多个标点的示例详解

本文链接: https://www.lsjlt.com/news/150013.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作