vue3+vite 中使用百度地图【两种方式】

方式一:直接使用百度地图的ak

提前准备:

  • 创建一个vite项目
  • 申请好的百度地图ak值

百度地图使用:

  1. 在创建好的vite项目的入口文件index.html中引入百度地图ak
    在这里插入图片描述

附代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <link rel="icon" type="image/svg+xml" href="/vite.svg" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Vite + Vue</title>
  <script src="https://api.map.baidu.com/api?v=3.0&ak=IszZX5*****************“></script>
</head>

<body>
  <div id="app"></div>
  <script type="module" src="/src/main.js"></script>
</body>

</html>
  1. 去创建自己的地图
  • 在\src\echarts\MyMap.vue创建自己的地图
<template>
  <div class="map" ref="baiduRef"></div>
</template>
<script setup>
import { ref, onMounted } from "vue";

const baiduRef = ref();
const map = ref();
const point = ref();

function initMap(lng = 116.405725, lat = 39.935362) {
  map.value = new BMap.Map(baiduRef.value);
  point.value = new BMap.Point(lng, lat);

  map.value.centerAndZoom(point.value, 15);
  map.value.enableScrollWheelZoom(true); //滚轮缩放
  map.value.setMapStyleV2({
  //可以自己去百度地图中调整样式(方法在在一条)
    styleId: "9a7c760437*********************",
  });
}
onMounted(() => {
  initMap();
});
</script>
<style  scoped>
.map {
  width: 100%;
  height: 400px;
}
</style>

  1. 在百度地图开放平台中申请样式id(不是关键步骤,可忽略)
    在这里插入图片描述
  • 进入地图选项,下滑页面,可以看到查看详情
    在这里插入图片描述

  • 在详情页有新建选项,去创建自己的样式,并进行发布
    在这里插入图片描述

  • 点击发布后就能够获取到样式的id,还可以进行修改等操作

  • 在这里插入图片描述

  1. 在App组件中进行调用
<template>
  <BaiduMap  />
</template>
<script setup>
import BaiduMap from './components/baiduMap.vue'
</script>

方式二:使用vue-baidu-map-3x插件

提前准备

使用方法

  1. 在项目中安装vue-baidu-map-3x
npm i vue-baidu-map-3x
  1. 在/src/main.js中引入
import { createApp } from 'vue'
import BaiduMap from 'vue-baidu-map-3x'
import App from './App.vue'
const app = createApp(App);
app.use(BaiduMap, {
    ak: "IszZX5ST6*********************"  //使用自己的ak
})
app.mount('#app')

  1. 在 \src\echarts\MyMap.vue中使用(样式功能等可参照 vue-baidu-map-3x官网
<template>
  <div>
    <baidu-map
      class="map"
      :center="{ lng: 116.404, lat: 39.915 }"
      :zoom="14"
      @mousemove="syncPolyline"
      @click="paintPolyline"
      @rightclick="newPolyline"
    >
      <bm-control>
        <button @click.stop="toggle">
          {{ polyline.editing ? "停止绘制" : "开始绘制" }}
        </button>
      </bm-control>
      <bm-polyline
        :path="path"
        v-for="path of polyline.paths"
        :key="path"
      ></bm-polyline>
    </baidu-map>
  </div>
</template>

<script setup>
import { ref, toRef } from "vue";

const polyline = ref({
  editing: false,
  paths: [],
});

const toggle = (name) => {
  polyline.value.editing = !polyline.value.editing;
};

const syncPolyline = (e) => {
  if (!polyline.value.editing) {
    return;
  }

  if (!polyline.value.paths.length) {
    return;
  }
  const path = polyline.value.paths[polyline.value.paths.length - 1];
  if (!path.length) {
    return;
  }
  if (path.length === 1) {
    polyline.value.paths[polyline.value.paths.length - 1].push(e.point);
  }
  polyline.value.paths[polyline.value.paths.length - 1][path.length - 1] =
    e.point;
};

const newPolyline = (e) => {
  if (!polyline.value.editing) {
    return;
  }
  if (!polyline.value.paths.length) {
    polyline.value.paths.push([]);
  }
  const path = polyline.value.paths[polyline.value.paths.length - 1];
  path.pop();
  if (path.length) {
    polyline.value.paths.push([]);
  }
};

const paintPolyline = (e) => {
  if (!polyline.value.editing) {
    return;
  }
  !polyline.value.paths.length && polyline.value.paths.push([]);
  polyline.value.paths[polyline.value.paths.length - 1].push(e.point);
};
</script>

<style>
.map {
  width: 400px;
  height: 100%;
}
</style>
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue3和Vite项目使用Electron,需要进行以下步骤: 1. 安装Electron 使用npm或者yarn安装Electron: ``` npm install --save-dev electron ``` 2. 创建Electron主进程 在项目根目录下创建一个main.js文件,作为Electron的主进程。在该文件,需要引入Electron,并创建一个BrowserWindow实例。 main.js的示例代码: ``` const { app, BrowserWindow } = require('electron') function createWindow() { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }) // 加载Vite生成的页面 win.loadURL('http://localhost:3000') } app.whenReady().then(() => { createWindow() app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } }) }) app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() } }) ``` 3. 修改Vite配置文件 在Vite项目的根目录下,需要创建一个vite.config.js文件,并在其添加以下内容: ``` const { defineConfig } = require('vite') const { resolve } = require('path') const { createElectronRendererConfig } = require('@forge/plugin-vue') module.exports = defineConfig({ build: { target: 'esnext', outDir: 'dist/renderer', emptyOutDir: true, assetsDir: 'assets', rollupOptions: { input: { main: resolve(__dirname, 'index.html'), } } }, plugins: [ createElectronRendererConfig({ // 更改Electron的主进程的文件路径 mainProcessFile: resolve(__dirname, 'main.js') }) ] }) ``` 4. 运行项目 在终端运行以下命令,启动Vite服务: ``` npm run dev ``` 然后,在另一个终端,运行以下命令,启动Electron: ``` npm run electron ``` 现在,你就可以在Electron看到你的Vue3+Vite应用程序了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值