forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLonLat.js
More file actions
188 lines (168 loc) · 6.81 KB
/
Copy pathLonLat.js
File metadata and controls
188 lines (168 loc) · 6.81 KB
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
/* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import {Util} from './Util';
/**
* @class SuperMap.LonLat
* @category BaseTypes Geometry
* @classdesc 这个类用来表示经度和纬度对。
* @param {number} [lon=0.0] - 地图单位上的 X 轴坐标,如果地图是地理投影,则此值是经度,否则,此值是地图地理位置的 x 坐标。
* @param {number} [lat=0.0] - 地图单位上的 Y 轴坐标,如果地图是地理投影,则此值是纬度,否则,此值是地图地理位置的 y 坐标。
* @param {Array.<float>} [location] - 如果要同时设置,则使用传入横纵坐标组成的数组。
* @example
* var lonLat = new SuperMap.LonLat(30,45);
*/
export class LonLat {
constructor(lon, lat) {
if (Util.isArray(lon)) {
lat = lon[1];
lon = lon[0];
}
/**
* @member {float} [SuperMap.LonLat.prototype.lon=0.0]
* @description 地图的单位的 X 轴(横轴)坐标。
*/
this.lon = lon ? Util.toFloat(lon) : 0.0;
/**
* @member {float} [SuperMap.LonLat.prototype.lat=0.0]
* @description 地图的单位的 Y 轴(纵轴)坐标。
*/
this.lat = lat ? Util.toFloat(lat) : 0.0;
this.CLASS_NAME = "SuperMap.LonLat";
}
/**
* @function SuperMap.LonLat.prototype.toString
* @description 返回此对象的字符串形式
* @example
* var lonLat = new SuperMap.LonLat(100,50);
* var str = lonLat.toString();
* @returns {string} 例如: "lon=100,lat=50"
*/
toString() {
return ("lon=" + this.lon + ",lat=" + this.lat);
}
/**
* @function SuperMap.LonLat.prototype.toShortString
* @description 将经度纬度转换成简单字符串。
* @example
* var lonLat = new SuperMap.LonLat(100,50);
* var str = lonLat.toShortString();
* @returns {string} 返回处理后的经纬度字符串。例如:"100,50"
*/
toShortString() {
return (this.lon + "," + this.lat);
}
/**
* @function SuperMap.LonLat.prototype.clone
* @description 复制坐标对象,并返回复制后的新对象。
* @example
* var lonLat1 = new SuperMap.LonLat(100,50);
* var lonLat2 = lonLat1.clone();
* @returns {SuperMap.LonLat} 返回相同坐标值的新的坐标对象。
*/
clone() {
return new LonLat(this.lon, this.lat);
}
/**
* @function SuperMap.LonLat.prototype.add
* @description 在已有坐标对象的经纬度基础上加上新的坐标经纬度,并返回新的坐标对象。
* @example
* var lonLat1 = new SuperMap.LonLat(100,50);
* //lonLat2 是新的对象
* var lonLat2 = lonLat1.add(100,50);
* @param {float} lon - 传入的经度参数。
* @param {float} lat - 传入的纬度参数。
* @returns {SuperMap.LonLat} 返回一个新的 LonLat 对象,此对象的经纬度是由传入的经纬度与当前的经纬度相加所得。
*/
add(lon, lat) {
if ((lon == null) || (lat == null)) {
throw new TypeError('LonLat.add cannot receive null values');
}
return new LonLat(this.lon + Util.toFloat(lon),
this.lat + Util.toFloat(lat));
}
/**
* @function SuperMap.LonLat.prototype.equals
* @description 判断两个坐标对象是否相等。
* @example
* var lonLat1 = new SuperMap.LonLat(100,50);
* var lonLat2 = new SuperMap.LonLat(100,50);
* var isEquals = lonLat1.equals(lonLat2);
* @param {SuperMap.LonLat} ll - 需要进行比较的坐标对象。
* @returns {boolean} 如果LonLat对象的经纬度和传入的经纬度一致则返回true,不一
* 致或传入的ll参数为NULL则返回false。
*/
equals(ll) {
var equals = false;
if (ll != null) {
equals = ((this.lon === ll.lon && this.lat === ll.lat) ||
(isNaN(this.lon) && isNaN(this.lat) && isNaN(ll.lon) && isNaN(ll.lat)));
}
return equals;
}
/**
* @function SuperMap.LonLat.prototype.wrapDateLine
* @description 通过传入的范围对象对坐标对象转换到该范围内。
* 如果经度小于给定范围最小精度,则在原经度基础上加上范围宽度,直到精度在范围内为止,如果经度大于给定范围则在原经度基础上减去范围宽度。
* 即指将不在经度范围内的坐标转换到范围以内(只会转换 lon,不会转换 lat,主要用于转移到日界线以内)。
* @example
* var lonLat1 = new SuperMap.LonLat(420,50);
* var lonLat2 = lonLat1.wrapDateLine(
* new SuperMap.Bounds(-180,-90,180,90)
* );
* @param {SuperMap.Bounds} maxExtent - 最大边界的范围。
* @returns {SuperMap.LonLat} 将坐标转换到范围对象以内,并返回新的坐标。
*/
wrapDateLine(maxExtent) {
var newLonLat = this.clone();
if (maxExtent) {
//shift right?
while (newLonLat.lon < maxExtent.left) {
newLonLat.lon += maxExtent.getWidth();
}
//shift left?
while (newLonLat.lon > maxExtent.right) {
newLonLat.lon -= maxExtent.getWidth();
}
}
return newLonLat;
}
/**
*
* @function SuperMap.LonLat.prototype.destroy
* @description 销毁此对象。
* 销毁后此对象的所有属性为 null,而不是初始值。
* @example
* var lonLat = new SuperMap.LonLat(100,50);
* lonLat.destroy();
*/
destroy() {
this.lon = null;
this.lat = null;
}
/**
* @function SuperMap.LonLat.fromString
* @description 通过字符串生成一个 {@link SuperMap.LonLat} 对象。
* @example
* var str = "100,50";
* var lonLat = SuperMap.LonLat.fromString(str);
* @param {string} str - 字符串的格式:Lon+","+Lat。如:"100,50"。
* @returns {SuperMap.LonLat} 返回一个 {@link SuperMap.LonLat} 对象。
*/
static fromString(str) {
var pair = str.split(",");
return new LonLat(pair[0], pair[1]);
}
/**
* @function SuperMap.LonLat.fromArray
* @description 通过数组生成一个 <SuperMap.LonLat> 对象。
* @param {Array.<float>} arr - 数组的格式,长度只能为2,:[Lon,Lat]。如:[5,-42]。
* @returns {SuperMap.LonLat} 返回一个 <SuperMap.LonLat> 对象。
*/
static fromArray(arr) {
var gotArr = Util.isArray(arr),
lon = gotArr && arr[0],
lat = gotArr && arr[1];
return new LonLat(lon, lat);
}
}