vue3+mapbox

下载 

npm i mapbox-gl

直接上代码

<script lang="tsx">
import { defineComponent, onMounted, ref, inject } from "vue";
import type { Map, Style } from "mapbox-gl";
import mapbox from "mapbox-gl";

import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
// import MapboxLanguage from '@mapbox/mapbox-gl-language'; 
mapbox.accessToken = "YOUR_MAPBOX_ACCESS_TOKEN"
const gaode: Style = {
    version: 8,
    sources: {
        "raster-annotation": {
            type: "raster",
            tiles: ["http://webst02.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8"],
            tileSize: 256
        },
        // "source": {
        //     type: "geojson",
        //     data: "https://geo.datav.aliyun.com/areas_v3/bound/210000_full.json"
        // },
        // "image-source": {
        //     type: "image",
        //     url: 'https://img2.baidu.com/it/u=3880877693,3055552033&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1721322000&t=db28eb3e08018cc0a9092e9b7b3c2a1b',
        //     coordinates: [[121.38, 42.31], [121.38, 42.31], [121.38, 42.31], [121.38, 42.31],] // 图片在地图上的位置,使用经纬度
        // }
        // "mapsource": {
        //     type: "raster",
        //     tiles: ["http://webst04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=6&x={x}&y={y}&z={z}"],
        //     tileSize: 256
        // },

    },
    layers: [
        // {
        //     id: "source",
        //     type: "fill",
        //     source: "source",
        //     minzoom: 0,
        //     maxzoom: 17,
        //     filter: ['==', 'name', '沈阳市'],
        //     paint: {
        //         //填充颜色
        //         'fill-color': 'rgb(255, 0, 55)',
        //         //透明度设置
        //         'fill-opacity': 1,
        //         'fill-outline-color': 'rgb(0, 153, 255)',
        //         'fill-antialias': true
        //     }
        // },
        // {
        //     id: 'image-layer',
        //     type: 'raster',
        //     source: 'image-source',
        //     paint: {
        //         'raster-opacity': 0.5 // 设置图片的透明度
        //     }
        // },
        // {
        //     id: "mapsource",
        //     type: "raster",
        //     source: "mapsource",
        //     minzoom: 0,
        //     maxzoom: 22
        // },
        {
            id: "simple-annotation",
            type: "raster",
            source: "raster-annotation",
            minzoom: 0,
            maxzoom: 22
        }
    ]
};

export default defineComponent(() => {
    const mapRef = ref<HTMLElement | null>(null);
    onMounted(() => {
        const container = mapRef.value as HTMLElement;

        if (!container) {
            throw new Error("mapRef cant found.");
        }
        const map = new mapbox.Map({
            container,
            style: gaode,
            center: [121.38, 42.31],
            zoom: 5, //缩放等级
            pitch: 0, // 相对于地面3D视角的角度
            bearing: 3, // 东西南北 方向,正北方为 0
            projection: 'globe', // 为 3D 地球
            antialias: false, //抗锯齿,通过false关闭提升性能
            renderWorldCopies: false, //可理解为loop,在projection: 'globe'时无效
        });
        // ### 标签汉化
        // map.addControl(new MapboxLanguage({ defaultLanguage: 'zh-Hans' }));
        // ### 添加导航控制条
        map.addControl(new mapboxgl.NavigationControl(), 'top-left');
        map.on('style.load', () => {
            console.log('hfhfhfhfhfh')
            map._logoControl && map.removeControl(map._logoControl); //去除mapbox logo
            map.addSource('source', {
                type: 'geojson',
                data: "https://geo.datav.aliyun.com/areas_v3/bound/210000_full.json"
            })
            map.addLayer(
                {
                    id: "source",
                    type: "fill",
                    source: "source",
                    minzoom: 0,
                    maxzoom: 17,
                    filter: ['!=', 'name', '沈阳市'],
                    paint: {
                        //填充颜色
                        'fill-color': 'rgb(255, 0, 55)',
                        //透明度设置
                        'fill-opacity': 1,
                        'fill-outline-color': 'rgb(0, 153, 255)',
                        'fill-antialias': true
                    }

                })
        })
        console.log(map)

    });

    return () => <div ref={mapRef} class="map" />
});
</script>

<style lang="scss" scoped>
.map {
    width: 1000px;
    height: 700px;
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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 中轻松使用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值