1.0.0 • Published 1 year ago

sgup-jsutil-leaflet v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

sgup-jsutil-leaflet 一个基于leaflet封装的操作地图的基类

包的使用

1、安装
npm install sgup-jsutil-leaflet --save

2、面向对象开发
// 导入基类
import SgupJsutilLeaflet from 'sgup-jsutil-leaflet'
// 导入样式
import 'sgup-jsutil-leaflet/dist/leaflet.css'
// 配置文件
import { MarkerConfig, ClusterConfig, LeafletPopup } from './leaflet-config'

// 子类
class SgupLeafletMap extends SgupJsutilLeaflet{
    // 获取点位图标
  getMarkerConfig (markerType) {
    return MarkerConfig[markerType]
  }

  // 获取popup弹框
  getLeafletPopup (markerType) {
    return LeafletPopup[markerType]
  }
    
  // 绘制轨迹
  addTrack (trackList) {
    // 加速度列表
    var speedList = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    this.trackFeature = L.featureGroup()
    this.trackFeature.setZIndex(120)

    // 轨迹线
    const polyline = L.polyline(trackList, {
      color: '#FF00FF',
      fillColor: '#FF00FF',
      weight: 4
    })

    // 轨迹开始点位
    const startPoint = this.creatOrdinaryMarker('tractStart', {
      latitude: trackList[0][0],
      longitude: trackList[0][1],
      id: '0'
    }, new L.Icon({
      iconSize: [40 * this.rate, 40 * this.rate],
      // 图标的取图地址
      iconUrl: require('./icons/track_start.png'),
      // 图标中心点偏移量
      iconAnchor: [20, 20]
    }))

    // 轨迹结束点位
    const endPoint = this.creatOrdinaryMarker('tractEnd', {
      latitude: trackList[trackList.length - 1][0],
      longitude: trackList[trackList.length - 1][1],
      id: '1'
    }, new L.Icon({
      iconSize: [40 * this.rate, 40 * this.rate],
      // 图标的取图地址
      iconUrl: require('./icons/track_end.png'),
      // 图标中心点偏移量
      iconAnchor: [20, 20]
    }))

    // 移动的点位
    const moveMarker = L.animatedMarker(polyline.getLatLngs(), {
      speedList: speedList,
      interval: 3000,
      zIndexOffset: 130,
      icon: new L.Icon({
        iconSize: [32 * this.rate, 32 * this.rate],
        // 图标的取图地址
        iconUrl: require('./icons/track_move.png'),
        // 图标中心点偏移量
        iconAnchor: [16, 16]
      }),
      autoStart: false
    })

    this.trackFeature.addLayer(moveMarker)
    this.trackFeature.addLayer(polyline)
    this.trackFeature.addLayer(startPoint)
    this.trackFeature.addLayer(endPoint)
    this.trackFeature.addTo(this.leafletMap)
    this.leafletMap.setView(trackList[0], 17)

    return moveMarker
  }

  // 清除轨迹
  clearTrack () {
    if (this.trackFeature) {
      const bool = this.leafletMap.hasLayer(this.trackFeature)
      if (bool) {
        this.trackFeature.remove()
      }
    }
  }
    
   // 添加聚合
  addCluster (data, type) {
    const clusterOptions = ClusterConfig[type]
    var cluster = this.clusters[type] || L.markerClusterGroup({
      showCoverageOnHover: false,
      iconCreateFunction: function (cluster) {
        const count = cluster._childCount
        let bgUrl = clusterOptions['level1']
        if (count > 10 && count <= 99) {
          bgUrl = clusterOptions['level2']
        } else if (count > 99) {
          bgUrl = clusterOptions['level3']
        }
        return L.divIcon({ iconSize: [40, 40], html: `<div style="background:url('${bgUrl}') no-repeat;background-size: 100%;width:40px;height:40px;text-align:center;line-height:40px;color: #fff;">${count}</div>` })
      }
    })
    if (!this.clusters[type]) {
      this.leafletMap.addLayer(cluster)
    } else {
      this.clusters[type].clearLayers()
    }
    data.forEach(item => {
      cluster.addLayer(item)
    })
    this.clusters[type] = cluster
    return this.clusters[type]
  }

  // 清除聚合
  clearCluster (type) {
    if (!this.clusters[type]) return

    if (type && this.clusters[type]) {
      this.clusters[type].remove()
      delete this.clusters[type]
    } else {
      for (const key in this.clusters) {
        this.clusters[key] && this.clusters[key].remove()
      }
      this.clusters = {}
    }
  }
  
  // WMS图层返回的数据
  wmsCallback (data) {
    console.log('wms图层数据')
  }
    
}


// 方法使用 创建SgupLeafletMap实例对象
import SgupLeafletMap from '../../map/SgupLeafletMap'
// 地图中心点 纬度在前  必传
const mcenter = [36.78839127856239, 116.68853759765626]
// 地图底图地址 必传
const mtile = '/sgup/tile?x={x}&y={y}&z={z}&customid=midnight&type=WGS84'
// wms服务地址 非必传 默认值为null
const wmsservice = '/geoserver/qh_grid/wms'
// wms服务图层 非必传 默认值为null
const wmslayer = 'qh_grid:qh_grid_1130_84'
const MAP = new SgupLeafletMap(mcenter, mtile, wmsservice, wmslayer)

leaflet-config.js文件

import Vue from 'vue'
import EventPop from './popup/EventPop.vue'

// marker
export const MarkerConfig = {
  event: {
    defaultIcon: require('@/assets/gis/event_default.png'),
    activeIcon: require('@/assets/gis/event_active.png'),
    defaultSize: [34, 42],
    activeSize: [40, 50]
  },
  equipment: {
    defaultIcon: require('@/assets/gis/equipment_default.png'),
    activeIcon: require('@/assets/gis/equipment_default.png'),
    defaultSize: [34, 42],
    activeSize: [40, 50]
  }
}

// 聚合
export const ClusterConfig = {
  equipment: {
    level1: require('@/assets/gis/equipment_0.png'),
    level2: require('@/assets/gis/equipment_1.png'),
    level3: require('@/assets/gis/equipment_2.png')
  }
}

// 弹框
export const LeafletPopup = {
  'event': Vue.extend(EventPop),
}

方法说明

方法名方法用途参数参数说明是否必须使用示例
createMap创建地图对象mapId, optionmapId:地图容器;option:地图配置项是;否MAP.createMap('leaflet-wrap-map', { preferCanvas: true, zoomControl: true, attributionControl: true})
getZoom获取地图层级MAP.getZoom()
setZoom设置地图层级zoomzoom:地图层级,Number类型MAP.setZoom(MAP.getZoom() + 1)
setCenter设置地图中心点latLnglatLng: 经纬度数组MAP.setCenter(36.18418, 116.162767)
getCenter获取地图中心点MAP.getCenter()
setCenterZoom设置中心点及层级latLng, zoomlatLng:中心点数组;zoom:层级MAP.setCenterZoom(36.18418, 116.162767, 15)
addFeature添加图层typetype: 图层类型,String类型MAP.addFeature('event')
getFeature获取图层typetype: 图层类型,String类型MAP.getFeature('event')
removeaFeature清除图层typetype: 图层类型,String类型,不传则删除全部图层·否MAP.removeaFeature('event')
addPolyLine绘制线latLngs, lineType, optionlatLngs:线数组,lineType:线类型, option:线配置是,是,否MAP.addPolyLine([36.78839127856239, 116.68853759765626, 36.789954, 116.725273], 'line')
clearPolyLine清除线lineType线类型,StringMAP.clearPolyLine('line')
addCricle绘制圆形latLng,cricleType radiuslatLng:中心点, cricleType: 类型,radius:半径是,是,否MAP.addCricle(36.789954, 116.725273, 'cricle')
clearCricle清除圆形cricleTypecricleType: 类型,MAP.clearCricle('cricle')
addOneMarker添加点位detail,clickEvent,showDetaildetail:点位详情,其中要包含isCluster和markerType字段,isCluster表示是否聚合,markerType表示点位类型,clickEvent:点位点击事件,showDetail:是否显示详情是,否,否image-20230210102608998
clearMarker删除点位type点位类型MAP.clearMarker('event')
addAreaPolygon绘制地理边界data, callback, showName , color, fillColordata:网格边界数据, callback:网格的点击回调, showName :是否显示网格标注,如果需要展示网格标注,请在网格边界数据中心添加groupName字段, color:网格边界颜色, fillColor:网格填充色是,否,否,否,否MAP.addAreaPolygon(data, this.gridClick, true)
clearAreaPolygon清除地理边界MAP.clearAreaPolygon()
addHeatMap绘制热力图data, typedata:热力图数据, type:热力图类型是,是image-20230210103920891
clearHeatmap清除热力图type热力图类型MAP.clearHeatmap('equipment')
clearWMS清除WMS图层MAP.clearWMS()
addWMS添加WMS图层MAP.addWMS()