Jinuss's blog Jinuss's blog
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《Git》
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

东流

前端可视化
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《Git》
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 核心基类

  • Control控件篇

  • Geom几何图形篇

  • Layer图层篇

    • 概览
    • BaseLayer类介绍
      • 概述
      • 源码剖析
        • BaseLayer类的构造函数
        • BaseLayer类的方法
        • get类方法
        • set类方法
      • 总结
    • Layer类介绍
    • BaseVectorLayer类
    • VectorLayer类
  • Renderer篇

  • Feature篇

  • style样式篇

  • 《Openlayers源码》笔记
  • Layer图层篇
东流
2024-12-02
目录

BaseLayer类介绍

# 概述

Layer图层是 Openlayers 中很重要的一个概念和部分,而无论是VectorLayer矢量图层还是VectorTileLayer瓦片图层都是继承BaseLayer实现的。本文主要介绍BaseLayer的核心部分以及实现。

# 源码剖析

BaseLayer类继承自BaseObject类,其实现如下:

class BaseLayer extends BaseObject {
  constructor(options) {
    super();
    this.on;
    this.once;
    this.un;
    this.background_ = options.background;
    const properties = Object.assign({}, options);
    if (typeof options.properties === "object") {
      delete properties.properties;
      Object.assign(properties, options.properties);
    }

    properties[LayerProperty.OPACITY] =
      options.opacity !== undefined ? options.opacity : 1;
    assert(
      typeof properties[LayerProperty.OPACITY] === "number",
      "Layer opacity must be a number"
    );

    properties[LayerProperty.VISIBLE] =
      options.visible !== undefined ? options.visible : true;
    properties[LayerProperty.Z_INDEX] = options.zIndex;
    properties[LayerProperty.MAX_RESOLUTION] =
      options.maxResolution !== undefined ? options.maxResolution : Infinity;
    properties[LayerProperty.MIN_RESOLUTION] =
      options.minResolution !== undefined ? options.minResolution : 0;
    properties[LayerProperty.MIN_ZOOM] =
      options.minZoom !== undefined ? options.minZoom : -Infinity;
    properties[LayerProperty.MAX_ZOOM] =
      options.maxZoom !== undefined ? options.maxZoom : Infinity;

    this.className_ =
      properties.className !== undefined ? properties.className : "ol-layer";
    delete properties.className;

    this.setProperties(properties);

    this.state_ = null;
  }
  getBackground() {
    return this.background_;
  }
  getClassName() {
    return this.className_;
  }
  getLayerState(managed) {
    const state = this.state_ || {
      layer: this,
      managed: managed === undefined ? true : managed,
    };
    const zIndex = this.getZIndex();
    state.opacity = clamp(Math.round(this.getOpacity() * 100) / 100, 0, 1); // clamp函数保证了透明度的区间在[0,1]之间
    state.visible = this.getVisible();
    state.extent = this.getExtent();
    state.zIndex = zIndex === undefined && !state.managed ? Infinity : zIndex;
    state.maxResolution = this.getMaxResolution();
    state.minResolution = Math.max(this.getMinResolution(), 0);
    state.minZoom = this.getMinZoom();
    state.maxZoom = this.getMaxZoom();
    this.state_ = state;

    return state;
  }
  getLayersArray(array) {
    return abstract();
  }
  getLayerStatesArray(states) {
    return abstract();
  }
  getExtent() {
    return this.get(LayerProperty.EXTENT);
  }
  getMaxResolution() {
    return this.get(LayerProperty.MAX_RESOLUTION);
  }
  getMinResolution() {
    return this.get(LayerProperty.MIN_RESOLUTION);
  }
  getMinZoom() {
    return this.get(LayerProperty.MIN_ZOOM);
  }
  getMaxZoom() {
    return this.get(LayerProperty.MAX_ZOOM);
  }

  getOpacity() {
    return this.get(LayerProperty.OPACITY);
  }
  getSourceState() {
    return abstract();
  }
  getVisible() {
    return this.get(LayerProperty.VISIBLE);
  }
  getZIndex() {
    return this.get(LayerProperty.Z_INDEX);
  }
  setBackground(background) {
    this.background_ = background;
    this.changed();
  }

  setExtent(extent) {
    this.set(LayerProperty.EXTENT, extent);
  }

  setMaxResolution(maxResolution) {
    this.set(LayerProperty.MAX_RESOLUTION, maxResolution);
  }
  setMinResolution(minResolution) {
    this.set(LayerProperty.MIN_RESOLUTION, minResolution);
  }

  setMaxZoom(maxZoom) {
    this.set(LayerProperty.MAX_ZOOM, maxZoom);
  }

  setMinZoom(minZoom) {
    this.set(LayerProperty.MIN_ZOOM, minZoom);
  }

  setOpacity(opacity) {
    assert(typeof opacity === "number", "Layer opacity must be a number");
    this.set(LayerProperty.OPACITY, opacity);
  }

  setVisible(visible) {
    this.set(LayerProperty.VISIBLE, visible);
  }

  setZIndex(zindex) {
    this.set(LayerProperty.Z_INDEX, zindex);
  }

  disposeInternal() {
    if (this.state_) {
      this.state_.layer = null;
      this.state_ = null;
    }
    super.disposeInternal();
  }
}
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

BaseLayer类主要就是设置属性,然后提供get和set两大类方法分别用于获取属性和设置属性

# BaseLayer类的构造函数

BaseLayer类的构造函数接受一个options对象类型的参数,显示声明三个变量on,once和un,然后设置this.background_。然后将参数options赋值给properties,再判断options.properties的类型是否是Object,若它存在且其类型是Object,则将其赋值给properties。

再通过一些列的三元判断设置properties的属性,若options中的对应的属性不存在,则设置默认值,如:properties.opacity:透明度,默认为1;properties.visible:是否可见,默认为true;properties.zIndex:层级,无默认值;properties.maxResolution:最大分辨率,默认为无穷大;properties.minResolution:最小分辨率,默认为0;properties.minZoom:最小缩放级别,默认为负无穷;properties.maxZoom:最大缩放级别,默认为正无穷大;

设置对象的className_,若options.className存在,则不存在,则赋值为ol-layer.

最后调用this.setProperties方法,设置一组集合键值对,然后初始化对象的state_为null

# BaseLayer类的方法

# get类方法

get类方法就是调用this.get方法通过key去查找对应值,get方法是在BaseObject类中定义的,只会返回自身的属性值,若不存在则返回undefined

  • getBackground方法:获取this.background_
  • getClassName方法:获取this.className_
  • getLayerState方法

源码中写道getLayerState方法并不应由图层或图层渲染器调用,因为如果图层被包含在图层组中,状态会不正确。其内部就是调用各种get类方法,获取图层的当前值,组成在一个对象中,作为状态返回。

  • getLayersArray,getLayerStatesArray和getSourceState方法均暂未实现

  • getExtent方法:获取对象的边界范围extent

  • getMaxResolution方法:获取对象的最大分辨率maxResolution

  • getMinResolution方法:获取对象的最小分辨率minResolution

  • getMinZoom方法:获取对象的最小缩放级别minZoom

  • getMaxZoom方法:获取对象的最大缩放级别maxZoom

  • getOpacity方法:获取对象的透明度opacity

  • getVisible方法:获取对象的可见性,返回一个布尔值

  • getZIndex方法:获取对象的层级,返回一个数字

# set类方法

set类方法主要是调用set方法实现的,该方法是在BaseObject类中定义的,如果新值和旧值不等,会调用notify方法,若存在该属性的监听事件,就会调用dispatchEvent派发相应事件。

  • setBackground方法

setBackground方法用于设置对象的background_,然后调用changed方法 该方法是在Observable类中定义的,其内部会调用dispatchEvent方法,派发类型为change的注册事件

  • setExtent方法:设置对象的范围extent

  • setMaxResolution方法:设置对象的最大分辨率maxResolution

  • setMinResolution方法:设置对象的最小分辨率minResolution

  • setMaxZoom方法:设置对象的最大缩放级别maxZoom

  • setMinZoom方法:设置对象的最小缩放级别minZoom

  • setOpacity方法:设置对象的透明度,参数必须是一个数字类型的值

  • setVisible方法:设置对象的可见性

  • setZIndex方法:设置对象的层级

# 总结

BaseLayer类中的方法主要分为两大类,而它们又是基于BaseObject类中的get和set方法实现的。

编辑 (opens new window)
上次更新: 2024/12/11, 10:30:03
概览
Layer类介绍

← 概览 Layer类介绍→

最近更新
01
GeoJSON
05-08
02
Circle
04-15
03
CircleMarker
04-15
更多文章>
Theme by Vdoing | Copyright © 2024-2025 东流 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式