Vue + MapBox快速搭建

一、说明:

1.mapbox-gl自2.0版本开始不再开源,需要用户在官网申请key使用。

2.maplibre GL JS是一个开源库,它起源于 mapbox-gl-js 的开源分支。该库的初始版本(1.x)旨在替代Mapbox的OSS版本。简单来说maplibre是mapbox-gl1.0版本的替代,接替了开源的重任,但是之后就各自发展了。

二、新版MapBox搭建

1.使用npm引入依赖

javascript 复制代码
npm i mapbox-gl

javascript 复制代码
<script src='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css' rel='stylesheet' />

2.导入并初始化地图

javascript 复制代码
import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
javascript 复制代码
<div id='map' style='width: 400px; height: 300px;'></div>
<script>
mapboxgl.accessToken = '你的tk';
const map = new mapboxgl.Map({
    container: 'map', // container ID
    style: 'mapbox://styles/mapbox/streets-v12', // style URL
    center: [-74.5, 40], // starting position [lng, lat]
    zoom: 9, // starting zoom
});
</script>

三、MapLibre搭建

1.使用npm引入依赖

javascript 复制代码
npm i maplibre-gl

javascript 复制代码
<script src='https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.js'></script>
<link href='https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.css' rel='stylesheet' />

2.导入并初始化地图

javascript 复制代码
import maplibregl from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
javascript 复制代码
<div id='map' style='width: 400px; height: 300px;'></div>
<script>
var map = new maplibregl.Map({
  container: 'map',
  style: 'https://demotiles.maplibre.org/style.json', // stylesheet location
  center: [-74.5, 40], // starting position [lng, lat]
  zoom: 9 // starting zoom
});
</script>
相关推荐
Jerry5 分钟前
Compose 从 View 系统迁移
前端
IT码农-爱吃辣条20 分钟前
Three.js 初级教程大全
开发语言·javascript·three.js
GIS之路25 分钟前
2025年 两院院士 增选有效候选人名单公布
前端
四岁半儿27 分钟前
vue,H5车牌弹框定制键盘包括新能源车牌
前端·vue.js
烛阴40 分钟前
告别繁琐的类型注解:TypeScript 类型推断完全指南
前端·javascript·typescript
gnip1 小时前
工程项目中.env 文件原理
前端·javascript
JefferyXZF1 小时前
Next.js Server Actions 详解: 无缝衔接前后端的革命性技术(八)
前端·全栈·next.js
JohnYan2 小时前
工作笔记 - CentOS7环境运行Bun应用
javascript·后端·容器
芜青2 小时前
HTML+CSS:浮动详解
前端·css·html
SchuylerEX3 小时前
第六章 JavaScript 互操(2).NET调用JS
前端·c#·.net·blazor·ui框架