openlayers显示mapbox源

下面的代码acesstoken修改为自己的acesstoken后即可全屏展示地图:

使用前确保拥有openlayer依赖包的ol库npm install ol

在package.json中dependencies中加入:

"ol": "^6.15.1",后yarn install也可
<template>
  <div>
    <div ref="map" class="map"></div>
  </div>
</template>

<script>
import 'ol/ol.css';
import { Map, View } from 'ol';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';

export default {
  name: 'MapboxMap',
  data() {
    return {
      map: null,
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
      const mapboxAccessToken = '你的mapbox acesstoken';

      // Create a Mapbox layer
      const mapboxLayer = new TileLayer({
        source: new XYZ({
          url: `https://api.mapbox.com/styles/v1/mapbox/light-v10/tiles/{z}/{x}/{y}?access_token=${mapboxAccessToken}`,
        }),
      });

      // Create a map
      this.map = new Map({
        target: this.$refs.map,
        layers: [mapboxLayer],
        view: new View({
          center: [0, 0],
          zoom: 2,
        }),
      });
    },
  },
};
</script>

<style scoped>
.map {
  height: 100vh; /* 100% of the viewport height */
  width: 100vw; /* 100% of the viewport width */
}
</style>

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值