Vue+Openlayers加载OSM、加载天地图

文章目录

  • [1. 介绍](#1. 介绍)
  • [2. 加载底图](#2. 加载底图)
    • [2.1 加载默认OSM地图](#2.1 加载默认OSM地图)
    • [2.2 加载天地图](#2.2 加载天地图)

1. 介绍

Openlayers官网:https://openlayers.org/

安裝依赖:npm i ol

2. 加载底图

参考博客:

vue+openlayers环境配置:https://blog.csdn.net/cuclife/article/details/126195754

加载天地图:https://blog.csdn.net/weixin_43390116/article/details/122326847

2.1 加载默认OSM地图

主要的HomeView.vue内容:

html 复制代码
<template>
  <div class="home">
    <div id="map"></div>
  </div>
</template>

<script>
import "ol/ol.css";
import { Map, View } from "ol";
import Tile from "ol/layer/Tile";
import OSM from "ol/source/OSM";

export default {
  name: "HomeView",
  components: {},
  data() {
    return {
      map: null,
    };
  },
  methods: {
    initMap() {
      this.map = new Map({
        target: "map",
        layers: [
          new Tile({
            source: new OSM({
              wrapX: true,
            }),
          }),
        ],
        view: new View({
          projection: "EPSG:4326",
          center: [114.064839, 22.548857],
          zoom: 8,
        }),
      });
    },
  },
  mounted() {
    this.initMap();
  },
};
</script>
<style scoped>
.home,
#map {
  height: 100%;
  width: 100%;
}
</style>

需确保网络通畅可以访问OSM。

效果图:

2.2 加载天地图

HomeView.vue主要内容(需要更换token):

javascript 复制代码
<template>
  <div class="home">
    <div id="map"></div>
  </div>
</template>

<script>
import "ol/ol.css";
import { Map, View } from "ol";
import { defaults as Defaults } from "ol/control.js";
import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
export default {
  name: "HomeView",
  components: {},
  data() {
    return {
      map: null,
    };
  },
  methods: {
    initMap() {
      let token = "xxxx换成自己申请的天地图tokenxxxx";
      let url_vec = `http://t4.tianditu.com/DataServer?T=vec_w&tk=${token}&x={x}&y={y}&l={z}`;
      let url_cav = `http://t4.tianditu.com/DataServer?T=cva_w&tk=${token}&x={x}&y={y}&l={z}`;
      var source = new XYZ({
        url: url_vec,
      });
      var tileLayer = new TileLayer({
        title: "矢量图层",
        source,
      });
      var sourceCav = new XYZ({
        url: url_cav,
      });
      var tileLayerCav = new TileLayer({
        title: "标注图层",
        source: sourceCav,
      });

      this.map = new Map({
        target: "map",
        layers: [tileLayer, tileLayerCav],
        view: new View({
          projection: "EPSG:4326",
          center: [118.91, 32.1],
          zoom: 14,
          maxZoom: 18,
          minZoom: 1,
        }),
        //不显示地图自带控件(左上角的加号、减号)
        controls: new Defaults({
          zoom: false,
          rotate: false,
        }),
      });
    },
  },
  mounted() {
    this.initMap();
  },
};
</script>
<style scoped>
.home,
#map {
  height: 100%;
  width: 100%;
}
</style>

效果图:

相关推荐
还是大剑师兰特7 小时前
Vue3 中的 defineExpose 完全指南
前端·javascript·vue.js
xkxnq9 小时前
第六阶段:Vue生态高级整合与优化(第93天)Element Plus进阶:自定义主题(变量覆盖)+ 全局配置与组件按需加载优化
前端·javascript·vue.js
A黄俊辉A10 小时前
vue css中 :global的使用
前端·javascript·vue.js
终端鹿13 小时前
Vue2 迁移 Vue3 避坑指南
前端·javascript·vue.js
SuperEugene14 小时前
TypeScript+Vue 实战:告别 any 滥用,统一接口 / Props / 表单类型,实现类型安全|编码语法规范篇
开发语言·前端·javascript·vue.js·安全·typescript
还是大剑师兰特15 小时前
Vue3 报错:computed value is readonly 解决方案
前端·vue.js
北寻北爱16 小时前
前端加密解密- base64、md5、sha256、AES
前端·vue.js
spencer_tseng17 小时前
Vue node_modules
javascript·vue.js
SuperEugene17 小时前
前端 console 日志规范实战:高效调试 / 垃圾 log 清理与线上安全避坑|编码语法规范篇
开发语言·前端·javascript·vue.js·安全
发现一只大呆瓜18 小时前
Vue - @ 事件指南:原生 / 内置 / 自定义事件全解析
前端·vue.js·面试