tileLayer.wmts.poi.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. L.TileLayer.WMTS.POI = L.TileLayer.WMTS.extend({
  2. options: {
  3. showTitle: true,
  4. showLabellevel:14,
  5. tileSize: 256,
  6. loadShape:false
  7. },
  8. markerLayerGroup: null,
  9. markerPopupGroup: null,
  10. clickHander: null,
  11. shapeGroup:null,
  12. mouseoverObj:null,
  13. shapeIds:{},
  14. initialize: function (url, jsonUrl, options, markerClick) { // (String, Object)
  15. this._jsonUrl = jsonUrl;
  16. var host = window.EASYMAP_CONFIG['poihost'];
  17. if(options.layer === 'xs_cb_cbyj'){
  18. this._url = host + '/poiwmts/color/getTile';
  19. }else{
  20. this._url = url;
  21. }
  22. var wmtsParams = L.extend({}, this.defaultWmtsParams);
  23. var tileSize = options.tileSize || this.options.tileSize;
  24. if (options.detectRetina && L.Browser.retina) {
  25. wmtsParams.width = wmtsParams.height = tileSize * 2;
  26. } else {
  27. wmtsParams.width = wmtsParams.height = tileSize;
  28. }
  29. for (var i in options) {
  30. // all keys that are not TileLayer options go to WMTS params
  31. if (!this.options.hasOwnProperty(i) && i != "matrixIds" && i != "jsonUrl") {
  32. wmtsParams[i] = options[i];
  33. }
  34. }
  35. this.wmtsParams = wmtsParams;
  36. this.matrixIds = options.matrixIds || this.getDefaultMatrix();
  37. //console.log(this);
  38. if (!options.zIndex || options.zIndex < 10) {
  39. options.zIndex = 10;
  40. }
  41. L.setOptions(this, options);
  42. var _this = this;
  43. this.eventMap = {
  44. click: function (ev) {
  45. if (_this.clickHander) {
  46. clearTimeout(_this.clickHander)
  47. _this.clickHander = undefined;
  48. } else {
  49. _this.clickHander = setTimeout(function () {
  50. var minPoint = ev.layerPoint.subtract(L.point(12, 12));
  51. var maxPoint = ev.layerPoint.add(L.point(12, 12));
  52. var minLatLng = map.layerPointToLatLng(minPoint);
  53. var maxLatLng = map.layerPointToLatLng(maxPoint);
  54. ////BBOX:"BBOX(minx,maxx,maxy,miny)
  55. var bbox = "BBOX(" + minLatLng.lng + "," + maxLatLng.lng + "," + minLatLng.lat + "," + maxLatLng.lat + ")";
  56. _this.getClickPointBBoxJsonMarker(bbox, markerClick)
  57. _this.clickHander = undefined;
  58. }, 300);
  59. }
  60. //_this.markerLayerGroup = L.marker(ev.latlng,{iconUrl:'images/marker-icon-2x.png'}).addTo(map);
  61. /*if(!_this.markerLayerGroup || !_this.markerLayerGroup.popup){
  62. return ;
  63. }
  64. var minDistance = -1;
  65. var minDistancePopup;
  66. var markerInfo;
  67. var distance;
  68. for( var key in _this.markerLayerGroup.popup ){
  69. distance = ev.latlng.distanceTo(_this.markerLayerGroup.popup[key].getLatLng());
  70. //console.log("....."+distance);
  71. if(minDistance == -1){
  72. minDistance = distance;
  73. minDistancePopup = _this.markerLayerGroup.popup[key];
  74. markerInfo = _this.markerLayerGroup.markerInfo[key];
  75. continue;
  76. }
  77. if(minDistance > distance ){
  78. minDistancePopup = _this.markerLayerGroup.popup[key]
  79. markerInfo = _this.markerLayerGroup.markerInfo[key];
  80. minDistance = distance;
  81. }
  82. }
  83. if(minDistancePopup){
  84. var pointDistace = ev.layerPoint.distanceTo(map.latLngToLayerPoint(minDistancePopup.getLatLng()));
  85. var maxPointDistace = Math.max(markerInfo.markerWidth,markerInfo.markerHeigth)/2;
  86. if(pointDistace <= maxPointDistace){
  87. markerClick && markerClick({markerInfo:})
  88. // minDistancePopup.openOn(map);
  89. }
  90. }*/
  91. },
  92. };
  93. window.map.on(this.eventMap);
  94. this.shapeGroup = L.featureGroup().addTo(window.map);
  95. },
  96. onRemove: function (map) {
  97. window.map.off(this.eventMap);
  98. if(this.mouseoverObj){
  99. this.mouseoverObj.closePopup();
  100. this.mouseoverObj.unbindPopup();
  101. this.mouseoverObj = null;
  102. }
  103. this.shapeGroup.clearLayers();
  104. this.shapeGroup.off();
  105. map.removeLayer(this.shapeGroup);
  106. L.TileLayer.WMTS.prototype.onRemove.call(this, map);
  107. },
  108. // @method createTile(coords: Object, done?: Function): HTMLElement
  109. // Called only internally, overrides GridLayer's [`createTile()`](#gridlayer-createtile)
  110. // to return an `<img>` HTML element with the appropiate image URL given `coords`. The `done`
  111. // callback is called when the tile has been loaded.
  112. createTile: function (coords, done) {
  113. var tile = document.createElement('img');
  114. L.DomEvent.on(tile, 'load', L.bind(this._tileOnLoad, this, done, tile));
  115. L.DomEvent.on(tile, 'error', L.bind(this._tileOnError, this, done, tile));
  116. if (this.options.crossOrigin) {
  117. tile.crossOrigin = '';
  118. }
  119. /*
  120. Alt tag is set to empty string to keep screen readers from reading URL and for compliance reasons
  121. http://www.w3.org/TR/WCAG20-TECHS/H67
  122. */
  123. tile.alt = '';
  124. /*
  125. Set role="presentation" to force screen readers to ignore this
  126. https://www.w3.org/TR/wai-aria/roles#textalternativecomputation
  127. */
  128. tile.setAttribute('role', 'presentation');
  129. tile.src = this.getTileUrl(coords);
  130. var _this = this;
  131. if(this._jsonUrl && this.options.loadShape ){
  132. setTimeout(function(){
  133. _this.loadShape(coords);
  134. },100);
  135. }
  136. return tile;
  137. },
  138. loadShape: function(coords){
  139. if (!this._jsonUrl) {
  140. return "";
  141. }
  142. console.log(this.shapeGroup.getLayers().length)
  143. var url = L.Util.template(this._jsonUrl, {});
  144. var param = L.extend({}, this.wmtsParams);
  145. param.format = "json";
  146. param.size = 100;
  147. url += this.getParamString(param, this._jsonUrl)
  148. + "&tilematrix=" + (coords.z) + "&tilerow=" + coords.y +"&tilecol=" + coords.x;
  149. // this.shapeGroup.clearLayers();
  150. let _this = this;
  151. this.doXHR(url, function (data) {
  152. if( data && data.resultList && data.resultList.length > 0 ){
  153. let item,geometry;
  154. let shapes = [];
  155. let attributes = [];
  156. for( var i in data.resultList ){
  157. item = data.resultList[i];
  158. if(item && item.geoShapeType
  159. && item.geoShapeType.toLocaleLowerCase() != "point"
  160. && item.shape ){
  161. if(item.id in _this.shapeIds){
  162. continue;
  163. }else{
  164. _this.shapeIds[item.id] = 0;
  165. }
  166. geometry = omnivore.wkt.parse(item.shape );
  167. geometry.attribute = {name:item.name, pname:item.layerCnName}
  168. console.log(geometry)
  169. _this.shapeGroup.addLayer(geometry);
  170. // shapes.push(geometry);
  171. }
  172. }
  173. _this.shapeGroupOn();
  174. }
  175. });
  176. },
  177. shapeGroupOn:function(){
  178. this.shapeGroup.setStyle( {dashArray :'12',weight:1,color :'#172d58',fill:false});
  179. let _this = this;
  180. this.shapeGroup.eachLayer(function(layer){
  181. layer.unbindPopup().bindPopup(layer.attribute.name,{closeButton:false})
  182. layer.off().on("mousemove",function(e){
  183. this.openPopup();
  184. if( _this.mouseoverObj && _this.mouseoverObj != this ){
  185. setTimeout(function(handle){
  186. handle.setStyle( {dashArray :'12',weight:1,color :'#172d58',fill:false});
  187. },1000,_this.mouseoverObj)
  188. }
  189. _this.mouseoverObj = this.setStyle( {dashArray :null,weight:3,color :'#3388ff',fill:false});
  190. });
  191. });
  192. },
  193. getTileUrl: function (coords) { // (Point, Number) -> String
  194. //小于14层级,不显示具体图标,并且不能看详情
  195. if (this.options.layer === 'xs_cb_cbyj' && coords.z < this.options.showLabellevel) {
  196. this._url = window.EASYMAP_CONFIG['poihost'] + '/poiwmts/color/getTile'
  197. this.options.showTitle = false
  198. }else if(this.options.layer === 'xs_cb_cbyj' && coords.z >= this.options.showLabellevel){
  199. this._url = window.EASYMAP_CONFIG['poihost'] + '/poiwmts/aggMarker/getTile'
  200. this.options.showTitle = true
  201. }
  202. var url = L.Util.template(this._url, { s: this._getSubdomain(coords) });
  203. //argis版本的要-1
  204. url += this.getParamString(this.wmtsParams, url) + "&tilematrix=" + (coords.z) + "&tilerow=" + coords.y + "&tilecol=" + coords.x + "&sessionToken=" + window.sessionToken;
  205. if (this.wmtsParams.hnjhpt_rid) {
  206. var date = new Date();
  207. var time = date.getTime();
  208. var message = this.wmtsParams.hnjhpt_sid + this.wmtsParams.hnjhpt_rid + time;
  209. var hash = CryptoJS.HmacSHA256(message, this.wmtsParams.secret);
  210. var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
  211. url += "&hnjhpt_sign=" + encodeURIComponent(hashInBase64) + "&hnjhpt_rtime=" + time
  212. }
  213. return url;
  214. },
  215. getJsonUrl: function (bbox) { // (Point, Number) -> String
  216. if (!this._jsonUrl) {
  217. return "";
  218. }
  219. var url = L.Util.template(this._jsonUrl, {});
  220. var param = L.extend({}, this.wmtsParams);
  221. param.format = "json";
  222. param.size = 1;
  223. // lat: 17.9736328125
  224. // lng: 108.973388671875
  225. param.bbox = bbox;
  226. url += this.getParamString(param, this._jsonUrl);
  227. //+ "&tilematrix=" + (coords.z) + "&tilerow=" + coords.y +"&tilecol=" + coords.x;
  228. /*if (this.wmtsParams.hnjhpt_rid) {
  229. var date = new Date();
  230. var time = date.getTime();
  231. var message = this.wmtsParams.hnjhpt_sid + this.wmtsParams.hnjhpt_rid + time;
  232. var hash = CryptoJS.HmacSHA256(message, this.wmtsParams.secret);
  233. var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
  234. url += "&hnjhpt_sign=" + encodeURIComponent(hashInBase64) + "&hnjhpt_rtime=" + time
  235. }*/
  236. return url;
  237. },
  238. getClickPointBBoxJsonMarker: function (bbox, callback) {
  239. //判断图标是否可以点击显示详情
  240. if(!this.options.showTitle){
  241. return;
  242. }
  243. var _this = this;
  244. /*if( !this.markerLayerGroup ){
  245. this.markerLayerGroup = L.layerGroup().addTo(map);
  246. }*/
  247. this.doXHR(this.getJsonUrl(bbox), function (data) {
  248. if (data && data.resultList && data.resultList.length && data.resultList.length > 0) {
  249. if (callback) {
  250. callback({ markerInfo: data.resultList[0] });
  251. } else {
  252. L.popup().setLatLng([parseFloat(data.resultList[0].lat)
  253. , parseFloat(data.resultList[0].lon)])
  254. .setContent('<p>名称:' + data.resultList[0].name + '<br />'
  255. + '地址:' + data.resultList[0].dz + '<br />'
  256. + '经度:' + data.resultList[0].lon + '<br />'
  257. + '纬度:' + data.resultList[0].lat
  258. + '</p>').openOn(window.map);;
  259. //.openOn(map);
  260. }
  261. //var i = 0;
  262. // var layername ,marker;
  263. // for( var i in data.resultList ){
  264. /*marker = L.marker([parseFloat(data.resultList[i].lat), parseFloat(data.resultList[i].lon)]
  265. ,{icon:L.icon({iconUrl:host+data.resultList[i].markerPath})}).bindPopup('<p>名称:'+data.resultList[i].name+'<br />'
  266. +'地址:'+data.resultList[i].dz
  267. +'</p>');
  268. _this.markerLayerGroup.addLayer(marker);
  269. marker = null; */
  270. /*layername = data.resultList[i].layername;
  271. if( !_this.markerLayerGroup ){
  272. _this.markerLayerGroup = {};
  273. }
  274. if(!_this.markerLayerGroup.popup){
  275. _this.markerLayerGroup.popup = {};
  276. }*/
  277. /*if(!_this.markerLayerGroup[layername]){
  278. _this.markerLayerGroup[layername] = {};
  279. }*/
  280. /*if(!_this.markerLayerGroup[layername].icon){
  281. _this.markerLayerGroup[layername].icon = L.icon({iconUrl:host+data.resultList[i].markerPath});
  282. }*/
  283. /*if(!_this.markerLayerGroup.markerInfo){
  284. _this.markerLayerGroup.markerInfo = {};
  285. }*/
  286. /*_this.markerLayerGroup.markerInfo[data.resultList[i].id] = data.resultList[i];
  287. _this.markerLayerGroup.popup[data.resultList[i].id] = L.popup().setLatLng([parseFloat(data.resultList[i].lat)
  288. , parseFloat(data.resultList[i].lon)])
  289. .setContent('<p>名称:'+data.resultList[i].name+'<br />'
  290. +'地址:'+data.resultList[i].dz
  291. +'</p>');*/
  292. //.openOn(map);
  293. // }
  294. }
  295. });
  296. },
  297. doXHR: function (url, callback) {
  298. console.log("window.sessionToken",window.sessionToken)
  299. var httpXml = this.createXMLHttpRequest();
  300. httpXml.open('get', url, true);
  301. httpXml.setRequestHeader("sessionToken",window.sessionToken)
  302. httpXml.send(null);
  303. httpXml.onreadystatechange = function () {
  304. if (httpXml.readyState == 4 && httpXml.status == 200) {
  305. var data = JSON.parse(httpXml.responseText);
  306. if (callback) {
  307. callback(data);
  308. }
  309. }
  310. };
  311. },
  312. createXMLHttpRequest: function () {
  313. var xmlHttp;
  314. try {
  315. xmlHttp = new XMLHttpRequest();
  316. } catch (e) {
  317. // 适用于IE6
  318. try {
  319. xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  320. } catch (e) {
  321. // 适用于IE5.5,以及IE更早版本
  322. try {
  323. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  324. } catch (e) { }
  325. }
  326. }
  327. return xmlHttp;
  328. }
  329. });
  330. L.tileLayer.wmts.poi = function (url, jsonUrl, options, markerClick) {
  331. return new L.TileLayer.WMTS.POI(url, jsonUrl, options, markerClick);
  332. };