Namespace: CommonUtil

CommonUtil

common 工具类。

Usage
import { CommonUtil } from '@supermapgis/iclient-leaflet';

const result = CommonUtil.getElement();
<script type="text/javascript" src="https://iclient.supermap.io/dist/leaflet/iclient-leaflet.js"></script>
<script>
  const result = L.supermap.CommonUtil.getElement();

  // 弃用的写法
  const result = SuperMap.Util.getElement();

</script>

Members

createUniqueID

创建唯一 ID 值。

DEFAULT_PRECISIONnumber

为了避免浮点精度错误而保留的有效位数。

Default Value:
14

extend

复制源对象的所有属性到目标对象上,源对象上的没有定义的属性在目标对象上也不会被设置。

Example
要复制 Size 对象的所有属性到自定义对象上,使用方法如下:
    var size = new Size(100, 100);
    var obj = {};
    CommonUtil.extend(obj, size);

isSupportCanvas

浏览器是否支持 Canvas。

lastSeqIDnumber

不断递增计数变量,用于生成唯一 ID。

Default Value:
0

Methods

applyDefaults(to, from){Object}

common/commontypes/Util.js, line 349

比较两个对象并合并。

Name Type Description
to Object 可选

目标对象。

from Object

源对象。

Returns:
Type Description
Object 返回合并后的对象。

assign(dest, arguments){Object}

common/commontypes/Util.js, line 140

对象拷贝赋值。

Name Type Description
dest Object

目标对象。

arguments Object

待拷贝的对象。

Returns:
Type Description
Object 赋值后的目标对象。

calculateDpi(viewBounds, viewer, scale, coordUnit, datumAxis){number}

common/commontypes/Util.js, line 670

计算 iServer 服务的 REST 图层的显示分辨率,需要从 iServer 的 REST 图层表述中获取 viewBounds、viewer、scale、coordUnit、datumAxis 五个参数,来进行计算。

Name Type Default Description
viewBounds Bounds

地图的参照可视范围,即地图初始化时默认的地图显示范围。

viewer Size

地图初始化时默认的地图图片的尺寸。

scale number

地图初始化时默认的显示比例尺。

coordUnit string 'degrees' 可选

投影坐标系统的地图单位。

datumAxis number 6378137 可选

地理坐标系统椭球体长半轴。用户自定义地图的 Options 时,若未指定该参数的值,则系统默认为 WGS84 参考系的椭球体长半轴 6378137。

Returns:
Type Description
number 图层显示分辨率。

cloneObject(obj){Object}

common/commontypes/Util.js, line 933

克隆一个 Object 对象。

Name Type Description
obj Object

需要克隆的对象。

Returns:
Type Description
Object 对象的拷贝对象,注意是新的对象,不是指向。

convertPath(path, pathParams){string}

common/commontypes/Util.js, line 1054

获取转换后的path路径。

Name Type Description
path string

待转换的path,包含{param}

pathParams Object

path中待替换的参数。

Returns:
Type Description
string 转换后的path路径。

copy(des, soc)

common/commontypes/Util.js, line 179

对象拷贝。

Name Type Description
des Object 可选

目标对象。

soc Object

源对象。

copyAttributes(destination, source)

common/commontypes/Util.js, line 879

属性拷贝,不拷贝方法类名(CLASS_NAME)等。

Name Type Description
destination Object 可选

拷贝目标。

source Object

源对象。

copyAttributesWithClip(destination, source, clip)

common/commontypes/Util.js, line 900

将源对象上的属性拷贝到目标对象上。(不拷贝 CLASS_NAME 和方法)

Name Type Description
destination Object 可选

目标对象。

source Object

源对象。

clip Array.<string>

源对象中禁止拷贝到目标对象的属性,目的是防止目标对象上不可修改的属性被篡改。

getBrowser(){Object}

common/commontypes/Util.js, line 619

获取浏览器相关信息。支持的浏览器包括:Opera,Internet Explorer,Safari,Firefox。

Returns:
Type Description
Object 浏览器名称、版本、设备名称。对应的属性分别为 name, version, device。

getElement(){Array.<HTMLElement>}

common/commontypes/Util.js, line 222

获取 HTML 元素数组。

Returns:
Type Description
Array.<HTMLElement> HTML 元素数组。

getParameters(url){Object}

common/commontypes/Util.js, line 505

从 URL 字符串中解析出参数对象。

Name Type Description
url string

URL。

Returns:
Type Description
Object 解析出的参数对象。

getParameterString(params){string}

common/commontypes/Util.js, line 390

将参数对象转换为 HTTP 的 GET 请求中的参数字符串。例如:"key1=value1&key2=value2&key3=value3"。

Name Type Description
params Object

参数对象。

Returns:
Type Description
string HTTP 的 GET 请求中的参数字符串。

getResolutionFromScale(scale, units){number}

common/commontypes/Util.js, line 586

比例尺转分辨率。

Name Type Default Description
scale number

比例尺。

units string 'degrees' 可选

比例尺单位。

Returns:
Type Description
number 转化后的分辨率。

getResolutionFromScaleDpi(scale, dpi, coordUnit, datumAxis){number}

common/commontypes/Util.js, line 804

根据比例尺和 DPI 计算屏幕分辨率。

Name Type Default Description
scale number

比例尺。

dpi number

图像分辨率,表示每英寸内的像素个数。

coordUnit string 可选

投影坐标系统的地图单位。

datumAxis number 6378137 可选

地理坐标系统椭球体长半轴。用户自定义地图的 Options 时,若未指定该参数的值,则 DPI 默认按照 WGS84 参考系的椭球体长半轴 6378137 来计算。

Returns:
Type Description
number 当前比例尺下的屏幕分辨率。

getScaleFromResolution(resolution, units){number}

common/commontypes/Util.js, line 605

分辨率转比例尺。

Name Type Default Description
resolution number

分辨率。

units string 'degrees' 可选

分辨率单位。

Returns:
Type Description
number 转化后的比例尺。

getScaleFromResolutionDpi(resolution, dpi, coordUnit, datumAxis){number}

common/commontypes/Util.js, line 837

根据 resolution、dpi、coordUnit 和 datumAxis 计算比例尺。

Name Type Default Description
resolution number

用于计算比例尺的地图分辨率。

dpi number

图像分辨率,表示每英寸内的像素个数。

coordUnit string 可选

投影坐标系统的地图单位。

datumAxis number 6378137 可选

地理坐标系统椭球体长半轴。用户自定义地图的 Options 时,若未指定该参数的值,则 DPI 默认按照 WGS84 参考系的椭球体长半轴 6378137 来计算。

Returns:
Type Description
number 当前屏幕分辨率下的比例尺。

getTextBounds(style, text, element){Object}

common/commontypes/Util.js, line 1021

获取文本外接矩形宽度与高度。

Name Type Description
style ThemeStyle

文本样式。

text string

文本内容。

element Object

DOM 元素。

Returns:
Type Description
Object 裁剪后的宽度,高度信息。

indexOf(array, obj){number}

common/commontypes/Util.js, line 282

获取某对象在数组中的索引值。

Name Type Description
array Array.<Object>

数组。

obj Object

对象。

Returns:
Type Description
number 某对象在数组中的索引值。

isArray(a){boolean}

common/commontypes/Util.js, line 254

判断一个对象是否是数组。

Name Type Description
a Object

对象。

Returns:
Type Description
boolean 是否是数组。

isElement(o){boolean}

common/commontypes/Util.js, line 244

instance of 的跨浏览器实现。

Name Type Description
o Object

对象。

Returns:
Type Description
boolean 是否是页面元素。

isInTheSameDomain(url){boolean}

common/commontypes/Util.js, line 645

判断一个 URL 请求是否在当前域中。

Name Type Description
url string

URL 请求字符串。

Returns:
Type Description
boolean URL 请求是否在当前域中。

lineIntersection(a1, a2, b1, b2){Object}

common/commontypes/Util.js, line 975

判断两条线段是不是有交点。

Name Type Description
a1 GeometryPoint

第一条线段的起始节点。

a2 GeometryPoint

第一条线段的结束节点。

b1 GeometryPoint

第二条线段的起始节点。

b2 GeometryPoint

第二条线段的结束节点。

Returns:
Type Description
Object 如果相交返回交点,如果不相交返回两条线段的位置关系。

modifyDOMElement(element, id, px, sz, position, border, overflow, opacity)

common/commontypes/Util.js, line 312

修改某 DOM 元素的许多属性。

Name Type Description
element HTMLElement

待修改的 DOM 元素。

id string 可选

DOM 元素的 ID。

px Pixel 可选

DOM 元素的 style 属性的 left 和 top 属性。

sz Size 可选

DOM 元素的 width 和 height 属性。

position string 可选

DOM 元素的 position 属性。

border string 可选

DOM 元素的 style 属性的 border 属性。

overflow string 可选

DOM 元素的 style 属性的 overflow 属性。

opacity number 可选

不透明度值。取值范围:(0.0 - 1.0)。

normalizeScale(scale){number}

common/commontypes/Util.js, line 574

判断并转化比例尺。

Name Type Description
scale number

比例尺。

Returns:
Type Description
number 正常的 scale 值。

rad(x){number}

common/commontypes/Util.js, line 495

角度转弧度。

Name Type Description
x number

角度。

Returns:
Type Description
number 转化后的弧度。

removeItem(array, item){Array}

common/commontypes/Util.js, line 265

从数组中删除某一项。

Name Type Description
array Array

数组。

item Object

数组中要删除的一项。

Returns:
Type Description
Array 执行删除操作后的数组。

reset(obj)

common/commontypes/Util.js, line 196

销毁对象,将其属性置空。

Name Type Description
obj Object 可选

目标对象。

supportCanvas(){boolean}

common/commontypes/Util.js, line 635

判断浏览器是否支持 Canvas。

Returns:
Type Description
boolean 当前浏览器是否支持 HTML5 Canvas 。

toFloat(number, precision){number}

common/commontypes/Util.js, line 479

将字符串以接近的精度转换为数字。

Name Type Default Description
number string

字符串。

precision number 14 可选

精度。

Returns:
Type Description
number 转化后的数字。

toJSON(obj){string}

common/commontypes/Util.js, line 706

将对象转换成 JSON 字符串。

Name Type Description
obj Object

要转换成 JSON 的 Object 对象。

Returns:
Type Description
string 转换后的 JSON 对象。

transformResult(result){Object}

common/commontypes/Util.js, line 865

转换查询结果。

Name Type Description
result Object

查询结果。

Returns:
Type Description
Object 转换后的查询结果。

urlAppend(url, paramStr){string}

common/commontypes/Util.js, line 429

给 URL 追加查询参数。

Name Type Description
url string

待追加参数的 URL 字符串。

paramStr string

待追加的查询参数。

Returns:
Type Description
string 新的 URL。

urlPathAppend(url, paramStr){string}

common/commontypes/Util.js, line 448

给 URL 追加 path 参数。

Name Type Description
url string

待追加参数的 URL 字符串。

paramStr string

待追加的path参数。

Returns:
Type Description
string 新的 URL。