[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>
相关推荐
天宇&嘘月2 小时前
web第三次作业
前端·javascript·css
小王不会写code2 小时前
axios
前端·javascript·axios
发呆的薇薇°4 小时前
vue3 配置@根路径
前端·vue.js
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker
前端郭德纲4 小时前
前端自动化部署的极简方案
运维·前端·自动化
海绵宝宝_5 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
码农土豆5 小时前
chrome V3插件开发,调用 chrome.action.setIcon,提示路径找不到
前端·chrome
鱼樱前端5 小时前
深入JavaScript引擎与模块加载机制:从V8原理到模块化实战
前端·javascript