Common.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. define("gmap/Common",["gmap/SymbolMgr",
  2. "esri/SpatialReference", "esri/geometry/Extent", "esri/geometry/Point", "esri/geometry/ScreenPoint",
  3. "esri/graphic",
  4. "esri/tasks/QueryTask", "esri/tasks/query",
  5. "dojo/_base/declare", "dojo/io/script", "dojo/_base/lang"],
  6. function (SymbolMgr,
  7. SpatialReference, Extent, Point, ScreenPoint,
  8. Graphic,
  9. QueryTask, Query,
  10. declare, script, lang) {
  11. return declare(null, {
  12. constructor: function (map, mapload) {
  13. this._map = map;
  14. this._mapload = mapload;
  15. //缩放到矩形
  16. this._map.ZoomToExtent = lang.hitch(this, function (xmin, ymin, xmax, ymax, zoomfactor) {
  17. this._ZoomToExtent(xmin, ymin, xmax, ymax, zoomfactor);
  18. });
  19. //缩放到级别
  20. this._map.ZoomToLevel = lang.hitch(this, function (level, lon, lat) {
  21. this._ZoomToLevel(level, lon, lat);
  22. });
  23. //添加临时用户数据
  24. this._map.AddGraphic = lang.hitch(this, function (gra) {
  25. this._AddGraphic(gra);
  26. });
  27. //移除临时用户数据
  28. this._map.RemoveGraphic = lang.hitch(this, function (gra) {
  29. this._RemoveGraphic(gra);
  30. });
  31. //清空临时用户数据
  32. this._map.ClearGraphic = lang.hitch(this, function () {
  33. this._ClearGraphic();
  34. });
  35. //缩放至Geometry
  36. this._map.ZoomToGeometry = lang.hitch(this, function (geometry, layerinfo) {
  37. this._ZoomToGeometry(geometry, layerinfo);
  38. });
  39. this._map.MapToScreen = lang.hitch(this, function (x, y) {
  40. var point = new Point(x, y, new SpatialReference({ wkid: this._map.WGS84WKID }));
  41. var mapPoint = this._map.ToMapSR(point);
  42. return this._map.MainMap.toScreen(mapPoint);
  43. });
  44. this._map.SetFullExtent = lang.hitch(this, function (xmin, ymin, xmax, ymax) {
  45. this._FullExtent = new Extent(xmin, ymin, xmax, ymax, new SpatialReference({ wkid: this._map.WGS84WKID }));
  46. });
  47. this._map.ScreenToMap = lang.hitch(this, function (x, y) {
  48. var screenPoint = new ScreenPoint(x, y);
  49. var point = this._map.MainMap.toMap(screenPoint);
  50. return this._map.ToWGS84(point);
  51. });
  52. this._map.Hilight = lang.hitch(this, this._Hilight);
  53. this._map.ClearHilight = lang.hitch(this, this._ClearHilight);
  54. this._map._getScaleLevel = function (scale) {
  55. for (var i = 0, c = this.Lods.length; i < c; i++) {
  56. if (scale >= this.Lods[i].scale) {
  57. return this.Lods[i].level;
  58. }
  59. }
  60. return this.Lods[this.Lods.length - 1].level;
  61. };
  62. this._map._getLevelScale = function (level) {
  63. for (var i = 0, c = this.Lods.length; i < c; i++) {
  64. if (level >= this.Lods[i].level) {
  65. return this.Lods[i].scale;
  66. }
  67. }
  68. return this.Lods[this.Lods.length - 1].scale;
  69. };
  70. this._map.GetScale = lang.hitch(this, function () {
  71. return this._map.MainMap.getScale();
  72. });
  73. this._map.GetMinScale = lang.hitch(this, function () {
  74. return this._map.MainMap.getMinScale();
  75. });
  76. this._map.GetMaxScale = lang.hitch(this, function () {
  77. return this._map.MainMap.getMaxScale();
  78. });
  79. this._map.GetVisible = lang.hitch(this, function () {
  80. return this._map.MainMap.visible;
  81. });
  82. // 获取定位级别
  83. this._map._getShowScale = function (layerinfo, checkLabel) {
  84. if (checkLabel == null) checkLabel = false;
  85. if (checkLabel != true && checkLabel != false) checkLabel = false;
  86. var minScale = 0;
  87. var maxScale = 0;
  88. if (layerinfo.minScale != 0) {
  89. if (minScale == 0 || layerinfo.minScale < minScale) minScale = layerinfo.minScale;
  90. }
  91. if (layerinfo.maxScale != 0) {
  92. if (maxScale == 0 || layerinfo.maxScale > maxScale) maxScale = layerinfo.maxScale;
  93. }
  94. if (layerinfo.drawingInfo.labelingInfo && layerinfo.drawingInfo.labelingInfo.length > 0 && layerinfo.hasLabels == true && checkLabel == true) {
  95. for (var i = 0; i < layerinfo.drawingInfo.labelingInfo.length; i++) {
  96. var lblInfo = layerinfo.drawingInfo.labelingInfo[i];
  97. if (lblInfo.minScale != 0) {
  98. if (minScale == 0 || lblInfo.minScale < minScale) minScale = lblInfo.minScale;
  99. }
  100. if (lblInfo.maxScale != 0) {
  101. if (maxScale == 0 || lblInfo.maxScale > maxScale) maxScale = lblInfo.maxScale;
  102. }
  103. }
  104. }
  105. if (layerinfo.effectiveMinScale != null && layerinfo.effectiveMinScale != 0) {
  106. if (minScale == 0 || layerinfo.effectiveMinScale < minScale) minScale = layerinfo.effectiveMinScale;
  107. }
  108. if (layerinfo.effectiveMaxScale != null && layerinfo.effectiveMaxScale != 0) {
  109. if (maxScale == 0 || layerinfo.effectiveMaxScale > maxScale) maxScale = layerinfo.effectiveMaxScale;
  110. }
  111. return { maxScale: maxScale, minScale: minScale };
  112. }
  113. },
  114. _ZoomToGeometry: function (geometry, layerinfo) {
  115. var mapGeo = this._map.ToMapSR(geometry);
  116. var gcsGeo = this._map.ToWGS84(geometry);
  117. var geoWidthLevel = null;
  118. var geoHeightLevel = null;
  119. if (mapGeo.type == "polyline" || mapGeo.type == "polygon") {
  120. var extent = mapGeo.getExtent();
  121. var width = extent.xmax - extent.xmin;
  122. var resWidth = width / this._map.MainMap.width;
  123. geoWidthLevel = this._map.Lods[this._map.Lods.length - 1].level;
  124. for (var i = 0; i < this._map.Lods.length - 1; i++) {
  125. var lod1 = this._map.Lods[i];
  126. var lod2 = this._map.Lods[i + 1];
  127. if (resWidth < lod1.resolution && resWidth > lod2.resolution) {
  128. geoWidthLevel = lod1.level;
  129. break;
  130. }
  131. }
  132. var height = extent.ymax - extent.ymin;
  133. var resHeight = height / this._map.MainMap.height;
  134. geoHeightLevel = this._map.Lods[this._map.Lods.length - 1].level;
  135. for (var i = 0; i < this._map.Lods.length - 1; i++) {
  136. var lod1 = this._map.Lods[i];
  137. var lod2 = this._map.Lods[i + 1];
  138. if (resHeight < lod1.resolution && resHeight > lod2.resolution) {
  139. geoHeightLevel = lod1.level;
  140. break;
  141. }
  142. }
  143. }
  144. var geoLevel = (geoWidthLevel == null || geoHeightLevel == null) ?
  145. null : (geoHeightLevel < geoWidthLevel ? geoHeightLevel : geoWidthLevel);
  146. var level = this._getLocateLevel(layerinfo, geoLevel);
  147. var point = this._getPoint(gcsGeo);
  148. this._map.ZoomToLevel(level, point.x, point.y);
  149. },
  150. // 获取中心点
  151. _getPoint: function (geometry) {
  152. switch (geometry.type) {
  153. case "point":
  154. return geometry;
  155. case "extent":
  156. return geometry.getCenter();
  157. default:
  158. return geometry.getExtent().getCenter();
  159. }
  160. },
  161. // 获取定位级别
  162. _getLocateLevel: function (layerinfo, geoLevel) {
  163. var minLevel = this._map.MinLevel;
  164. var maxLevel = this._map.MaxLevel;
  165. var scaleInfo = this._map._getShowScale(layerinfo, true);
  166. if (scaleInfo.minScale != 0) {
  167. var level = this._map._getScaleLevel(scaleInfo.minScale);
  168. if (level > minLevel) minLevel = level;
  169. }
  170. if (scaleInfo.maxScale != 0) {
  171. var level = this._map._getScaleLevel(scaleInfo.maxScale) - 1;
  172. if (level < maxLevel) maxLevel = level;
  173. }
  174. if (geoLevel == null) {
  175. return Math.ceil((minLevel + maxLevel) / 2);//这种情况只有点
  176. }
  177. else {
  178. var level = geoLevel; //获取当前级别
  179. if (level > maxLevel) {
  180. return maxLevel;
  181. }
  182. else if (level < minLevel) {
  183. return minLevel;
  184. }
  185. else {
  186. return level;
  187. }
  188. }
  189. },
  190. _Hilight: function (geo) {
  191. geo = this._map.ToMapSR(geo);
  192. var gra = new Graphic();
  193. gra.geometry = geo;
  194. if (geo.type == "point") {
  195. gra.setSymbol(SymbolMgr.GetHilightPointCircleSymbol());
  196. }
  197. else if (geo.type == "polyline") {
  198. gra.setSymbol(SymbolMgr.GetHilightLineSymbol());
  199. }
  200. else if (geo.type == "polygon" || geo.type == "extent") {
  201. gra.setSymbol(SymbolMgr.GetHilightFillSymbol());
  202. }
  203. this._mapload.DrawHilightLayer.add(gra);
  204. },
  205. _ClearHilight: function () {
  206. this._mapload.DrawHilightLayer.clear();
  207. },
  208. //缩放到矩形
  209. _ZoomToExtent: function (xmin, ymin, xmax, ymax, zoomfactor) {
  210. var fromPt = this._map.ToMapSR(new Point(xmin, ymin, new SpatialReference({ wkid: this._map.WGS84WKID })));
  211. var toPt = this._map.ToMapSR(new Point(xmax, ymax, new SpatialReference({ wkid: this._map.WGS84WKID })));
  212. var extent = new Extent(fromPt.x, fromPt.y, toPt.x, toPt.y, fromPt.spatialReference);
  213. if (zoomfactor != null) extent = extent.expand(zoomfactor);
  214. this._map.MainMap.setExtent(extent);
  215. },
  216. //缩放到级别
  217. _ZoomToLevel: function (level, lon, lat) {
  218. var point = this._map.ToMapSR(new Point(lon, lat, new SpatialReference({ wkid: this._map.WGS84WKID })));
  219. this._map.MainMap.centerAndZoom(point, level - this._map.MinLevel);
  220. },
  221. //添加临时用户数据
  222. _AddGraphic: function (gra) {
  223. if (gra && gra.geometry && gra.geometry.type) {
  224. gra.geometry = this._map.ToWGS84(gra.geometry);
  225. if (gra.geometry.type == "point") {
  226. this._mapload.Temp_PointLayer.add(gra);
  227. return true;
  228. }
  229. else if (gra.geometry.type == "polyline") {
  230. this._mapload.Temp_LineLayer.add(gra);
  231. return true;
  232. }
  233. else if (gra.geometry.type == "polygon") {
  234. this._mapload.Temp_AreaLayer.add(gra);
  235. return true;
  236. }
  237. }
  238. return false;
  239. },
  240. //移除临时用户数据
  241. _RemoveGraphic: function (gra) {
  242. if (gra && gra.geometry && gra.geometry.type) {
  243. if (gra.geometry.type == "point") {
  244. this._mapload.Temp_PointLayer.remove(gra);
  245. return true;
  246. }
  247. else if (gra.geometry.type == "polyline") {
  248. this._mapload.Temp_LineLayer.remove(gra);
  249. return true;
  250. }
  251. else if (gra.geometry.type == "polygon") {
  252. this._mapload.Temp_AreaLayer.remove(gra);
  253. return true;
  254. }
  255. }
  256. return false;
  257. },
  258. //清空临时用户数据
  259. _ClearGraphic: function () {
  260. this._mapload.Temp_PointLayer.clear();
  261. this._mapload.Temp_LineLayer.clear();
  262. this._mapload.Temp_AreaLayer.clear();
  263. }
  264. });
  265. });