<!DOCTYPE html>
<html><head><meta charset='utf-8' /><title>添加 GeoJSON 线</title><meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /><script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.js'></script><link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.css' rel='stylesheet' /><style>body {margin: 0;padding: 0;}#map {position: absolute;top: 0;bottom: 0;width: 100%;}</style>
</head><body><div id='map'></div><script>mapboxgl.accessToken ='pk.eyJ1IjoibHh0aWFudGlhbiIsImEiOiJjaXd2ZjlkYnQwMTZvMnRtYWZnM2lpbHFvIn0.ef3rFZTr9psmLWahrqap2A';var map = new mapboxgl.Map({container: 'map',style: 'mapbox://styles/mapbox/streets-v11',center: [-122.486052, 37.830348],zoom: 15});let coordinates = [],layerActive = false;map.on('click', (e) => addLayer(e))map.on('mousemove', (e) => updateLayer(e))function addLayer(e) {if (layerActive) {removeLayer();layerActive = !layerActive;return;}const { lat, lng } = e.lngLat;coordinates[0] = [lng, lat];map.addSource('sourceId', {type: 'geojson',data: {"type": "Feature","properties": {},"geometry": {"type": "LineString","coordinates": coordinates}}});map.addLayer({"id": "route","type": "line","source": "sourceId","layout": {"line-join": "round","line-cap": "round"},"paint": {"line-color": "#888","line-width": 8,"line-dasharray": [2, 4]}});layerActive = !layerActive;}function updateLayer(e) {const {lat,lng} = e.lngLat;coordinates[1] = [lng, lat];const datas = {"type": "Feature","properties": {},"geometry": {"type": "LineString","coordinates": coordinates}}if (map.getSource('sourceId')) {map.getSource('sourceId').setData(datas);}}function removeLayer() {if (map.getLayer('route')) {map.removeLayer('route');}if (map.getSource('sourceId')) {map.removeSource('sourceId');}}</script></body></html>
详细解决方案
mapbox 绘制虚线
热度:77 发布时间:2023-10-17 07:02:04.0
相关解决方案
- mapbox 基于turf 绘制扇形
- mapbox 绘制虚线
- mapbox 3d建筑物交互
- mapbox 添加geojson图层实现属性查询、高亮、地图手势变化等功能
- mapbox 添加geoserver发布的wms服务及wms服务属性查询
- mapbox 添加argis rest 动态地图服务
- mapbox 笔记
- mapbox 点、线、面绘制工具添加
- mapbox 地图交互
- mapbox 聚合图
- mapbox studio 地图制作完如何引入应用
- mapbox-gl-native qt编译问题
- mapbox gl结合turf.js完成绘图工具画线等距平分
- mapbox gl 加载gltf模型
- mapbox-gl知识点整理
- MapBox GL JS动态空间数据渲染策略
- Mapbox GL JS 表达式概述
- mapbox GL源码编译运行实操记录
- Mapbox 加载自定义 WMS 数据源
- Mapbox、GeoServer离线部署矢量地图
- dyld: Library not loaded - iOS 13.3.1 and embedded Mapbox.framework
- MapBox-Android的layer重置位置问题
- MapBox MVT数据规范说明
- pgsql+postgis 实时返回mvt(Mapbox Vector Tiles) 切片。
- mapbox-gl中精灵图的使用