腾讯地图+vue实现后台设置某外卖店铺的位置坐标

1、开发需求

在vue后台管理系统实现定位选点(位置),即后台设置某个外卖店铺的位置坐标。

2、效果展示

3、技术选择

使用腾讯地图的JavaScript API GL在vue框架中实现,一个vue页面即可搞定。

官方文档地址:https://lbs.qq.com/webApi/javascriptGL/glGuide/glOverview

4、代码

复制代码
<template>
  <div id="container"></div>
</template>

<script>
export default {
  data() {
    return {}
  },
  mounted() {
    this.loadTMapScript().then(() => {
      // 脚本加载完成后可以安全使用 TMap
      this.initMap()
    }).catch(err => {
      console.error('加载 TMap 脚本失败:', err)
    })
  },
  methods: {
    loadTMapScript() {
      return new Promise((resolve, reject) => {
        if (window.TMap) {
          // 如果 TMap 已经加载,直接 resolve
          resolve()
        } else {
          const script = document.createElement('script')
          script.src = 'https://map.qq.com/api/gljs?v=1.exp&key=xxx-xxx-xxxx' // key请替换为你的腾讯地图key
          script.onload = () => {
            const TMapCheck = window.TMap // 确认 TMap 是否已存在
            if (TMapCheck) {
              resolve() // 脚本加载成功
            } else {
              reject(new Error('TMap 未定义'))
            }
          }
          script.onerror = () => {
            reject(new Error('脚本加载失败'))
          }
          document.head.appendChild(script)
        }
      })
    },
    initMap() {
      // 检查 TMap 是否已加载
      if (window.TMap) {
        this.map = new window.TMap.Map('container', {
          center: new window.TMap.LatLng(39.916527, 116.397128), // 设置中心坐标
          zoom: 18,
        })
        const { map } = this
        // 初始化marker
        const marker = new window.TMap.MultiMarker({
          id: 'marker-layer', // 图层id
          map,
          styles: { // 点标注的相关样式
            marker: new window.TMap.MarkerStyle({
              width: 25,
              height: 35,
              anchor: { x: 16, y: 32 }
            })
          },
          geometries: [{ // 点标注数据数组
            id: 'demo',
            styleId: 'marker',
            position: new window.TMap.LatLng(39.916527, 116.397128),
            properties: {
              title: 'marker'
            }
          }]
        })
        // 监听点击地图,切换位置
        map.on('click', event => {
          const position = event.latLng // 获取点击位置的经纬度
          console.log(`地图点击位置: (${position.lat}, ${position.lng})`)
          // 更新地图
          marker.setGeometries([{ // 点标注数据数组
            id: 'demo',
            styleId: 'marker',
            position: new window.TMap.LatLng(position.lat, position.lng),
            properties: {
              title: 'marker'
            }
          }])
        })
      } else {
        console.error('TMap 未定义,无法初始化地图')
      }
    },
  }
}
</script>

<style scoped>
#container{
  width:50%;
  height:800px;
  position: relative;
  top: 0px; /* 设置上边距为 50% */
  left: 50%; /* 设置左边距为 50% */
  transform: translate(-50%, 0);
}
</style>

如果对你有帮助,多谢一键三连!!!

相关推荐
2301_8152795210 分钟前
如何实现C++ Web 自动化测试实战:常用函数全解析与场景化应用指南
开发语言·前端·c++
代码不停10 分钟前
Spring Web MVC
前端·spring·mvc
倾颜6 小时前
从 textarea 到 AI 输入框:用 Tiptap 实现 / 命令、@ 引用和结构化请求
前端·langchain·next.js
kyriewen8 小时前
程序员连夜带团队跑路,省了23万:这AI太贵,真的用不起了
前端·javascript·openai
kyriewen8 小时前
你写的代码没有测试,就像出门不锁门——Jest + Testing Library 从入门到不慌
前端·单元测试·jest
yuzhiboyouye9 小时前
web前端英语面试
前端·面试·状态模式
canonical_entropy10 小时前
下一代低代码渲染框架 nop-chaos-flux 的设计原则
前端·低代码·前端框架
东方小月10 小时前
5分钟搞懂Harness Engineering(驾驭工程):从提示词到AI Agent的进化之路
前端·后端·架构
我叫黑大帅10 小时前
为什么需要 @types/react?解决“无法找到模块 react 的声明文件”报错
前端·javascript·面试
之歆11 小时前
DAY_21JavaScript 深度解析:数组(Array)与函数(Function)(一)
前端·javascript