一、Mapbox 在 vue 中的使用
安装 @mapbox/mapbox-gl-geocoder
npm install --save @mapbox/mapbox-gl-geocoder
安装 @mapbox/mapbox-gl-language
npm install --save @mapbox/mapbox-gl-language
安装 mapbox-gl
npm install --save mapbox-gl
图片展示
二、代码
<template>
<div class="map-box-index">
<div
id="indexMap"
:style="{ height: height + 'px', width: width + 'px' }"
></div>
</div>
</template>
<script>
import "mapbox-gl/dist/mapbox-gl.css";
import MapboxLanguage from "@mapbox/mapbox-gl-language";
import MapboxGeocoder from "@mapbox/mapbox-gl-geocoder";
import "@mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.css";
const mapboxgl = require("mapbox-gl");
export default {
name: "indexMap",
props: {
height: {
type: Number,
default: 310,
},
width: {
type: Number,
default: 310,
},
},
data() {
return {
map: null,
center: [116.18425567303788, 39.90661201076973],
mapNewData: {},
};
},
mounted() {
this.initMap();
},
watch: {
height: {
immediate: true,
deep: true,
handler: function(newVal, oldVal) {
if (newVal !== null) {
this.$nextTick(() => {
this.initMap();
});
}
},
},
},
methods: {
initMap() {
mapboxgl.accessToken =
"pk.eyJ1IjoibHVrYXNtYXJ0aW5lbGxpIiwiYSI6ImNpem85dmhwazAyajIyd284dGxhN2VxYnYifQ.HQCmyhEXZUTz3S98FMrVAQ";
this.map = new mapboxgl.Map({
container: "indexMap",
style: {
version: 8,
sources: {
"tian-satellite": {
type: "raster",
tiles: [
"http://t0.tianditu.gov.cn/img_w/wmts?tk=1883a2da124fe27b3c281f9d65356e82&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=tiles",
],
tileSize: 256,
minzoom: 0,
maxzoom: 18,
},
},
layers: [
{
id: "tian-satellite",
type: "raster",
source: "tian-satellite",
layout: {
visibility: "none",
},
minzoom: 0,
maxzoom: 22,
},
],
},
center: [116.18425567303788, 39.90661201076973],
zoom: 5,
});
this.map.addControl(new mapboxgl.FullscreenControl(), "top-left");
this.map.on("load", (e) => {
this.map.setFog({});
this.map.setLayoutProperty("tian-satellite", "visibility", "visible");
this.map.addSource("myCity", {
type: "geojson",
data: "https://geo.datav.aliyun.com/areas_v3/bound/110000_full.json",
});
this.map.addControl(new mapboxgl.NavigationControl(), "top-left");
this.map.addControl(
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
placeholder: "请输入搜索地址",
mapboxgl: mapboxgl,
})
);
this.map.addControl(new mapboxgl.ScaleControl({}), "bottom-right");
this.map.addLayer({
id: "beijing",
type: "fill",
source: "myCity",
layout: {},
paint: {
"fill-color": "#f08",
"fill-opacity": 0.3,
},
});
this.flyTo();
});
},
flyTo() {
this.map.flyTo({
center: [116.4178, 40.222279390523586],
zoom: 5,
});
},
},
};
</script>
<style lang="less" scoped>
.map-box-index {
width: 100%;
height: 100%;
#indexMap {
width: 100%;
height: 100%;
/deep/ .mapboxgl-canvas {
}
/deep/ .mapboxgl-control-container {
.mapboxgl-ctrl-bottom-left {
.mapboxgl-ctrl {
.mapboxgl-ctrl-logo {
display: none;
}
}
}
.mapboxgl-ctrl-bottom-right {
.mapboxgl-ctrl-attrib-inner {
display: none;
}
.mapboxgl-compact {
display: none;
}
}
}
}
}
</style>