Summary
Methods:
fromArray |
用数组来构造坐标对象。 |
fromString |
用字符串构造坐标。 |
addOffset |
对当前坐标进行偏移处理。当前坐标被处理后会发生变化。 |
clone |
克隆当前坐标对象。 |
destroy |
销毁坐标对象 |
equals |
判断两个坐标是否相等,返回true则表示相等,反之表示不相等。 |
format |
用字符串模板格式化坐标。 |
get2Dist |
计算当前坐标与传入坐标,地理坐标系或投影坐标系下的球面距离(单位为米)。地理坐标系距离采用haversine大圆最短距离计算公式,半径采用指定坐标系的参考椭球体的平均地球半径(1/3 * (2a + b)),计算结果的最大误差为0.3%。 |
get3Dist |
计算当前坐标与传入坐标间的三维距离。 |
getDimension |
获取坐标维度。默认返回3,表示三维坐标。 |
getOrdinate |
获取坐标分量。坐标分量指的是坐标对在各坐标轴上对应的值。 |
isValid |
验证坐标在指定坐标系的限制下是否有效。 |
minus |
计算两个坐标的差值,当前坐标减去入参坐标。 |
rotate |
绕着指定直线(旋转轴)旋转,所指定的直线穿过给定点且平行于坐标轴。旋转后的坐标可能发生变化。绕轴旋转的本质如下: 绕平行于Z轴的直线旋转即是在XY平面内旋转;绕平行于X轴的直线旋转即是在YZ平面内旋转;绕平行于Y轴的直线旋转即是在XZ平面内旋转 |
scale |
缩放坐标。即在坐标的各分量上,分別以指定的倍数缩放。 |
setOrdinate |
修改坐标分量 |
to2DString |
将坐标值的XY转换为对应的字符串 |
toArray |
将坐标值转换为数组 |
toFixed |
精确一个坐标中每个分量的小数点后的位数。小数位数支持0~14,建议最多保留6位小数。在Chrome浏览器的不同版本中进位、舍位规则不一致,导致结果可能不一致。 |
toString |
将坐标值转换为字符串 |
Constructor
new Coordinate(x, y, z, zDegreee)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
x |
Number | 0 | 经线分量 |
y |
Number | 0 | 纬线分量 |
z |
Number | 0 | 高程分量 |
zDegreee |
Boolean | true | 高程分量是否为度,当使用地理坐标系:比如4326等时,该参数有效,缺省true:高程单位和坐标单位统一,如果设为false,那么我们认为高程单位为米。 |
Throws:
当坐标分量参数不是数字时,抛出异常
Example
let coord = new hmap.basetype.Coordinate(120,30,0);
Methods
-
static fromArray(coordArr) → {module:basetype~Coordinate}
-
用数组来构造坐标对象。
Parameters:
Name Type Description coordArr
Array.<Number> 含1-3个数字值组成的数组,依次对应坐标对象的x、y、z三个坐标分量。数组长度不足时,自动用0补全。
Example
let coord = hmap.basetype.Coordinate.fromArray([120.2,30,0]);
-
static fromString(coordStr) → {module:basetype~Coordinate}
-
用字符串构造坐标。
Parameters:
Name Type Description coordStr
String 1-3个数字用逗号分隔构成的字符串,每个数值依次对应坐标对象x,y,z三个坐标分量,当长度不足时,自动用0 补全。
Example
let str1 = "120,20,30,123,12"; let coord= hmap.basetype.Coordinate.fromString(str1);//输出结果:coord=(120,20,30) let ste2 = "120"; let coord= hmap.basetype.Coordinate.fromString(str2);//输出结果:coord=(120,0,0)
-
addOffset(offset) → {module:basetype~Coordinate}
-
对当前坐标进行偏移处理。当前坐标被处理后会发生变化。
Parameters:
Name Type Description offset
module:basetype~Offset 偏移量。
Throws:
当入参不是module:basetype~Offset时,抛出异常
Example
let offset = new hmap.basetype.Offset(2,1,0); let result = coord.addOffset(offset); //result=new hmap.basetype.Coordinate(122,31,0)
-
clone() → {module:basetype~Coordinate}
-
克隆当前坐标对象。
Example
let coord =new hmap.basetype.Coordinate(120,30,0) let result = coord.clone();
-
destroy()
-
销毁坐标对象
Example
coord.destroy();
-
equals(coord) → {Boolean}
-
判断两个坐标是否相等,返回true则表示相等,反之表示不相等。
Parameters:
Name Type Description coord
module:basetype~Coordinate 地理坐标
Throws:
当入参不是坐标对象类型时,抛出异常
Returns:
Boolean -相等返回true;不相等返回false
Example
let coord1 = new hmap.basetype.Coordinate(120,30,0); let coord2 = new hmap.basetype.Coordinate(121,30,0); let result = coord1.equals(coord2);//result=false
-
format(template, precision) → {String|module:basetype~Coordinate}
-
用字符串模板格式化坐标。
Parameters:
Name Type Default Description template
String 字符串模板。模板中的{x}将被替换成真实的经线分量;{y}将被替换成真实纬线分量;{z}将被替换成真实高程分量。
precision
Number 1 坐标精度,1-13的整数,不指定,默认为1,精确到小数点后一位。
Example
let originCoord = new hmap.basetype.Coordinate(120.325,30.589); let template = '模版字符串是 ({x},{y}) '; let out = originCoord.format(template, 2);//输出结果: '模版字符串是 (120.33,30.59) '
-
get2Dist(coord, srid) → {Number}
-
计算当前坐标与传入坐标,地理坐标系或投影坐标系下的球面距离(单位为米)。地理坐标系距离采用haversine大圆最短距离计算公式,半径采用指定坐标系的参考椭球体的平均地球半径(1/3 * (2a + b)),计算结果的最大误差为0.3%。
Parameters:
Name Type Default Description coord
module:basetype~Coordinate 同一坐标系下的目标坐标
srid
Number 4326 坐标系标识。可用选项为4326、4490(即CGCS2000)、3857和900913,默认为4326。
Throws:
当第一个参数不是坐标对象时,抛出异常
Returns:
Number -二维坐标距离(单位:米)
Example
let coord1 = new hmap.basetype.Coordinate(120,30,0); let coord2 = new hmap.basetype.Coordinate(180,40,0); let result = coord1.get2Dist(coord2, 4326); //result=5473489.163137614
-
get3Dist(coord, srid) → {Number}
-
计算当前坐标与传入坐标间的三维距离。
两点在地面的投影距离和两点的高度差(高度方向上经纬度和米的转换按照赤道位置圆来换算)进行勾股定理计算后的距离。Parameters:
Name Type Default Description coord
module:basetype~Coordinate 同一坐标系下的目标坐标
srid
Number | String 4326 坐标系标识,可选项为4326(WGS84)和3857(WGS84 Web Mercator)。
Throws:
当第一个参数不是坐标对象时,抛出异常
Returns:
Number -三维坐标距离,单位是米
Example
let coord1 = new hmap.basetype.Coordinate(120,30,0); let coord2 = new hmap.basetype.Coordinate(180,40,0); let result = coord1.get3Dist(coord2);//result=5473489.163137614
-
getDimension() → {Number}
-
获取坐标维度。默认返回3,表示三维坐标。
Returns:
Number -坐标点的维度
Example
let coord = new hmap.basetype.Coordinate(120,30,0); let num = coord.getDimension();
-
getOrdinate(axis) → {Number}
-
获取坐标分量。坐标分量指的是坐标对在各坐标轴上对应的值。
Parameters:
Name Type Description axis
String 坐标轴,大写字母。"X":经线坐标轴,"Y":纬线坐标轴,"Z":高程坐标轴。
Throws:
当参数不是"X"或"Y"或"Z"时,抛出异常
Returns:
Number -指定坐标分量的值
Example
let coord = new hmap.basetype.Coordinate(120,30,0); let x = coord.getOrdinate("X"); let y = coord.getOrdinate("Y"); let z = coord.getOrdinate("Z"); //result:x=120;y=30;z=0
-
isValid(crs) → {Boolean}
-
验证坐标在指定坐标系的限制下是否有效。
Parameters:
Name Type Description crs
module:proj~Crs 4326坐标系范围x:[-180,180],y:[-90,90]。900913坐标系范围x:[-20037508.342789,20037508.342789],y:[-20037508.342789,20037508.342789]
Returns:
Boolean -true表示在当前坐标系最大的范围内,坐标有效;false表示超过当前坐标系最大的范围,坐标无效。坐标系不被支持时抛出异常。
Example
let isValid = coord.isValid(new hmap.proj.Crs('4326'));
-
minus(coord) → {module:basetype~Offset}
-
计算两个坐标的差值,当前坐标减去入参坐标。
Parameters:
Name Type Description coord
module:basetype~Coordinate 坐标。
Throws:
当参数不是坐标对象时,抛出异常
Example
let coord1 = new hmap.basetype.Coordinate(120,30,0); let coord2 = new hmap.basetype.Coordinate(122,30,0); let result = coord1.minus(coord2);
-
rotate(axis, angle, center) → {module:basetype~Coordinate}
-
绕着指定直线(旋转轴)旋转,所指定的直线穿过给定点且平行于坐标轴。旋转后的坐标可能发生变化。绕轴旋转的本质如下: 绕平行于Z轴的直线旋转即是在XY平面内旋转;绕平行于X轴的直线旋转即是在YZ平面内旋转;绕平行于Y轴的直线旋转即是在XZ平面内旋转
Parameters:
Name Type Default Description axis
String Z 坐标轴,旋转轴平行于该坐标轴并与坐标轴同向。可选值为"X","Y","Z",缺省'Z'.
angle
Number 0 旋转的角度,超过360度时,取angle/360的余数。逆时针为正,顺时针为负
center
module:basetype~Coordinate 旋转轴所穿过的点,可选项。该参数为空时,绕坐标轴旋转
Throws:
参数类型不对时,都会抛出异常
Example
let coord = new hmap.basetype.Coordinate(120,30,0); let center = new hmap.basetype.Coordinate(122,32,0); let result = coord.rotate("Z", 180, center);
-
scale(scaleX, scaleY, scaleZ) → {module:basetype~Coordinate}
-
缩放坐标。即在坐标的各分量上,分別以指定的倍数缩放。
Parameters:
Name Type Default Description scaleX
Number 1 经线分量缩放倍数,如果0<scaleX<1,为缩小;若scaleX>1,则放大
scaleY
Number 1 纬线分量缩放倍数,如果0<scaleY<1,为缩小;若scaleY>1,则放大
scaleZ
Number 1 高程分量缩放倍数,如果0<scaleZ<1,为缩小;若scaleZ>1,则放大
Throws:
当参数不是数值类型或者是小于等于0的数值类型时,抛出异常
Example
let result = coord.scale(2, 2, 2); //result=new hmap.basetype.Coordinate(240, 60, 0)
-
setOrdinate(axis, val) → {module:basetype~Coordinate}
-
修改坐标分量
Parameters:
Name Type Default Description axis
String X 坐标轴标识,"X"——经线坐标轴,"Y"——纬线坐标轴,"Z"——高程坐标轴。
val
Number 坐标分量
Throws:
第一个参数必须为大写的'X'、'Y'、'Z',否则抛出异常;第二个参数不是数值类型,抛出异常
Example
let coord = new hmap.basetype.Coordinate(120,30,0); coord.setOrdinate('X',100); coord.setOrdinate('Y',40); coord.setOrdinate('Z',10); //coord修改后的坐标分量值为(100,40,10)
-
to2DString() → {String}
-
将坐标值的XY转换为对应的字符串
Returns:
String -字符串形式的坐标值
Example
let result = coord.to2DString();
-
toArray() → {Array.<Number>}
-
将坐标值转换为数组
Returns:
Array.<Number> -数组形式的坐标值,顺序依次为经线分量X、纬线分量Y和高程分量Z。
-
toFixed(precision) → {module:basetype~Coordinate}
-
精确一个坐标中每个分量的小数点后的位数。小数位数支持0~14,建议最多保留6位小数。在Chrome浏览器的不同版本中进位、舍位规则不一致,导致结果可能不一致。
Parameters:
Name Type Description precision
Number 小数位数,一个包括0在内的正整数。参数非法时,坐标不会被精确。
Example
let result = coord.toFixed(6);
-
toString() → {String}
-
将坐标值转换为字符串
Returns:
String -字符串形式的坐标值
Example
let result = coord.toString();