最近正在搞Mapbox-GL地图的一系列东西,按照公司的需求,要做成离线地图(点击这里),然后要在地图的基础上进行增加图标标记,线条连接、弹窗等等需求。ok废话不多,往下看。

如果还没有安装mapbox-gl的小伙伴请先看一下上篇的离线地图的文章,这里不再重复写安装步骤了,直接上代码:

一、在地图上增加图标

<template><div><div class="mapBOX" id="map" ref="basicMapbox"></div></div>
</template><script>
let map;import mapboxgl from 'mapbox-gl';export default {
methods: {this.initMap(){let mapStyle = require('../../../static/style.json');// sytle.json看上一篇文章,或者使用官方的网址也可以:let mapStyle = "mapbox://styles/mapbox/streets-v11"map = new mapboxgl.Map({container: this.$refs.basicMapbox,style: mapStyle,// 中心点:北京市center: [116.469000,40.251706],zoom: 10,minZoom: 3,maxZoom: 15,fadeDuration: 100,antialias: true,});this.map.on('load', () => {// 图片需要是网络地址,前端本地地址行不通map.loadImage('http://ip/file/img/icon.png', (error, image) => {if (error) throw error;map.addImage('iconImage', image);// 图标位置坐标let features = [{"type": "Feature","geometry": {"type": "Point","coordinates": [116.469000,40.251706]}}, {"type": "Feature","geometry": {"type": "Point","coordinates": [116.469000,40.351706]}}]map.addSource('iconImage', {type: 'geojson',data: {type: 'FeatureCollection',features}});// 增加图片map.addLayer({id: "iconImage",type: "symbol",source: 'iconImage', // 对应addSource第一个参数名字layout: {"icon-image": "iconImage", // 对应addImage()第一个参数名字"icon-size": 0.1,//图标的大小},})}
},
mounted() {this.initMap()
}
}
</script>

官网也有示例:
Add an icon to the map | Mapbox GL JS | MapboxAdd an icon to the map from an external URL and use it in a symbol layer.https://docs.mapbox.com/mapbox-gl-js/example/add-image/

二、在地图上画线条

<template><div><div class="mapBOX" id="map" ref="basicMapbox"></div></div>
</template><script>
let map;import mapboxgl from 'mapbox-gl';export default {
methods: {this.initMap(){let mapStyle = require('../../../static/style.json');// sytle.json看上一篇文章,或者使用官方的网址也可以:let mapStyle = "mapbox://styles/mapbox/streets-v11"map = new mapboxgl.Map({container: this.$refs.basicMapbox,style: mapStyle,// 中心点:北京市center: [116.469000,40.251706],zoom: 10,minZoom: 3,maxZoom: 15,fadeDuration: 100,antialias: true,});map.on('load', () => {map.addSource('lineSource', {type: 'geojson',data: {type: "FeatureCollection",features: [{type: "Feature",geometry: {type: 'LineString',coordinates: [[116.469000,40.251706],[116.469000,40.351706],[117.469000,40.351706]],}},{type: 'Feature',geometry: {type: 'Point',coordinates: [116.469000,40.251706]}},{type: 'Feature',geometry: {type: 'Point',coordinates: [116.469000,40.351706]}},{type: 'Feature',geometry: {type: 'Point',coordinates: [117.469000,40.351706]}},]}});// 增加线条map.addLayer({id: "lines",type: "line",source: "lineSource",layout: {line-cap: "round",line-join: "round",},paint: {"line-width": 3, // 线条宽度"line-opacity": 1, // 线条透明度"line-color": "#000000", // 线条颜色}});// 显示线路节点map.addLayer({id: "lineSources",type: "circle",source: "lineSource",paint: {"circle-radius": 6, // 圆角值"circle-color": "#000000" // 节点颜色 },})}
},
mounted() {this.initMap()
}
}
</script>

三、地图点击标记点弹窗

这里参考复制了部分代码:

Mapbox 添加弹窗 点击标记点出现弹窗 vue_Windyluna的博客-CSDN博客_mapbox添加弹框方法一:字符串拼接弹框内容:(弹框内容样式少的情况下)效果图:核心代码:var popup = new mapboxgl.Popup({ offset: 25 }).setHTML(`<div class="makerTop"><h2 class="markerHear" > 综合办一处 </h2></div>' '<div class="markerBody" ><p>设https://blog.csdn.net/Windyluna/article/details/120658613官网API:
http://www.mapbox.cn/mapbox-gl-js/api/#popup#settexthttp://www.mapbox.cn/mapbox-gl-js/api/#popup%23settext

<template><div><div class="mapBOX" id="map" ref="basicMapbox"></div></div>
</template><script>
let map;import mapboxgl from 'mapbox-gl';
import InfoPopup from "./components/info-popup.vue";
export default {
components: { InfoPopup },methods: {this.initMap(){let mapStyle = require('../../../static/style.json');// sytle.json看上一篇文章,或者使用官方的网址也可以:let mapStyle = "mapbox://styles/mapbox/streets-v11"map = new mapboxgl.Map({container: this.$refs.basicMapbox,style: mapStyle,// 中心点:北京市center: [116.469000,40.251706],zoom: 10,minZoom: 3,maxZoom: 15,fadeDuration: 100,antialias: true,});map.on('load', () => {this.openPopup()        });},// 地图标记点弹窗openPopup(coordinate = [116.469000,40.251706]) {let el = document.createElement('div')el.id = 'markerId'el.style.backgroundColor= '#6699ff'el.style.width = 20 + 'px'el.style.height = 20 + 'px'el.style.borderRadius = '50%'let marker = new mapboxgl.Marker(el).setLngLat(coordinate).setOffset([0, -19]).addTo(map) // 将标记添加到地图上this.currentMarkers.push(marker)// 添加弹窗const popDetail = Vue.extend(InfoPopup)let vm = new popDetail({propsData: {color: '#6699ff',name: 'xxx',},})vm.$mount() //挂载let popupTemp = vm.$el// 添加弹窗var popup = new mapboxgl.Popup({ closeButton: false, offset: 25, className: 'map-popup' }).setDOMContent(popupTemp)new mapboxgl.Marker(el).setLngLat(coordinate).setPopup(popup).addTo(map)},
},
mounted() {this.initMap()
}
}
</script>

 InfoPopup组件:

<!-- 地图弹窗 -->
<template><div class="info-popup"><div class="info-popup-content"></div>{{ name }}</div></div>
</template><script>
export default {name: '',props: {// 显示名称name: {type: String,default: '名称'}},data() {return {}}
};
</script>
<style lang='less' scoped>
.info-popup {border-radius: 10px;font-family: "Microsoft YaHei";
}
</style>

四、再附上一些可能用到的方法

1. 鼠标经过事件

map.on('mouseenter', 'addLayer的id值', (e) => {console.log('e', e);
});

2. 鼠标点击事件

map.on('click', 'addLayer的id值', (e) => {console.log('e', e);
});

3. 地图平移

// center为要平移的坐标,zoom为平移的缩放级别
map.flyTo({ center:[坐标], zoom: 9 });

Vue Mapbox-GL 在地图中增加图标、线条、标记点击弹窗、地图平移相关推荐

  1. 谷歌地图中给infowindow添加点击事件

    项目中想实现点击地图中marker上面的图标,即点击infowindow之后跳转到另一个界面,发现不可以像监听marker点击事件一样直接给infowindow添加点击事件.解决方法为将content ...

  2. python在地图上增加图层_Python Matplotlib底图在地图上叠加小图

    我正在地图上绘制一架飞机上的数据,我想在飞机上的最新数据点的坐标上插入一张飞机的这个75px×29px PNG图像. 据我所知,pyplot.imshow()是完成此操作的最佳方法.但是,我在第1步挂 ...

  3. vue写的页面title中ico图标不显示的问题

    首先把格式为ico的图片放在文件根目录下: 如图: 然后在根目录下的index.js中加入代码 如图:这时网页的title中 图标不显示,需要在build 文件夹下的webpack.dev.conf. ...

  4. echarts地图中增加图片的实例

    这个是自己从官网扒来的实例,自己进行了一些修改,没有什么技术难度,就是增加了一些参数,直接上代码吧 var geoCoordMap = {'上海': [121.4648,31.2891],'东莞': ...

  5. android标签栏图标大小,如何在android底部导航布局中增加图标大小?

    我在 Android中使用最近由设计库25中的谷歌引入的底部布局导航样式.在我看到的所有教程和问题中,图标中的图像是正常大小,但是我的图像非常小,尽管事实上我保存到drawable文件夹的图像是72× ...

  6. Mapbox GL JS实现炫酷的地图背景

    经常看到网上的各种地图,有着炫酷的地图背景,用户体验非常不错.在Mapbox GL JS这块,其实关于地图的背景没有太多的设置.但当我们想基于Mapbox GL JS实现炫酷的地图背景改怎么办呢?这里 ...

  7. android 右上角 xml,android状态栏右上角增加图标的方法

    目前是在rk4.1系统下进行学习,具体添加方法如下: 1.base\core\res\res\Values\config.xml 增加图标标识字符串 test_hd_status        //在 ...

  8. android状态栏右上角增加图标的方法

    目前是在rk4.1系统下进行学习,具体添加方法如下: 1.base\core\res\res\Values\config.xml 增加图标标识字符串 <item><xliff:g i ...

  9. Echarts自定义地图和添加图标

    介绍:由于我司在项目中使用到了Echarts,所以我开了Echarts的相关栏目来分享在使用上的经验.这是本栏目的第一篇文章,需要Echarts相关基础,之后会介绍Echarts的基础,需要的朋友可以 ...

  10. 关于百度api地图中中文乱码的处理

    1 /注意把 app改成 map , 注意把 images 改成 image/ 地图中的图标消失是原来的icon图片没有了,我们需要把 var icon = new BMap.Icon("h ...

最新文章

  1. 分布式RPC框架性能大比拼 dubbo、motan、rpcx、gRPC、thrift的性能比较
  2. Android开发之工厂模式初探
  3. 苹果内存取证工具volafox
  4. xml解析案例:一个简单的学生管理系统
  5. hadoop在子节点上没有datanode进程
  6. 一个B/S结构自动二次请求需求的实现
  7. idea 2020和2017下载
  8. 前端开发者常用的9个JavaScript图表库
  9. Swift - 将String类型的数字转换成数字类型
  10. 『编程题全队』Alpha 阶段冲刺博客集合
  11. 2022年工作室暑期培训
  12. 技术大众化--10款无需编程的App DIY开发工具
  13. mikumikudance
  14. JetPack之DataStore源码笔记
  15. flask学习笔记代码篇-10
  16. tableau-瀑布图
  17. 微信支付(公众号支付)微信公众平台开发教程(5)
  18. 图像处理-007形态变换(二)
  19. Python 处理Excel内的数据(案例介绍*2)
  20. 卡片的sak为不支持的类型_Python 植物大战僵尸代码实现(2):植物卡片选择和种植...

热门文章

  1. BeagleBone 构建家庭安全系统
  2. 利用百度api接口制作在线语音合成软件
  3. 计算机语音发展,计算机语音合成技术研究及发展方向
  4. linux超进程,linux下查看超线程
  5. m3u8 TS 解密合并转码mp4,支持在线 ,UC、QQ等本地缓存
  6. 如何交叉编译fio并移植到ARM、IOT上
  7. mbr转gpt 无损 linux,无损 MBR转换为GPT加快启动
  8. 【移动端】企业微信移动app测试实战
  9. 《第一行代码——Android》封面诞生记
  10. 计算机没有usb硬件,电脑开机启动项中没有USB设备启动项怎么办?