开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 18

codepp/WMap

forked from 林鹏/WMap
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
wmap
/
map
/
WMarkerCluster.js
wmap
/
map
/
WMarkerCluster.js
WMarkerCluster.js 16.98 KB
一键复制 编辑 原始数据 按行查看 历史
林鹏 提交于 2022年03月28日 14:26 +08:00 . 聚合类下防抖函数修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
import { getUid } from 'ol/util'
import { boundingExtent } from 'ol/extent'
import { fromExtent } from 'ol/geom/Polygon'
import VectorSource from 'ol/source/Vector'
import { Fill, Icon, Stroke, Style, Text } from 'ol/style'
import * as turf from '@turf/turf'
import { OlCluster, OlFeature, OlVectorLayer } from './type'
import { getMarkerClusterDefaultStyle } from './defaultOptions'
import { isFunction, debounce } from './utils'
import img0 from './images/m0.png'
import img1 from './images/m1.png'
import img2 from './images/m2.png'
import img3 from './images/m3.png'
const defaultOptions = {
zIndex: 3, // 覆盖物的叠加顺序
noClusterZoom: 18, // 在zoom及以上不聚合
distance: 40, // 要素将聚集在一起的距离(以像素为单位)
minDistance: 30, // 簇之间的最小距离(以像素为单位)
showViewExtent: true, // 只展示可视区域 Marker
zoomOnClick: true, // 是否点击展开 Cluster
averageCenter: false, // 聚合点的图标位置是否是所有聚合内点的中心点。默认为否
styles: [] // 自定义1-10 ,11-100,101-1000,10001- 聚合物的样式
}
// marker Text样式处理
function createSingleTextStyle(style) {
return {
placement: style.placement, // 默认为point
text: style.label,
offsetX: style.labelXOffset,
offsetY: style.labelYOffset,
fill: new Fill({
// 字体颜色
color: style.fontColor
}),
font: `${style.fontWeight}${style.fontSize}${style.fontFamily}`, // 字体样式
backgroundFill: new Fill({
// 背景颜色
color: style.labelBgColor
}),
backgroundStroke: new Stroke({
// 边框样式
color: style.borderColor,
width: 10,
lineCap: 'round', // 线帽风格 butt, round, 或者 square 默认 round
lineJoin: 'round', // 线连接方式 bevel, round, 或者 miter 默认 round
lineDash: [], // 线间隔模式 控制虚线
lineDashOffset: 0, // 线段间隔偏移 默认0
miterLimit: 10 // 默认10
}),
padding: style.padding,
textBaseline: style.textBaseline, // 似乎无效
textAlign: style.textAlign //文本对齐方式,似乎无效
}
}
function createSingleIconStyle(style) {
const sIconStyle = {
crossOrigin: 'anonymous', // 图片跨域允许
anchor: [0.5, 0.5], // 原点位置
anchorOrigin: 'top-left', // 原点位置偏移方向
anchorXUnits: 'fraction', // 基于原点位置百分比
anchorYUnits: 'fraction', // 基于原点位置像素
offset: [0, 0], // 偏移量设置,相对于原点
scale: 1, // 图标缩放比例
opacity: 1, // 透明度
src: style.icon,
// img: style.img, // 图标的url
rotateWithView: style.rotateWithView, // 是否旋转
rotation: style.rotation // 旋转角度
}
return sIconStyle
}
// 设置聚合物不同数量的自定义样式
const getClusterStyle = (img, size, defaultStyle, style = {}) => {
const { url, offset, angle, textColor, textSize, textWeight } = style
const iconStyle = createSingleIconStyle({
icon: url || img,
offset: offset || [0, 0],
angle: angle || defaultStyle.angle,
rotateWithView: defaultStyle.rotateWithView
})
const textStyle = createSingleTextStyle({
label: size.toString() || defaultStyle.label,
font: defaultStyle.font,
fontSize: textSize || defaultStyle.fontSize,
fontFamily: defaultStyle.fontFamily,
fontWeight: textWeight || defaultStyle.fontWeight,
fontColor: textColor || defaultStyle.fontColor,
labelXOffset: defaultStyle.labelXOffset,
labelYOffset: defaultStyle.labelYOffset,
labelBgColor: defaultStyle.labelBgColor,
borderColor: defaultStyle.borderColor,
borderWidth: defaultStyle.borderWidth,
padding: defaultStyle.padding,
placement: defaultStyle.placement,
textBaseline: defaultStyle.textBaseline,
textAlign: defaultStyle.textAlign
})
return {
textStyle,
iconStyle
}
}
// 设置聚合要素样式
function createClusterStyle(styleCache, defaultStyle, assignOptions, feature) {
const size = feature.get('features').length // 获取该要素所在聚合群的要素数量
let style = styleCache[size]
const styles = assignOptions.styles || []
// styles
if (!style) {
let clusterStyle = {
textStyle: {},
iconStyle: {}
}
if (size == 1) {
// 处理 VMarker 和 正常 Marker
const marker = feature.get('features')[0]
return !marker.get('VMarker') ? marker.getStyle() : null
} else if (size < 11) {
clusterStyle = getClusterStyle(img0, size, defaultStyle, styles[0])
} else if (size < 101) {
clusterStyle = getClusterStyle(img1, size, defaultStyle, styles[1])
} else if (size < 1001) {
clusterStyle = getClusterStyle(img2, size, defaultStyle, styles[2])
} else {
clusterStyle = getClusterStyle(img3, size, defaultStyle, styles[3])
}
style = [
new Style({
image: new Icon(clusterStyle.iconStyle),
text: new Text(clusterStyle.textStyle),
zIndex: defaultStyle.zIndex // 设置层级
})
]
styleCache[size] = style
}
return style
}
// Cluster change 单个 marker 添加
const addOverlaysAction = (map, clusterSource, features, showViewExtent) => {
//获取可视区域范围的1.2倍
const mapsize = map.getSize().map(it_ => it_ * 1.2)
const extent = map.getView().calculateExtent(mapsize)
//获取可视区域范围的几何对象
const viewGeometry = fromExtent(extent)
// console.log(viewGeometry.getBottomLeft())
features.forEach(feature => {
const markers = feature.get('features')
const showZoomFeature = feature.get('showZoomFeature')
if (!markers) {
//不存在 features 即为 单个 VMarker
const VMarker = feature.get('VMarker')
if (VMarker) {
// 获取 VMarker id
const id = VMarker.getId()
// 获取 VMarker 经纬度
const coordinate = VMarker.getPosition()
// 判断是否在可视区域内
const inViewGeometry = viewGeometry.intersectsCoordinate(coordinate)
// 判断是否已经添加
const inOverlaysList = clusterSource.overlayIds.includes(id)
if (!inOverlaysList) {
// 未被添加的 VMarker
if (inViewGeometry && showViewExtent) {
//只添加在可视区域
clusterSource.overlayIds.push(id)
map.add(VMarker)
}
if (!showViewExtent) {
// 可视区域外的也添加
clusterSource.overlayIds.push(id)
map.add(VMarker)
}
}
} else {
// 获取 id
const id = feature.getId()
// 获取 feature 经纬度
const position = feature.get('position')
const coordinate = [position.olng, position.olat]
// 判断是否在可视区域内
const inViewGeometry = viewGeometry.intersectsCoordinate(coordinate)
// 判断是否已经添加
const inOverlaysList = clusterSource.showZoomFeatureIds.includes(id)
clusterSource.singleFeatureIds.add(id)
if (!inOverlaysList) {
// 未被添加
if (inViewGeometry && showViewExtent) {
//只添加在可视区域
clusterSource.showZoomFeatureIds.push(id)
map.add(feature)
}
if (!showViewExtent) {
// 可视区域外的也添加
clusterSource.showZoomFeatureIds.push(id)
map.add(feature)
}
}
}
} else if (showZoomFeature) {
//处理zoom 不聚合情况
feature.setGeometry(null) // 清空聚合图形
addOverlaysAction(map, clusterSource, markers, showViewExtent)
}
})
}
//创建要素聚合数据来源
function createClusterSource(map, vectorSource, options) {
const { distance, minDistance, showViewExtent, noClusterZoom, averageCenter } = options
// 创建要素聚合数据来源
const clusterSource = new OlCluster({
distance: parseInt(distance, 10),
minDistance: parseInt(minDistance, 10),
source: vectorSource,
createCluster(point, features) {
let cluster = null
const zoom = map.getZoom()
const showZoomFeature = zoom >= noClusterZoom
if (features.length == 1) {
const VMarker = features[0].get('VMarker')
clusterSource.singleFeatureIds.add(features[0].getId())
// 创建聚合对象时候,只有一个聚合物情况
cluster = VMarker
? new OlFeature({
VMarker
})
: new OlFeature({
geometry: point,
features,
// 聚合下一个要素,其唯一子 feature 已经注册监听事件,传递 WEvents,在 WMap dispatchAction
WEvents: features[0].get('WEvents')
})
} else {
if (averageCenter) {
const turfFeatures = features.map(feature => {
return turf.point(feature.getGeometry().getCoordinates())
})
const featureCollection = turf.featureCollection(turfFeatures)
const centerCoordinates = turf.center(featureCollection).geometry.coordinates
point.setCoordinates(centerCoordinates)
}
cluster = new OlFeature({
geometry: point,
features,
showZoomFeature
})
cluster.setId(getUid(cluster))
}
cluster.WTYPE = 'ClusterMarker'
return cluster
}
})
// 拖动变化-主动触发 clusterSource change
// 拖动时,判断Feature是否有变化,可优化
map.on('moveend', e => clusterSource.changed(e))
const clusterSourceChangeCallback = function (e) {
const features = e.target.getFeatures()
// 添加前先清除所有 VMarker
clusterSource.overlayIds.forEach(id => {
const VMarker = map.getOverlayById(id)
map.remove(VMarker)
})
clusterSource.showZoomFeatureIds.forEach(id => {
const feature = map.getFeatureById(id)
map.remove(feature)
})
clusterSource.overlayIds = []
clusterSource.showZoomFeatureIds = []
if (features.length > 0) {
// 添加每个聚合物相应的 OverlayMarker
addOverlaysAction(map, clusterSource, features, showViewExtent)
}
}
/**
* 监听聚合物体变化 ,用于清除聚合状态下 自定义overlays数量
* addFeatures/removeFeature 也会触发
*/
clusterSource.on(
'change',
debounce(e => {
clusterSourceChangeCallback(e)
}, 200)
)
return clusterSource
}
/**
* 创建聚合物覆盖群
* @param {object} map
* @param {object} options distance = 40, minDistance = 30
* @param {object} features
* @returns
*/
function createMarkerCluster(map, markers, options) {
const styleCache = {}
const assignOptions = Object.assign({}, defaultOptions, options)
const features = markers.map(marker => {
// 聚合对象单个矢量要素需要重新注册
return marker.olTarget
})
// 创建要素数据来源
const vectorSource = new VectorSource({
features
})
// 创建要素聚合数据来源
const clusterSource = createClusterSource(map, vectorSource, assignOptions)
const defaultStyle = getMarkerClusterDefaultStyle()
// 创建一个图层
const clusterLayer = new OlVectorLayer({
className: 'WMap-clusterer-layer',
source: clusterSource,
zIndex: 1,
style: feature => createClusterStyle(styleCache, defaultStyle, assignOptions, feature)
})
// 默认添加图层
map.addLayer(clusterLayer) // 初始默认数据添加图层
return { clusterLayer, assignOptions }
}
function WMarkerCluster(map, features = [], options) {
if (!map || map.WTYPE !== 'MAP') throw new Error('WMarkerCluster warn: 请传入正确的map对象~')
const events = ['click', 'dblclick', 'contextmenu'] // 支持的事件
const { clusterLayer, assignOptions } = createMarkerCluster(map, features, options) // 聚合图层
this.olTarget = clusterLayer // 聚合图层
this.options = assignOptions
this.map = map
this.WTYPE = 'MARKERCLUSTER'
this.WEvents = new Map() // 存储事件
this.getView = () => this.map.getView() // 获取地图的初始化 View 信息
this.getMaxZoom = () => this.getView().getMaxZoom() // 获取地图设置的最大放大
this.getVectorSource = () => this.olTarget.getSource().getSource() // Marker集合数据对象
this.getClusterSource = () => this.olTarget.getSource() // 聚合物集合数据对象
this.getDistance = () => this.getClusterSource().getDistance() // 聚合的距离
this.setDistance = distance => this.olTarget.getSource().setDistance(distance)
this.getMinDistance = () => this.olTarget.getSource().getMinDistance() // 聚合物的最小间距
this.setMinDistance = minDistance => this.olTarget.getSource().setMinDistance(minDistance)
this.hasFeature = feature => this.getVectorSource().hasFeature(feature) // 矢量要素是否在聚合图层上
this.getId = function () {
return getUid(this.olTarget)
}
// 获取聚合类的所有基础Marker集合
this.getMarkers = () => {
return this.getVectorSource().getFeatures()
}
// 将Marker集合添加到聚合
this.setMarkers = features => {
const distance = this.getDistance()
const minDistance = this.getMinDistance()
if (Array.isArray(features) && features.length > 0) {
this.olTarget && this.map.removeLayer(this.olTarget)
const { clusterLayer, assignOptions } = createMarkerCluster(this.map, features, {
distance,
minDistance
})
this.options = assignOptions
this.olTarget = clusterLayer
return this.olTarget
}
}
// 添加Marker
this.add = (...args) => {
if (args.length === 1) {
if (Array.isArray(args[0])) {
this.add(...args[0])
} else if (args[0].WTYPE === 'OVERLAYMARKER' || args[0].WTYPE === 'FEATUREMARKER') {
const olTarget = args[0].olTarget ? args[0].olTarget : args[0]
this.getVectorSource().addFeature(olTarget)
}
} else {
const markers = args.map(marker => {
if (marker.WTYPE === 'OVERLAYMARKER' || marker.WTYPE === 'FEATUREMARKER') {
return marker.olTarget
}
})
this.getVectorSource().addFeatures(markers)
}
}
// 删除Marker
this.remove = (...args) => {
if (args.length === 1) {
// 单参数或者数组
if (Array.isArray(args[0])) {
this.remove(...args[0])
} else if (args[0].WTYPE === 'FEATUREMARKER' || args[0].WTYPE === 'OVERLAYMARKER') {
// removeFeature 时候 触发 change 事件,并且会 remove VMarker
const olTarget = args[0].olTarget ? args[0].olTarget : args[0]
console.log(this.hasFeature(olTarget))
this.getVectorSource().removeFeature(olTarget)
}
} else {
// 多个参数
args.forEach(marker => this.remove(marker))
}
}
// 清空 Markers
this.clearMarkers = () => {
// 遍历清空已经渲染 OverlayMarkers
this.getClusterSource().overlayIds.forEach(id => {
const VMarker = this.map.getOverlayById(id)
this.map.remove(VMarker)
})
this.getClusterSource().overlayIds = [] // 清空overlayMarkers 数据
this.getClusterSource().showZoomFeatureIds = [] // 清空 showZoomFeature
this.getVectorSource().clear() // 清空 Marker集合数据对象
}
// 点击散开属性
this.setZoomOnClick = function (zoomOnClick = false) {
this.options.zoomOnClick = zoomOnClick
}
/* 聚合物放大展开,视野适应地图
*@param{object} target 聚合features 对象
*@param{Array} options 组成聚合物的 feature 集合
*@return {object} target
*@存在问题,会使 zoom 出现小数
*/
this.setClusterExtentView = function (target, options = {}) {
// 所有要素坐标集合
if (!target || target.WTYPE !== 'ClusterMarker') return
const features = target.get('features')
const coordinates = features.length > 1 ? features.map(r => r.getGeometry().getCoordinates()) : features.getGeometry().getCoordinates()
// 放大地图,让要素刚好出现在视野
this.getView().fit(boundingExtent(coordinates), {
maxZoom: this.getMaxZoom(),
duration: 300,
padding: [100, 100, 100, 100], // 点要素距离视野边距
nearest: true,
...options
})
return target
}
//事件监听
this.on = (eventName, callBack = () => {}) => {
if (callBack && !isFunction(callBack)) throw new Error('请传入正确的 callBack!')
if (!eventName || typeof eventName !== 'string') throw new Error('请传入正确的 eventName!')
const WEventName = !events.includes(eventName) ? eventName : 'WCluster(' + eventName + ')' + this.getId()
const eventObject = {
eventName: WEventName,
callBack,
handler: e => {
// if (this.options.zoomOnClick) {
// this.setClusterExtentView(e.eventTarget)
// }
e.callBack({
type: e.eventName,
target: this,
event: e.event,
eventTarget: e.eventTarget
})
}
}
// 未绑定过事件
if (!this.WEvents.has(eventObject.eventName)) {
//事件监听- 传递如:WMarkerCluster(cliclk)
this.olTarget.on(eventObject.eventName, eventObject.handler)
//储存事件
this.WEvents.set(eventObject.eventName, eventObject)
} else {
const currentEventObject = this.WEvents.get(eventObject.eventName)
// 移除事件
this.olTarget.un(currentEventObject.eventName, currentEventObject.handler)
// 重新设置监听事件
this.olTarget.on(currentEventObject.eventName, eventObject.handler)
//储存新事件
this.WEvents.set(currentEventObject.eventName, eventObject)
}
//设置 WEvents
this.olTarget.set('WEvents', this.WEvents)
}
//事件移除
this.off = eventName => {
eventName = 'WCluster(' + eventName + ')' + this.getId()
if (this.WEvents.has(eventName)) {
// 获取事件对象
const currentEventObject = this.WEvents.get(eventName)
// 移除事件
this.olTarget.un(currentEventObject.eventName, currentEventObject.handler)
// 删除事件存储
this.WEvents.delete(eventName)
this.olTarget.set('WEvents', this.WEvents)
callBack()
}
}
map.setMarkerClusterer(this) // map 上添加 markerCluster
}
export default WMarkerCluster
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

WMap: Wonderful Map 博客地址: https://blog.csdn.net/qq_39404437/article/details/123628440
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/codepp/wmap.git
git@gitee.com:codepp/wmap.git
codepp
wmap
WMap
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /