tpanoramaSetting.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. 'use strict';
  2. var _setContainer;
  3. var _hideImgId = "hideimgid825";
  4. var _himg;
  5. var _cvId = 'cv825';
  6. var _cv;
  7. var _infoId = 'info825';
  8. var _info;
  9. var _lable = [];
  10. var count = 1;
  11. var setOpt = {
  12. container: 'myDiv', //setting容器
  13. imgUrl: 'resources/img/panorama/3.jpg',
  14. width: '', //指定宽度,高度自适应
  15. showGrid: true, //是否显示格网
  16. showPosition: true, //是否显示经纬度提示
  17. lableColor: '#9400D3', //标记颜色
  18. gridColor: '#48D1CC', //格网颜色
  19. lables: [], //标记 {lon:114,lat:38,text:'标记一'}
  20. addLable: true, //开启后双击添加标记 (必须开启经纬度提示)
  21. getLable: true, //开启后右键查询标记 (必须开启经纬度提示)
  22. deleteLbale: true //开启默认中键删除 (必须开启经纬度提示)
  23. };
  24. function panoramaSetting(opt) {
  25. this.config(opt);
  26. }
  27. panoramaSetting.prototype = {
  28. constructor: undefined,
  29. def: {},
  30. config: function config(opt) {
  31. this.def = extend(setOpt, opt, true);
  32. },
  33. init: function init() {
  34. var that = this;
  35. _lable = this.def.lables;
  36. initSetContainer(this.def.container, this.def.imgUrl);
  37. setTimeout(function () {
  38. adptpImg(that.def.width, that.def.imgUrl);
  39. clearCanvas();
  40. if (that.def.showGrid) {
  41. initGrid(that.def.gridColor);
  42. }
  43. if (that.def.showPosition) {
  44. initCursor();
  45. }
  46. initLables(that.def.lables, that.def.lableColor);
  47. var then = that;
  48. if (count == 2) {
  49. if (that.def.addLable) {
  50. _info.addEventListener("dblclick", function (e) {
  51. var text = prompt("标记名称");
  52. if (text) {
  53. addMark(e, then.def.lableColor, text);
  54. }
  55. });
  56. }
  57. if (that.def.getLable) {
  58. document.oncontextmenu = function (e) {
  59. e.preventDefault();
  60. };
  61. _info.addEventListener("mousedown", function (e) {
  62. if (e.button == 2) {
  63. var p = selectLable1(e);
  64. if (p.lon) {
  65. alert("经度" + p.lon + ",纬度" + p.lat + ",名称" + p.text);
  66. }
  67. }
  68. });
  69. }
  70. if (that.def.deleteLbale) {
  71. _info.addEventListener("mousedown", function (e) {
  72. if (e.button == 1) {
  73. var p = selectLable1(e);
  74. if (p.lon) {
  75. var c = confirm("您确认要删除该标记吗?");
  76. if (c) {
  77. removeByValue(_lable, p);
  78. that.clean();
  79. that.init();
  80. }
  81. }
  82. }
  83. });
  84. }
  85. }
  86. }, 100);
  87. count++;
  88. },
  89. getAllLables: function getAllLables() {
  90. return _lable;
  91. },
  92. addLable: function addLable(e, text) {
  93. var position = addMark(e, this.def.lableColor, text);
  94. },
  95. getLable: function getLable(e) {
  96. return selectLable1(e);
  97. },
  98. listen: function listen(type, fun) {
  99. _info.addEventListener(type, function (e) {
  100. fun(e);
  101. });
  102. },
  103. delete: function _delete(p) {
  104. if (p.lon) {
  105. removeByValue(_lable, p);
  106. }
  107. },
  108. clean: function clean() {
  109. document.onmousemove = function () {};
  110. document.getElementById(this.def.container).innerHTML = '';
  111. }
  112. };
  113. function initSetContainer(c, url) {
  114. _setContainer = document.getElementById(c);
  115. _himg = document.getElementById(_hideImgId);
  116. if (_himg != null) {
  117. document.body.removeChild(_himg);
  118. }
  119. _himg = document.createElement('img');
  120. _himg.style.visibility = 'hidden';
  121. _himg.id = _hideImgId;
  122. _himg.src = url;
  123. _cv = document.getElementById(_cvId);
  124. if (_cv != null) {
  125. _setContainer.removeChild(_cv);
  126. }
  127. _cv = document.createElement('canvas');
  128. _setContainer.appendChild(_cv);
  129. _cv.id = _cvId;
  130. _info = document.getElementById(_infoId);
  131. if (_info != null) {
  132. document.body.removeChild(_info);
  133. } else {
  134. _info = document.createElement('div');
  135. }
  136. _info.id = _infoId;
  137. _info.style.height = "40px";
  138. _info.style.width = "110px";
  139. _info.style.backgroundColor = "#3C8DBC";
  140. _info.style.display = "none";
  141. _info.style.position = "absolute";
  142. _info.style.filter = "alpha(Opacity=80)";
  143. _info.style.mozOpacity = 0.5;
  144. _info.style.opacity = 0.8;
  145. _info.style.fontFamily = "楷体";
  146. _info.style.fontWeight = "bold";
  147. _info.style.textShadow = "0 0 0.2em #fffd84";
  148. _info.style.textAlign = "center";
  149. document.body.appendChild(_info);
  150. }
  151. function adptpImg(width, url) {
  152. if (width) {
  153. _setContainer.style.width = width;
  154. }
  155. _setContainer.style.backgroundImage = '';
  156. var naturalHeight = _himg.naturalHeight;
  157. var naturalWidth = _himg.naturalWidth;
  158. var scale = naturalHeight / naturalWidth;
  159. var height = scale * _setContainer.style.width.split("px")[0];
  160. _setContainer.style.height = height + "px";
  161. setTimeout(function () {
  162. _setContainer.style.backgroundRepeat = 'no-repeat';
  163. _setContainer.style.backgroundPosition = '0% 0%';
  164. _setContainer.style.backgroundSize = 'cover';
  165. _setContainer.style.backgroundImage = "url(" + url + ")";
  166. }, 100);
  167. }
  168. function initGrid(color) {
  169. _cv.width = _setContainer.style.width.split("px")[0];
  170. _cv.height = _setContainer.style.height.split("px")[0];
  171. if (_cv.getContext) {
  172. var ctx = _cv.getContext("2d"),
  173. width = _cv.width,
  174. height = _cv.height;
  175. ctx.strokeStyle = color;
  176. for (var i = 1; i < 19; i++) {
  177. if (i == 9) {
  178. ctx.lineWidth = 3;
  179. } else {
  180. ctx.lineWidth = 0.8;
  181. }
  182. ctx.beginPath();
  183. ctx.moveTo(0, i * height / 18);
  184. ctx.lineTo(width, i * height / 18);
  185. ctx.stroke();
  186. }
  187. for (var j = 1; j < 37; j++) {
  188. if (j == 18) {
  189. ctx.lineWidth = 3;
  190. } else {
  191. ctx.lineWidth = 0.8;
  192. }
  193. ctx.beginPath();
  194. ctx.moveTo(j * width / 36, 0);
  195. ctx.lineTo(j * width / 36, height);
  196. ctx.stroke();
  197. }
  198. }
  199. }
  200. function clearCanvas() {
  201. var ctx = _cv.getContext("2d");
  202. var h = _setContainer.height;
  203. var w = _setContainer.width;
  204. ctx.clearRect(0, 0, w, h);
  205. }
  206. function initCursor() {
  207. var minX = _setContainer.offsetLeft;
  208. var maxX = minX + _setContainer.style.width.split("px")[0];
  209. var minY = _setContainer.offsetTop;
  210. var maxY = minY + _setContainer.style.height.split("px")[0];
  211. document.onmousemove = function (ev) {
  212. var oEvent = ev || event;
  213. var pos = getXY(oEvent);
  214. if (pos.x < maxX && pos.x > minX && pos.y < maxY && pos.y > minY) {
  215. _info.style.display = "block";
  216. _info.style.left = pos.x + "px";
  217. _info.style.top = pos.y + "px";
  218. updateInfoDiv(ev);
  219. } else {
  220. _info.style.display = "none";
  221. }
  222. };
  223. }
  224. function getXY(eve) {
  225. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  226. var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
  227. return { x: scrollLeft + eve.clientX, y: scrollTop + eve.clientY };
  228. }
  229. function updateInfoDiv(e) {
  230. var position = calLonLat(e);
  231. var html = "经度:" + position.lon + "</br>" + "纬度:" + position.lat;
  232. _info.innerHTML = html;
  233. }
  234. function calLonLat(e) {
  235. var h = _setContainer.style.height.split("px")[0];
  236. var w = _setContainer.style.width.split("px")[0];
  237. var ix = _setContainer.offsetLeft;
  238. var iy = _setContainer.offsetTop;
  239. iy = iy + h;
  240. var x = e.clientX;
  241. var y = e.clientY;
  242. var lonS = (x - ix) / w;
  243. var lon = 0;
  244. if (lonS > 0.5) {
  245. lon = -(1 - lonS) * 360;
  246. } else {
  247. lon = 1 * 360 * lonS;
  248. }
  249. var latS = (iy - y) / h;
  250. var lat = 0;
  251. if (latS > 0.5) {
  252. lat = (latS - 0.5) * 180;
  253. } else {
  254. lat = (0.5 - latS) * 180 * -1;
  255. }
  256. lon = lon.toFixed(2);
  257. lat = lat.toFixed(2);
  258. return { lon: lon, lat: lat };
  259. }
  260. function initLables(arr, color) {
  261. for (var i in arr) {
  262. var p = arr[i];
  263. var m = getXYByLonLat(p.lon, p.lat);
  264. drawCircle(m.x, m.y);
  265. drawText(m.x, m.y, p.text, color);
  266. }
  267. }
  268. function drawText(x, y, txt, lableColor) {
  269. var canvas = _cv;
  270. var ctx = canvas.getContext("2d");
  271. ctx.font = "bold 20px 楷体";
  272. ctx.fillStyle = lableColor;
  273. ctx.fillText(txt, x, y);
  274. }
  275. function drawCircle(x, y) {
  276. var canvas = _cv;
  277. var ctx = canvas.getContext("2d");
  278. ctx.fillStyle = "#0000ff";
  279. ctx.beginPath();
  280. ctx.arc(x, y, 5, 0, 2 * Math.PI, true);
  281. ctx.closePath();
  282. ctx.stroke();
  283. ctx.fill();
  284. ctx.fillStyle = "#ff0000";
  285. ctx.beginPath();
  286. ctx.arc(x, y, 2, 0, 2 * Math.PI, true);
  287. ctx.closePath();
  288. ctx.fill();
  289. }
  290. function getXYByLonLat(lon, lat) {
  291. var x = 0;
  292. var y = 0;
  293. var h = _setContainer.style.height.split("px")[0];
  294. var w = _setContainer.style.width.split("px")[0];
  295. if (lon > 0) {
  296. x = 1 * lon / 180 * 0.5 * w;
  297. } else {
  298. x = (1 + lon / 180) * 0.5 * w + 0.5 * w;
  299. }
  300. if (lat > 0) {
  301. y = (1 - lat / 90) * h * 0.5;
  302. } else {
  303. y = -1 * lat / 90 * 0.5 * h + 0.5 * h;
  304. }
  305. return { x: x, y: y };
  306. }
  307. function addMark(e, color, text) {
  308. var pos = getXY(e);
  309. var iX = _setContainer.offsetLeft;
  310. var iY = _setContainer.offsetTop;
  311. var x = pos.x - iX;
  312. var y = pos.y - iY;
  313. drawCircle(x, y);
  314. drawText(x, y, text, color);
  315. var ll = calLonLat(e);
  316. var l = { lon: ll.lon, lat: ll.lat, text: text };
  317. _lable.push(l);
  318. return l;
  319. }
  320. function selectLable1(e) {
  321. var flag = false;
  322. var p;
  323. for (var i = 0; i < _lable.length; i++) {
  324. p = _lable[i];
  325. var m = getXYByLonLat(p.lon, p.lat);
  326. var iX = _setContainer.offsetLeft;
  327. var iY = _setContainer.offsetTop;
  328. var screenX = e.clientX;
  329. var screenY = e.clientY;
  330. var x = screenX - iX;
  331. var y = screenY - iY;
  332. var cx = x - m.x;
  333. var cy = y - m.y;
  334. var distence = Math.sqrt(cx * cx + cy * cy);
  335. if (distence <= 5) {
  336. flag = true;
  337. break;
  338. }
  339. }
  340. if (flag) {
  341. return p;
  342. } else {
  343. return {};
  344. }
  345. }
  346. function removeByValue(arr, val) {
  347. for (var i = 0; i < arr.length; i++) {
  348. if (arr[i].lon == val.lon && arr[i].lat == val.lat) {
  349. arr.splice(i, 1);
  350. break;
  351. }
  352. }
  353. }
  354. module.exports = panoramaSetting;