[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>
相关推荐
大前端爱好者44 分钟前
React 19 新特性详解
前端
随云6321 小时前
WebGL编程指南之着色器语言GLSL ES(入门GLSL ES这篇就够了)
前端·webgl
寻找09之夏2 小时前
【Vue3实战】:用导航守卫拦截未保存的编辑,提升用户体验
前端·vue.js
多多米10053 小时前
初学Vue(2)
前端·javascript·vue.js
柏箱3 小时前
PHP基本语法总结
开发语言·前端·html·php
新缸中之脑3 小时前
Llama 3.2 安卓手机安装教程
前端·人工智能·算法
hmz8563 小时前
最新网课搜题答案查询小程序源码/题库多接口微信小程序源码+自带流量主
前端·微信小程序·小程序
看到请催我学习3 小时前
内存缓存和硬盘缓存
开发语言·前端·javascript·vue.js·缓存·ecmascript
blaizeer4 小时前
深入理解 CSS 浮动(Float):详尽指南
前端·css
编程老船长4 小时前
网页设计基础 第一讲:软件分类介绍、工具选择与课程概览
前端