Openlayer - vue中加载天地图(入门篇)

在vue中安装openlayers

npm i --save ol

这里说的vue是基于脚手架构建的。 新建个页面,也就是vue文件,配置好路由。接着 就是可以直接放入我的代码运行显示了。

vue利用openlayers加载天地图和高德地图
<template>
  <div class="wrapper">
    <div>天地图</div>
    <div class="map" id="olMap"></div>
  </div>
</template>
<script>
import "ol/ol.css";
import {
  Tile as TileLayer } from "ol/layer";
import XYZ from "ol/source/XYZ";
import {
  defaults as defaultControls } from "ol/control";
import Map from "ol/Map.js";
import View from "ol/View.js";
export default {
  data() {
   return {
      map: null,
      parser: null,
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
     const map = new Map({
       target: "olMap",
        view: new View({
 
          center: [0, 0], //中心点经纬度
          zoom: 4, //图层缩放大小
          projection: "EPSG:4326",
        }),
        controls: defaultControls({
          zoom: true,
          attribution: false,
          rotate: false,
        }),
      });
      this.map = map;
      // 添加地图
      let url = "http://t{0-7}.tianditu.com/DataServer?x={x}&y={y}&l={z}";
      url = `${
   url}&T=vec_c&tk=替代你的key`;
      const source = new XYZ({
     url: url,
        projection: "EPSG:4326",
      });
      const tdtLayer = new TileLayer({
        source: source,
      });
      this.map.addLayer(tdtLayer);
      // 添加注记
      url = "http://t{0-7}.tianditu.com/DataServer?x={x}&y={y}&l={z}";
      url = `${
   url}&T=cva_c&tk=替代你的key`;
      const sourceCVA = new XYZ({
        url: url,
        projection: "EPSG:4326",
      });
      const tdtcvaLayer = new TileLayer({
        source: sourceCVA,
      });
      this.map.addLayer(tdtcvaLayer);
    },
  },
};
</script>
<style scoped>
.map {
  width: 100%;
  height: 100vh;
}
</style>

天地图就可以显示出来了。
在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,而OpenLayers是一种开源JavaScript库,用于创建交互式地图应用程序。要在Vue项目加载地图,我们需要在Vue组件引入OpenLayers库,并使用相关的JavaScript代码来实现。 首先,在Vue项目安装OpenLayers库。我们可以使用npm或yarn来安装OpenLayers。在项目的根目录打开终端,并运行以下命令: ``` npm install ol ``` 或 ``` yarn add ol ``` 安装完成后,我们可以在Vue组件引入OpenLayers库。在需要加载地图Vue组件的script标签,添加以下代码: ```javascript import ol from 'ol'; export default { data() { return { map: null }; }, mounted() { this.initializeMap(); }, methods: { initializeMap() { this.map = new ol.Map({ target: 'map-container', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: [0, 0], zoom: 2 }) }); } } }; ``` 上述代码,我们首先引入OpenLayers库并创建了一个名为`map`的data属性。在`mounted`生命周期钩子,我们调用了`initializeMap`方法来初始化地图。在`initializeMap`方法,我们创建一个新的`ol.Map`实例,并将其绑定到id为`map-container`的HTML元素上。同时,我们还添加了一个基本的Tile图层,使用OpenStreetMap的数据源,并设置了初始的视图心和缩放级别。 最后,在Vue组件的模板,我们需要添加一个id为`map-container`的HTML元素,作为地图的容器,例如: ```html <template> <div id="map-container"></div> </template> ``` 运行项目,你就能够在浏览器看到OpenLayers加载地图了。通过使用OpenLayers的各种功能和选项,我们可以进一步自定义和扩展地图应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值