如何在vuejs中将Leaflet与Mapbox GL集成

rqdpfwrv  于 2023-08-07  发布在  Vue.js
关注(0)|答案(3)|浏览(153)

我试图整合传单在Map框GL,所以我们可以使用Map框GLMap和绘制形状使用传单。
我已经尝试了这个代码,并希望添加到这个传单。

<template>
  <div id="mapContainer" class="basemap"></div>
</template>

<script>
import mapboxgl from "mapbox-gl";

export default {
  name: "BaseMap",
  data() {
    return {
      accessToken: MAPBOX_ACCESS_TOKEN,
    };
  },
  mounted() {
    mapboxgl.accessToken = this.accessToken;

    new mapboxgl.Map({
      container: "mapContainer",
      style: "mapbox://styles/mapbox/streets-v11",
      center: [103.811279, 1.345399],
      zoom: 12,
      maxBounds: [
        [103.6, 1.1704753],
        [104.1, 1.4754753],
      ],
    });
  },
};
</script>

字符串

wkftcu5l

wkftcu5l1#

<template>
  <div id="map"></div>
</template>

<script>
export default {
  name: 'Map',
  mounted() {
    // Initialize Leaflet map
    this.map = L.map('map').setView([YOUR_LATITUDE, YOUR_LONGITUDE], YOUR_ZOOM_LEVEL);

    // Add Mapbox GL layer to Leaflet map
    const mapboxAccessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
    L.mapboxGL({
      accessToken: mapboxAccessToken,
      style: 'mapbox://styles/mapbox/streets-v11', // You can use your desired Mapbox style here
    }).addTo(this.map);
  },
};
</script>

<style>
#map {
  width: 100%;
  height: 500px;
}
</style>

字符串

yqyhoc1h

yqyhoc1h2#

假设您必须在Vue.js中使用Leaflet和Mapbox GL,然后使用以下步骤
步骤1:在项目中安装Leaflet包

npm install mapbox-gl
npm i -D @vue-leaflet/vue-leaflet leaflet

步骤2:然后创建GoogleMap.vuecommponet文件

<script>
    import { LMap, LTileLayer } from "@vue-leaflet/vue-leaflet";
    import "mapbox-gl/dist/mapbox-gl.css";

    export default {
      components: {
        LMap,
        LTileLayer,
      },
      mounted() {
        this.initMap();
      },
      methods: {
        initMap() {
          const map = L.map(this.$refs.map).setView([YOUR_INITIAL_LATITUDE, YOUR_INITIAL_LONGITUDE], YOUR_INITIAL_ZOOM_LEVEL);

          L.tileLayer(
            "https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}",
            {
              maxZoom: 12,
              tileSize: 300,
              zoomOffset: -1,
              accessToken: 'here token code',
            }
          ).addTo(map);
        },
      },
    };
    </script>

    <style>
    .map {
      height: 100%;
    }
    </style>

    <template>
      <div id="map">
        <div ref="map" class="map"></div>
      </div>
    </template>


步骤3:然后将GoogleMap.vue组件导入到主组件中

brvekthn

brvekthn3#

您可以使用以下代码将Leaflet集成到MapboxGl中。

<template>
  <LMap id="map" :center="center" :zoom="zoom">
    <LTileLayer
      :options="layerOptions"
      :tile-layer-class="tileLayerClass" />
  </LMap>
</template>

<script>
import { LMap, LTileLayer } from 'vue2-leaflet'
import L from 'leaflet'
import mapboxgl from 'mapbox-gl'
import 'mapbox-gl-leaflet'
import 'mapbox-gl/dist/mapbox-gl.css'
import 'leaflet/dist/leaflet.css'
window.mapboxgl = mapboxgl // mapbox-gl-leaflet expects this to be global
export default {
  components: {
    LMap,
    LTileLayer
  },
  data () {
    return {
      center: [39.9523893, -75.1636291],
      zoom: 14,
      tileLayerClass: (url, options) => L.mapboxGL(options),
      layerOptions: {
        accessToken: 'no-token',
        style: 'https://raw.githubusercontent.com/osm2vectortiles/mapbox-gl-styles/master/styles/bright-v9-cdn.json'
      }
    }
  }
}
</script>

<style>
#map {
  height: 500px;
}
</style>

字符串

相关问题