vue + mapbox 通过geojson数据加载面

1.首先通过npm 安装mapbox-gl

npm install mapbox-gl

2.页面代码

<template>
  <div>
    <div id="map"></div>
  </div>
</template>
<script>
  import mapboxgl from 'mapbox-gl'
  // import * as THREE from 'three'
  // import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
  import geojson from './chongqing1.json'
  export default {
    data() {
      return {
        title: 'Mapgis',
        hs: 'aad',
        positionLocation: [106.21176144800006, 29.59098730870005],
        //positionLocation: [106.697157, 29.501613],
        zoom: 16,
        pitch: 60, //地图的角度,不写默认是0,取值是0-60度,一般在3D中使用
        bearing: 10,
        antialias: false, //抗锯齿,通过false关闭提升性能
        maxPitch: 60,
        container: 'map',
        lonlats: [],
        baseUrl: 'api/service-count',
        markerRe: [], //标点
        x_PI: (3.14159265358979324 * 3000.0) / 180.0,
        PI: 3.1415926535897932384626,
        a: 6378245.0,
        ee: 0.00669342162296594323,
        layer: '',
      }
    },
    mounted() {
      this.init()
    },
    methods: {
      init: function () {
        let that = this
        mapboxgl.accessToken =
          'token'
        let map = new mapboxgl.Map({
          container: 'map',
          style: 'mapbox://styles/abcd19886/ckgbsqb68214i1apr7qifwpvm',
          maxPitch: this.maxPitch,
          center: this.positionLocation,
          zoom: this.zoom,
          pitch: this.pitch,
          bearing: this.bearing,
        })
        //console.log(typeof geojson.geometries)
        map.on('style.load', function () {
          that.chuliData()
        })
        window.mapBox = map
      },
      // 处理数据
      chuliData() {
        let featureArray = []
        for (let i in geojson.geometries) {
          let feature = {
            type: 'Feature',
            geometry: {
              type: 'Polygon',
              coordinates: geojson.geometries[i].coordinates,
            },
          }
          featureArray.push(feature)
        }
        mapBox.addLayer({
          id: 'maine',
          type: 'fill',
          source: {
            type: 'geojson',
            data: {
              type: 'FeatureCollection',
              features: featureArray,
            },
          },
          layout: {},
          paint: {
            'fill-color': '#088',
            'fill-opacity': 0.8,
          },
        })
      },
    },
  }
</script>
<style scoped>
  /* mapbox css*/
  @import url('https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.2/mapbox-gl.css');
  #map {
    position: absolute;
    top: 40px;
    bottom: 0;
    width: 100%;
    margin-left: -24px;
  }
</style>

3.展现效果

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 和 Mapbox 是两个非常流行的工具,用于构建用户界和提供强大的地图功能。Vue 是一个轻量级的前端框架,以其组件化开发和易于上手的特点受到开发者喜爱。Mapbox 则是一个提供了丰富的地图服务、地图样式和交互功能的平台。 当你将 VueMapbox 结合使用时,可以实现以下几个关键点: 1. 安装依赖:首先,你需要在 Vue 项目中安装 `@vuegis/vue-mapbox-gl` 或者直接使用 Mapbox GL JS(原生库),这是官方推荐的 Vue 组件化解决方案。 2. 创建地图组件:创建一个 Vue 组件,例如 `Map.vue`,引入 Mapbox GL JS,并设置初始化地图的方法,包括设置中心位置、比例尺、地图样式等。 ```html <template> <div ref="map"></div> </template> <script> import { mapboxgl, Map } from '@vuegis/vue-mapbox-gl' export default { components: { MapboxGL: mapboxgl.Map, }, mounted() { this.createMap() }, methods: { createMap() { this.$refs.map .el .style = 'width: 100%; height: 400px;'; // 设置样式 const map = new Map(this.$refs.map, { center: [51.505, -0.09], // 地图中心点 zoom: 13, // 初始缩放级别 style: 'mapbox://styles/mapbox/streets-v11', // 使用预设样式 }); }, }, } </script> ``` 3. 动态数据绑定:Vue 的响应式系统使得地图可以根据数据变化动态调整,比如根据用户的输入改变地图视图或添加标记。 4. 图层和交互:Mapbox 提供了各种图层(如矢量图层、影像图层等)和交互元素(如点击事件、鼠标悬停事件),可以在 Vue 中轻松使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值