[BD2.0] 在vue3中使用高德地图-非dialog

去除掉dialog弹窗, 直接展示地图, 下一步是做轨迹回放

相关文档

bash 复制代码
https://developer.amap.com/demo/javascript-api-v2/example/marker/replaying-historical-running-data
https://blog.csdn.net/qq_41339126/article/details/116596465
html 复制代码
<template>
    <div class="map-container">
      <div id="map" style="height: 800px; width: 80%;margin-left: 200px;" />
    </div>
  </template>
  
  <script>
  import { ref, onMounted } from 'vue'
  import AMapLoader from '@amap/amap-jsapi-loader'
  
  export default {
    setup() {
      const map = ref(null)
  
      const initMap = async () => {
        AMapLoader.load({
          key: '-', // 你的高德地图key
          version: '2.0',
          plugins: ['AMap.Marker'],
        })
          .then((AMap) => {
            map.value = new AMap.Map('map', {
              viewMode: '3D', // 是否为3D地图模式
              zoom: 15, // 初始化地图级别
              resizeEnable: true, // 是否监控地图容器尺寸变化
              mapStyle: 'amap://styles/normal',
              center: [116.361124, 39.959828], // 初始化地图中心点位置
              showMarker: true, // 定位成功后在定位到的位置显示点标记,默认:true
            })
  
            // 添加标记
            const marker = new AMap.Marker({
              position: [116.361124, 39.959828],
              icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
              draggable: true,
              // map: map.value,
            })
            map.value.add(marker)
          })
          .catch((e) => {
            console.log(e)
          })
      }
  
      onMounted(() => {
        window._AMapSecurityConfig = {
          securityJsCode: '-',
        }
        initMap() // 初始化地图
      })
  
      return {
        map,
        initMap,
      }
    },
  }
  </script>
  
  <style>
  .map-container {
    margin: 10px 0;
  }
  #map {
    height: 400px;
    width: 100%;
  }
  </style>
  • 添加标记接口
xml 复制代码
<template>
    <div class="map-container">
      <div id="map" style="height: 300px; width: 50%; margin-left: 200px;" />
      <el-button style="margin-left: 200px;" @click="addMarker">添加标记</el-button>
    </div>
  </template>
  
  <script>
  import { ref, onMounted } from 'vue'
  import AMapLoader from '@amap/amap-jsapi-loader'
  
  export default {
    setup() {
      const map = ref(null)
      let marker = null // 将 marker 变量移至 setup 函数外部
  
      const initMap = async () => {
        AMapLoader.load({
          key: '',
          version: '2.0',
          plugins: ['AMap.Marker'],
        })
          .then((AMap) => {
            map.value = new AMap.Map('map', {
              viewMode: '3D',
              zoom: 15,
              resizeEnable: true,
              mapStyle: 'amap://styles/normal',
              center: [116.361124, 39.959828],
              showMarker: true,
            })
  
            // 初始化标记
            marker = new AMap.Marker({
              position: [116.361124, 39.959828],
              icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
              draggable: true,
            })
            map.value.add(marker)
          })
          .catch((e) => {
            console.log(e)
          })
      }
  
      const addMarker = () => {
        if (map.value) {
          if (marker) {
            console.log("移除旧标记");
            map.value.remove(marker)
          }
          marker = new AMap.Marker({
            position: [116.361124, 39.959829],
            icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
            draggable: true,
          })
          map.value.add(marker)
        }
      }
  
      onMounted(() => {
        window._AMapSecurityConfig = {
          securityJsCode: '',
        }
        initMap()
      })
  
      return {
        addMarker,
      }
    },
  }
  </script> 
  
  <style>
  .map-container {
    margin: 10px 0;
  }
  
  #map {
    height: 400px;
    width: 100%;
  }
  </style>
  • 初步实现轨迹回复
xml 复制代码
<template>
    <div class="map-container">
      <div id="map" style="height: 300px; width: 50%; margin-left: 200px;"></div>
      <el-button style="margin-left: 200px;" @click="addMarker">添加标记</el-button>
      <div class="input-card">
        <h4>轨迹回放控制</h4>
        <div class="input-item">
          <input type="button" class="btn" value="开始动画" @click="startAnimation" />
          <input type="button" class="btn" value="暂停动画" @click="pauseAnimation" />
        </div>
        <div class="input-item">
          <input type="button" class="btn" value="继续动画" @click="resumeAnimation" />
          <input type="button" class="btn" value="停止动画" @click="stopAnimation" />
        </div>
      </div>
    </div>
  </template>
  
  <script>
  import { ref, onMounted } from 'vue'
  import AMapLoader from '@amap/amap-jsapi-loader'
  
  export default {
    setup() {
      const map = ref(null)
      let marker = null
      let polyline = null
      let passedPolyline = null
      const lineArr = [[116.478935,39.997761],[116.478939,39.997825],[116.478912,39.998549],[116.478912,39.998549],[116.478998,39.998555],[116.478998,39.998555],[116.479282,39.99856],[116.479658,39.998528],[116.480151,39.998453],[116.480784,39.998302],[116.480784,39.998302],[116.481149,39.998184],[116.481573,39.997997],[116.481863,39.997846],[116.482072,39.997718],[116.482362,39.997718],[116.483633,39.998935],[116.48367,39.998968],[116.484648,39.999861]];
      const initMap = async () => {

        AMapLoader.load({
          key: '-',
          version: '2.0',
          plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView',
            'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor', 'AMap.MarkerClusterer', 'AMap.MoveAnimation'],
        }).then((AMap) => {

            AMap.plugin('AMap.MoveAnimation', function(){});

            map.value = new AMap.Map('map', {
              viewMode: '3D',
              zoom: 15,
              resizeEnable: true,
              mapStyle: 'amap://styles/normal',
              center: [116.478935,39.997761],
              showMarker: true
            })
  
            // Initialize marker
            marker = new AMap.Marker({
              position: [116.478935,39.997761],
              icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
              draggable: true
            })
            map.value.add(marker)
  
            // Draw polyline
            polyline = new AMap.Polyline({
              map: map.value,
              path: lineArr,
              showDir: true,
              strokeColor: '#28F',
              strokeWeight: 6
            })
  
            passedPolyline = new AMap.Polyline({
              map: map.value,
              strokeColor: '#AF5',
              strokeWeight: 6
            })
  
            marker.on('moving', function (e) {
              passedPolyline.setPath(e.passedPath)
            })
  
            map.value.setFitView()
          })
          .catch((e) => {
            console.log(e)
          })
      }
  
      const addMarker = () => {
        if (map.value) {
          if (marker) {
            console.log('移除旧标记')
            map.value.remove(marker)
          }
          marker = new AMap.Marker({
            position: [116.361124, 39.959829],
            icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
            draggable: true
          })
          map.value.add(marker)
        }
      }
  
      const startAnimation = () => {
        if (marker) {
          marker.moveAlong(lineArr, 200)
        }
      }
  
      const pauseAnimation = () => {
        if (marker) {
          marker.pauseMove()
        }
      }
  
      const resumeAnimation = () => {
        if (marker) {
          marker.resumeMove()
        }
      }
  
      const stopAnimation = () => {
        if (marker) {
          marker.stopMove()
        }
      }
  
      onMounted(() => {
        window._AMapSecurityConfig = {
          securityJsCode: '-'
        }
        initMap()
      })
  
      return {
        addMarker,
        startAnimation,
        pauseAnimation,
        resumeAnimation,
        stopAnimation
      }
    }
  }
  </script>
  
  <style>
  .map-container {
    margin: 10px 0;
  }
  
  #map {
    height: 400px;
    width: 100%;
  }
  </style>
相关推荐
浮华似水15 分钟前
Javascirpt时区——脱坑指南
前端
王二端茶倒水18 分钟前
大龄程序员兼职跑外卖第五周之亲身感悟
前端·后端·程序员
_oP_i23 分钟前
Web 与 Unity 之间的交互
前端·unity·交互
钢铁小狗侠24 分钟前
前端(1)——快速入门HTML
前端·html
凹凸曼打不赢小怪兽1 小时前
react 受控组件和非受控组件
前端·javascript·react.js
狂奔solar1 小时前
分享个好玩的,在k8s上部署web版macos
前端·macos·kubernetes
qiyi.sky1 小时前
JavaWeb——Web入门(8/9)- Tomcat:基本使用(下载与安装、目录结构介绍、启动与关闭、可能出现的问题及解决方案、总结)
java·前端·笔记·学习·tomcat
清云随笔1 小时前
axios 实现 无感刷新方案
前端
鑫宝Code1 小时前
【React】状态管理之Redux
前端·react.js·前端框架
忠实米线2 小时前
使用pdf-lib.js实现pdf添加自定义水印功能
前端·javascript·pdf