如何在Vue.js中将Leaflet与Mapbox GL集成

huangapple go评论79阅读模式
英文:

How to Integrate Leaflet with Mapbox GL In vuejs

问题

我正在尝试在Mapbox GL中集成Leaflet,以便我们可以使用Mapbox GL地图并使用Leaflet绘制形状。

我已经尝试了这段代码,并希望将Leaflet添加到其中。

<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>
英文:

I am trying to integrate Leaflet in mapbox GL so we can use mapbox GL map and draw shapes using leaflet.

I have tried this code and want to add leaflet into this.

&lt;template&gt;
  &lt;div id=&quot;mapContainer&quot; class=&quot;basemap&quot;&gt;&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import mapboxgl from &quot;mapbox-gl&quot;;

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

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

答案1

得分: 2

<!-- 开始代码片段:JavaScript 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
<template>
  <div id="map"></div>
</template>

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

    // 将 Mapbox GL 图层添加到 Leaflet 地图
    const mapboxAccessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
    L.mapboxGL({
      accessToken: mapboxAccessToken,
      style: 'mapbox://styles/mapbox/streets-v11', // 您可以在此处使用所需的 Mapbox 样式
    }).addTo(this.map);
  },
};
</script>

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

<!-- 结束代码片段 -->
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;template&gt;
  &lt;div id=&quot;map&quot;&gt;&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  name: &#39;Map&#39;,
  mounted() {
    // Initialize Leaflet map
    this.map = L.map(&#39;map&#39;).setView([YOUR_LATITUDE, YOUR_LONGITUDE], YOUR_ZOOM_LEVEL);

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

&lt;style&gt;
#map {
  width: 100%;
  height: 500px;
}
&lt;/style&gt;

<!-- end snippet -->

答案2

得分: 2

Step 1: 在项目中安装 Leaflet 包

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

Step 2: 创建 GoogleMap.vue 组件文件

<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: '这里放置你的令牌代码',
        }
      ).addTo(map);
    },
  },
};
</script>

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

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

Step 3: 在你的主要组件中导入 GoogleMap.vue 组件

英文:

Suppose you have to used Leaflet with Mapbox GL in Vue.js Then used following step

Step 1: Install the Leaflet packages in your project

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->
Step 2: Then Create the GoogleMap.vue commponet file

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;script&gt;
    import { LMap, LTileLayer } from &quot;@vue-leaflet/vue-leaflet&quot;;
    import &quot;mapbox-gl/dist/mapbox-gl.css&quot;;

    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(
            &quot;https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}&quot;,
            {
              maxZoom: 12,
              tileSize: 300,
              zoomOffset: -1,
              accessToken: &#39;here token code&#39;,
            }
          ).addTo(map);
        },
      },
    };
    &lt;/script&gt;

    &lt;style&gt;
    .map {
      height: 100%;
    }
    &lt;/style&gt;

    &lt;template&gt;
      &lt;div id=&quot;map&quot;&gt;
        &lt;div ref=&quot;map&quot; class=&quot;map&quot;&gt;&lt;/div&gt;
      &lt;/div&gt;
    &lt;/template&gt;

<!-- end snippet -->

Step 3: Then Import the GoogleMap.vue component in Your main component

答案3

得分: 1

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

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->

<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>

<!-- 结束代码片段 -->

这是将Leaflet集成到MapboxGl中的代码示例。

英文:

You can use following code to integrate Leaflet into MapboxGl.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;template&gt;
  &lt;LMap id=&quot;map&quot; :center=&quot;center&quot; :zoom=&quot;zoom&quot;&gt;
    &lt;LTileLayer
      :options=&quot;layerOptions&quot;
      :tile-layer-class=&quot;tileLayerClass&quot; /&gt;
  &lt;/LMap&gt;
&lt;/template&gt;

&lt;script&gt;
import { LMap, LTileLayer } from &#39;vue2-leaflet&#39;
import L from &#39;leaflet&#39;
import mapboxgl from &#39;mapbox-gl&#39;
import &#39;mapbox-gl-leaflet&#39;
import &#39;mapbox-gl/dist/mapbox-gl.css&#39;
import &#39;leaflet/dist/leaflet.css&#39;
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) =&gt; L.mapboxGL(options),
      layerOptions: {
        accessToken: &#39;no-token&#39;,
        style: &#39;https://raw.githubusercontent.com/osm2vectortiles/mapbox-gl-styles/master/styles/bright-v9-cdn.json&#39;
      }
    }
  }
}
&lt;/script&gt;

&lt;style&gt;
#map {
  height: 500px;
}
&lt;/style&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月20日 13:15:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定