代码拉取完成,页面将自动刷新
基于openlayers6 开发的地图库
WMap: Wonderful Map
博客地址: https://blog.csdn.net/qq_39404437/article/details/123628440
csdn: 林大大哟, 有bug/建议 请关注我并私聊 / gitee提 issue
本地开发环境:本项目由vite启动,安装依赖使用pnpm/npm install, pnpm/npm run dev 启动后 请添加 /demos/home.html 导航到对应demo示例
例如: http://localhost:3000/demos/home.html
生产环境:打包请使用 pnpm/npm run build, 然后根据你的需求,使用其中 umd.js/es.js的包,并且引入样式表 index.css
希望老铁们能赞助些,作为独立开发者太难了...我会第一时间将赞助列表放在日志上噢
注:国内经纬度是需要GPS类来对WGS转GCJ坐标系处理的(无论是高德或是其他地图,都是需要这步进行转化,因为这是国内GCJ坐标系限制)
const point = [116.482301, 39.99639]
const encPoint = new WMap.GPS().gcj_encrypt(point[0], point[1]) // 加密后数据
const decPoint = new WMap.GPS().gcj_decrypt(encPoint.lng, encPoint.lat) // 解密后数据
// 注意GPS类转化的时候,所属方法第一参数为纬度,第二参数为经度,除此以外,本地图其他地方都是先经度后纬度
方法详解:
gcj_encrypt(wgsLon, wgsLat) // WGS-84 to GCJ-02
gcj_decrypt(gcjLon, gcjLat) // GCJ-02 to WGS-84
注: 本地图所有传入经纬度都必须先用经纬度类转换再使用
const point = [116.477648,39.997149] // 原始坐标
// 默认地图使用的是'EPSG:3857'坐标系, 传入的值是 'EPSG:4326'坐标系
// 参数详解
lng: number // 经度
lat: number // 纬度
// 输出
{
this.olat: number // 'EPSG:3857' 下纬度
this.olng: number // 'EPSG:3857' 下经度
this.lat: number // 传入的纬度
this.lng: number // 传入的经度
}
// 方法详解
fromLonLat(coordinate: {lng: number, lat: number}, projection: string = 'EPSG:3857') // 经纬度坐标转换 EPSG:4326 - 投影的坐标 projection
toLonLat(coordinate: {lng: number, lat: number}, projection: string = 'EPSG:3857') // 投影的坐标 projection - 经纬度坐标转换 EPSG:4326
getLng() // 获取传入经度
getLat() // 获取传入纬度
offset(x: number, y: number) // 当前经纬度坐标值经度移动w,纬度移动s,得到新的坐标, 经度向右移为正值,纬度向上移为正值,单位为米
distance(lnglat: {lng: number, lat: number}) // 当前经纬度和传入经纬度或者经纬度数组连线之间的地面距离,单位为米
outOfChina(lat: number, lng: number) // 判断坐标是否在中国以外
const map = new WMap.Map(target, options)
// 参数详解
target:'map', // 默认绑定dom
options:{
center: new WMap.LngLat(97.848297, 35.49053), //地图中心
zoom: 6, //地图缩放层级
minZoom: 0, // 最小可缩放层级
maxZoom: 18, // 最大可缩放层级
doubleClickZoom: false, // 是否双击放大
zoomShow: true, // 是否显示缩放控件
rotateShow: false, // 是否显示旋转复位控件
fullScreen: true, // 是否显示全屏控件
maxExtent: false, // 是否限制地图可拖动范围
mouseMoveExtent: boundingExtent([ // 默认展示的范围
[55.923433618652325, 3.359091468750009],
[171.31664592155698, 81.65358702968221]
])
}
// 方法详解
map.getCenter() // 获取地图的中心位置
map.getMaxZoom() // 获取地图设置的最大缩放
map.getMinZoom() // 获取地图设置的最小缩放
map.updateSize() // 强制更新地图视口大小
map.getCoordinateFromPixel() // 获取当前点击坐标点
map.getPixelFromCoordinate() // 获取当前经纬度坐标像素点
map.getZoom() // 获取当前层级
map.setZoom(zoom: number) // 设置当前层级
map.setCenter(coord: Array[number], zoom: number = 0) // 设置地图中心
map.panTo(coord: Array[number]) // 定位到目标位置
map.setBaseLayer(layerName: string = standard) // satellite 卫星地图图层, standard 标准地图图层
map.add(markers: Array[Marker] | Marker | INFOWINDOW | ContextMenu | POLYLINE) // 添加 单个或者多个覆盖物(包括但不限于覆盖物,信息框,右键菜单,轨迹线)
map.remove(markers: Array[Marker] | Marker | INFOWINDOW | ContextMenu | POLYLINE) // 移除 单个或者多个覆盖物(包括但不限于覆盖物,信息框,右键菜单,轨迹线)
map.setFitView() // 地图自动适应所有覆盖物或矢量图形, 以达到最佳视窗
map.setZoomOut(zoomNum: number = 1) // 地图缩小
map.setZoomIn(zoomNum: number = 1) // 地图放大
map.removeGraph(draw: VectorGraph) // 移除交互图形
map.addGraph(draw: VectorGraph) // 添加交互图形
map.getInteractions() // 获取所有交互图形
map.clear() // 清除地图上的各种类型覆盖物(包括不限于聚合/普通覆盖物/矢量图形/历史轨迹)
map.getMarkerById(id) // 获取聚合下单个覆盖物
map.calculateCenter(Array[Marker]) // 计算覆盖物集群的中心点坐标
map.setBaseMap(mapName: 'amap', token?: 'xxx') // 动态设置图层集合 高德setBaseMap('amap'), 天地图setBaseMap('tianditu', token)
map.getBaseMap() // 获取当前正在显示的底图图层
map.addLayer(layer) // 添加图层
map.getMarkerClusterer() // 获取聚合图层
map.setMarkerClusterer() // 设置聚合图层
map.setTop() // 设置置顶
// 支持事件
['complete', 'moveend', 'click', 'dblclick', 'contextmenu', 'moving', 'pointermove']
map.on('click', function (e) {
// e 为地图对象
// e.lngLat 当前点击坐标点对象
})
const marker = new WMap.Marker(options)
// 参数详解:
options: {
// 通用参数
map?: null, // 是否直接渲染到地图上, 加上此参数,就不用map.add(marker)
id: null, // 覆盖物id
position: [0, 0], // 坐标经纬度
angle: 0, //角度
offset: [0, 0], //偏移量
zIndex: 3, // 矢量图形层级
extData: {}, // 自定义数据
// 自定义配置下参数
content: '', // 自定义html内容
// 常规配置下参数
icon: '', // 显示图片,有默认
rotateWithView: true, // 是否跟随视图旋转
angle: 0, // 图片旋转角度
font: 'normal 12px sans-serif',
label: '',
fontSize: '12px',
fontFamily: 'sans-serif',
fontWeight: 'normal',
fontColor: '#000',
placement: 'point', // 默认为point
labelBgColor: '#fff',
borderColor: '#000', // 边框颜色
borderWidth: '1', // 边框宽度
textBaseline: 'bottom', // t 似乎无效
textAlign: 'centrer', //文本对齐方式 ,似乎无效,设置会让文本消失
labelXOffset: 0, // 水平文本偏移量
labelYOffset: -30, // 垂直文本偏移量
padding: [5, 5, 5, 5]
}
// 注: Marker分为两种模式,传了content就是自定义配置,不传就是默认的常规配置,
区别就是前者继承将继承于Overlay基类, 后者继承于Feature基类
// 方法详解:
marker.getId() // 获取id
marker.getExtentData() // 获取自定义信息
marker.getPosition() // 获取 Marker 坐标
marker.getOffset() // 获取Marker 偏移
//常规模式
marker.setExtentData(extData: Object) // 设置自定义信息
marker.setAngle(angle: number = 0) // 设置 Marker 角度
marker.setPosition(position: Array[number]) // 设置 Marker 坐标
marker.setId(id: number) // 设置id
marker.move(polyline: Polyline, curPath: 坐标点) // 覆盖物在实时轨迹下的移动
// 自定义模式
marker.setTop() // 置顶
marker.setElement() // 设置 content / html
marker.getContent() // 获取 content str
marker.setPosition(position: Array[number]) // 设置 Marker 坐标
marker.setAnimation(animationName: any) // 设置 Marker 弹跳动画
marker.setOpacity(opacity: number = 1) 设置样式 透明度
marker.setAngle(angle:number = 0) // 设置 Marker 角度
// 支持事件
marker.on('click', e => {
// e.type 事件类型
// e.target dom对象
console.log(e.target.getExtentData()) // 初始化Marker时有extData则获取,无则返回undefined
})
const markerClusterer = new WMap.MarkerCluster(map, markers)
// 参数详解:
map: Map, // 地图实例对象
markers: Array[Marker] // 覆盖物群体
针对聚合类中的Marker配置
options: {
zIndex: 3, // 覆盖物的叠加顺序
noClusterZoom: 18, // 在zoom及以上不聚合
distance: 40, // 要素将聚集在一起的距离(以像素为单位)
minDistance: 30, // 簇之间的最小距离(以像素为单位)
showViewExtent: true, // 只展示可视区域 Marker
zoomOnClick: true, // 是否点击展开 Cluster
averageCenter: false, // 聚合点的图标位置是否是所有聚合内点的中心点。默认为否
styles: Array[clustererStyle] // 自定义1-10 ,11-100,101-1000,10001- 聚合物的样式
}
clustererStyle: {
url, offset, angle, textColor, textSize, textWeight // 常用的url 就是指各个聚合层级下的显示图片,不设置则使用默认的
}
// 方法详解:
markerClusterer.setZoomOnClick(flag) // flag 为 true, 则点击聚合展开, 默认为false, 点击聚合不展开
markerClusterer.getMaxZoom() // 获取地图设置的最大放大
markerClusterer.getDistance() // 获取聚合的距离
markerClusterer.setDistance(distance: number) // 设置聚合物距离
markerClusterer.getMinDistance() // 获取聚合物的最小间距
markerClusterer.setMinDistance(distance: number) // 设置聚合物的最小间距
markerClusterer.getMarkers() // 获取聚合类的所有基础Marker集合
markerClusterer.setMarkers(markers: Array[Marker]) // 将Marker集合添加到聚合(覆盖性添加Marker)
markerClusterer.add(Marker) // 添加Marker
markerClusterer.remove(Marker) // 删除Marker
markerClusterer.clearMarkers() // 清空 Markers
// 支持事件
['click', 'dblclick', 'contextmenu']
const graph = new WMap.VectorGraph(map, options = {})
// 参数详解:
map: 地图实例对象
options: { // 样式参数 会影响整个矢量图层(className为'VectorLayer'图层)
fillColor: 'rgba(37,241,239,0.2)', // 填充颜色
strokeColor: '#264df6', // 笔触颜色
strokeWidth: 2, // 笔触宽度
lineCap: 'round', // 线帽样式 butt、round、 或square
lineJoin: 'round', // 线连接样式 bevel、round、 或miter
imageRadius: 7, // 点样式半径大小
imageFill: '#264df6', // 点样式填充颜色
}
// 方法详解:
graph.activate(graphName: string) // 激活矢量图绘制(graphName共五种, 分别为Point, Line, Circle, Polygon, Rectangle, 对应画哪种图形)
graph.deactivate() // 失活矢量图绘制(终止绘图)
// option 为专属于该图形的样式, 不会影响到整个图层, extData 为自定义数据
graph.getExtData() // 获取 自定义数据
graph.clearVector() // 清除图层上的数据源(矢量图形)
// 支持事件:
['done']
const edit = new WMap.GraphEdit(map)
// 参数详解:
map: 地图实例对象
// 方法详解
edit.open() // 开启编辑
edit.close() // 结束编辑
// 事件支持
['modifyend']
let point = new WMap.Point({
map: map,
center: new WMap.LngLat(116.479662,39.996956),
extData: {'name': '点'}
})
// 左键单击
point.on('click', e => {
console.log(e.target.getExtData())
})
// 右键单击
point.on('contextmenu', e => {
console.log(e.target.getExtData())
})
// 左键双击
point.on('dblclick', e => {
console.log(e.target.getExtData())
})
// 参数详解
map: 直接传入地图实例, 若不填写此参数, 则可以 map.add 进行添加
center: 经纬度位置
extData: 自定义数据
// 方法详解
point.show() // 地图上显示点
point.hide() // 地图上隐藏点
point.getExtData() // 获取自定义数据
let line= new WMap.Line({
map: map,
path: [new WMap.LngLat(116.477648,39.997149),new WMap.LngLat(116.480078, 39.996506)],
extData:{'name': '线'}
})
// 左键单击
line.on('click', e => {
console.log(e.target.getExtData())
})
// 右键单击
line.on('contextmenu', e => {
console.log(e.target.getExtData())
})
// 左键双击
line.on('dblclick', e => {
console.log(e.target.getExtData())
})
// 参数详解
map: 直接传入地图实例, 无不填写此参数, 则可以map.add 进行添加
path: 经纬度数组
extData: 自定义数据
// 方法详解
line.show() // 地图上显示线
line.hide() // 地图上隐藏线
line.getExtData() // 获取自定义数据
line.setPath() // 主动改变线段
const circle = new WMap.Circle({
map: map,
center: new WMap.LngLat(116.47614550262452, 39.99763225396728),
radius: 81.7,
extData:{'name': '圆'},
option: { // 自定义样式参数
fillColor: '#264df6', // 填充颜色
strokeColor: '#264df6', // 笔触颜色
strokeWidth: 2, // 笔触宽度
lineCap: 'round', // 线帽样式 butt、round、 或square
lineJoin: 'round', // 线连接样式 bevel、round、 或miter
imageRadius: 7, // 点样式半径大小
imageFill: '#264df6' // 点样式填充颜色
}
})
// 左键单击
circle.on('click', e => {
console.log(e.target.getExtData())
})
// 右键单击
circle.on('contextmenu', e => {
console.log(e.target.getExtData())
})
// 左键双击
circle.on('dblclick', e => {
console.log(e.target.getExtData())
})
// 参数详解:
map: 直接传入地图实例, 无不填写此参数, 则可以map.add 进行添加
center: 经纬度位置
radius: 半径长度
extData: 自定义数据
// 方法详解
circle.show() // 地图上显示圆形
circle.hide() // 地图上隐藏圆形
circle.getExtData() // 获取自定义数据
const polygon = new WMap.Polygon({
map: map,
path: [new WMap.LngLat(116.47699844509125, 39.9982652552948), new WMap.LngLat(116.47721838623048, 39.997278202377316), new WMap.LngLat(116.47869360118867, 39.99738549073791), new WMap.LngLat(116.47857558399201, 39.9979058392868), new WMap.LngLat(116.47837710052491, 39.99842618783569)],
extData: {'name': '多边形'},
option: { // 自定义样式参数
fillColor: '#264df6', // 填充颜色
strokeColor: '#264df6', // 笔触颜色
strokeWidth: 2, // 笔触宽度
lineCap: 'round', // 线帽样式 butt、round、 或square
lineJoin: 'round', // 线连接样式 bevel、round、 或miter
imageRadius: 7, // 点样式半径大小
imageFill: '#264df6' // 点样式填充颜色
}
})
// 左键单击
polygon.on('click', e => {
console.log(e.target.getExtData())
})
// 右键单击
polygon.on('contextmenu', e => {
console.log(e.target.getExtData())
})
// 左键双击
polygon.on('dblclick', e => {
console.log(e.target.getExtData())
})
// 参数详解:
map: 直接传入地图实例, 无不填写此参数, 则可以map.add 进行添加
path: 经纬度数组
extData: 自定义数据
// 方法详解
polygon.show() // 地图上显示多边形
polygon.hide() // 地图上隐藏多边形
polygon.getExtData() // 获取自定义数据
const rectangle = new WMap.Rectangle({
map: map,
path: [new WMap.LngLat(116.48053896099091,39.99640916665649), new WMap.LngLat(116.48225557476044, 39.99715482076263)],
extData: {'name': '矩形'},
option: { // 自定义样式参数
fillColor: '#264df6', // 填充颜色
strokeColor: '#264df6', // 笔触颜色
strokeWidth: 2, // 笔触宽度
lineCap: 'round', // 线帽样式 butt、round、 或square
lineJoin: 'round', // 线连接样式 bevel、round、 或miter
imageRadius: 7, // 点样式半径大小
imageFill: '#264df6' // 点样式填充颜色
}
})
// 左键单击
rectangle.on('click', e => {
console.log(e.target.getExtData())
})
// 右键单击
rectangle.on('contextmenu', e => {
console.log(e.target.getExtData())
})
// 左键双击
rectangle.on('dblclick', e => {
console.log(e.target.getExtData())
})
// 参数详解:
map: 直接传入地图实例, 无不填写此参数, 则可以map.add 进行添加
path: 经纬度数组
extData: 自定义数据
// 方法详解
rectangle.show() // 地图上显示多边形
rectangle.hide() // 地图上隐藏多边形
rectangle.getExtData() // 获取自定义数据
// 实例化信息框类有多种方式
// 方式1 常规模式
let Single1 = new WMap.InfoWindow({
title: '标记点',
content: '这是一个标记点喔',
offset: [0, 0],
width: 200,
height: 100
})
// 方式2 自定义模式之传入DOM
let Single2 = new WMap.InfoWindow({
content: document.querySelector('#single'),
offset: [0, 0],
width: 200,
height: 100
})
// 方式3 自定义模式之传入html字符串
let Single = new WMap.InfoWindow({
content: `<div>123</div>`,
offset: [0, 0],
width: 200,
height: 100
})
// 参数详解: (我是根据传入的content来判断是那种方式的)
content: (string | HTMLDivElement | HtmlString)
title: string = '信息框' // 常规模式下默认标题
offset: Array[number] = [0, 0] // 第二偏移
position: string = 'top-left' // 第一偏移
width = 200 // 默认信息框总宽度 box-sizing: border-box
height = 100 // 默认信息框总高度 box-sizing: border-box
// 支持事件
['click', 'dblclick', 'contextmenu']
我们的轨迹主要针对小车运行时候的轨迹,目前主要分为三类实例
1、历史双轨迹线实例
2、历史单轨迹线实例
3、实时轨迹线实例
每类实例都由Polyline类和Marker类组成
Class Polyline {
mode: 'all', // 轨迹线模式
path: [], // 点坐标数组
zIndex: 2, // 覆盖物的叠加顺序
showDir: true, // 是否显示白色方向箭头
strokeOpacity: 0.9, // 线条透明度
strokeWeight: 10, // 线条宽度
strokeStyle: 'solid', // 轮廓线样式,实线: solid 虚线:dashed
strokeDasharray: [], // 轮廓线间隙
lineCap: 'butt', // 折线两端线帽的绘制样式,默认值为'butt'无头,其他可选值:'round'圆头、'square'方头
lineJoin: 'round', // 折线拐点的绘制样式,默认值为'round'圆角,其他可选值:'round'圆角、'bevel'斜角
isOutline: false, // 线条是否带描边,默认false
borderWeight: 10, // 描边的宽度,默认为1
outlineColor: '#000000', // 线条描边颜色,此项仅在isOutline为true时有效,默认:#000000
extData: null, // 自定义信息
segmentLength: 50, // 对传入路径做分段处理,以防有路径间隔过远,默认为 50 m
strokeColor?: '#28F', // 线颜色
activeStrokeColor?: 'green', // 活跃状态时的线条颜色
segmentLength: 2, // 最小子路径长度
units: 'meters' // 最小子路径单位
}
Polyline类通用方法
polyline.setPath() // 设置当前路径的坐标集合
polyline.clearHistoryPath() // 清除历史轨迹路径
polyline.clearRealPath() // 清除实时轨迹路径
polyline.getHistoryPath() // 获取历史轨迹路径
polyline.getRealPath() // 获取实时轨迹路径
polyline.setVisible(type: boolean) // 隐藏/显示 历史轨迹线(只适用于有历史轨迹线模式)
Class Marker => 我们文档上面都有Marker覆盖物类的具体讲解,在本轨迹中不再讲解Marker覆盖物类
// 覆盖物和轨迹线结合使用的方法
marker.move(polyline, curPath) // 实时轨迹下的小车开始移动
marker.getIndex() // 历史轨迹下的获取行驶路径原始索引
marker.getMoveIndex() // 历史轨迹下的获取行驶路径虚拟索引
marker.getRatio() // 历史轨迹下的获取小车当前进度
marker.updateMoveDistance(radio: Number) // 历史轨迹下的更新小车当前距离进度(用于拖拽进度条),参数为浮点数,0-1之间
marker.updateMoveSpeed(radio: Number) // 历史轨迹下的更新小车当前行驶速度
marker.moveAlong(path = [], angleFixed = false, speed = 60, lockScope = true) // 历史轨迹下的小车开始行驶动画, 建议默认第四参数以达到更优效果
marker.stopMove() // 历史轨迹下的小车终止行驶操作
marker.resumeMove() // 历史轨迹下的小车继续行驶操作
marker.pauseMove() // 历史轨迹下的小车暂停行驶操作
// 控制当前行驶时是否固定角标
方式1: 初始化时控制
marker.moveAlong 的第二参数,true则固定。默认为不固定
方式2: 动态控制角度 只需控制 e.target.angleFixed 为true或false则可以实现实时控制角度固定
marker1.on('moving', function (e) {
e.target.angleFixed = angleFixed.checked
})
那如何区分怎么实例自己想要的呢?
我们在 Polyline类实例时可以区分:
有 strokeColor 和 activeStrokeColor 参数,就是历史双轨迹线
有 strokeColor 参数,就是历史单轨迹线
有 activeStrokeColor 参数,就是实时轨迹线
下面我会在各个例子中详解每种的用法及例图
// 右键菜单类实例
const contextMenu = new WMap.ContextMenu()
// 默认属性
this.defaultMenuList = [ // 默认菜单栏
{
title: '放大一级',
callBack: () => {
this.map.setZoomIn()
}
},
{
title: '缩小一级',
callBack: () => {
this.map.setZoomOut()
}
}
]
// 方法详解:
// 添加子菜单
contextMenu.addMenu({
title: string // 新增子菜单标题
callBack: Function // 点击子菜单后回调函数
})
// 移除子菜单
contextMenu.removeMenu(title: string) // 移除子菜单标题
// 显示右键菜单栏
contextMenu.open(map: Map, coordinate: Array[lng: number, lat: number]) // map为地图实例对象, coordinate为坐标点
let tools = new WMap.Tool(map, {
style: 样式,
unit: 进制单位(km | m)
})
Tool类参数:
map // 地图实例
style // 样式,如果不设置,将会默认使用通用样式
unit // 进制单位(km | m) ,在测量长度或面积时,返回的结果以km/m显示,默认为km
Tool类方法:
tools.setStyle() // 设置显示样式
tools.getStyle() // 获取显示样式
tools.close() // 停止工具类使用
tools.distance() // 开始测量距离
tools.area() // 开始测量面积
tools.remove(featrue) // 删除对应的矢量图形,feature为下面done事件返回的feature
// 监听工具类完成返回结果
tools.on('done', e => {
console.log(e)
feature = e.feature
})
(https://blog.csdn.net/qq_39404437/article/details/123628440)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型