map.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. <template>
  2. <div id="container"></div>
  3. </template>
  4. <script>
  5. import Vue from 'vue';
  6. export default {
  7. data() {
  8. return {
  9. moldList: [],
  10. map: "",
  11. }
  12. },
  13. methods: {
  14. setMap() {
  15. var _this = this;
  16. AMapUI.load(['ui/geo/DistrictCluster', 'ui/misc/PointSimplifier', 'lib/$', 'lib/utils'], function(DistrictCluster, PointSimplifier, $, utils) {
  17. AMapUI.loadUI(['overlay/SvgMarker'], function(SvgMarker) {
  18. // var pointSimplifierIns = new PointSimplifier({
  19. // map: _this.map, //所属的地图实例
  20. // autoSetFitView: false, //禁止自动更新地图视野
  21. // zIndex: 110,
  22. // getPosition: function(item) {
  23. // if (!item) {
  24. // return null;
  25. // }
  26. // //返回经纬度
  27. // return [parseFloat(item.lng), parseFloat(item.lat)];
  28. // },
  29. // getHoverTitle: function(dataItem, idx) {
  30. // var state = "" , hovertitle = "";
  31. // if(dataItem.state == '0'){
  32. // state = '<span style="color:#909399">'+_this.$t('base.static')+'</span>'//_this.$t('base.static') // 静止
  33. // } else if(dataItem.state == '1'){
  34. // state = '<span style="color:#00CD66">'+_this.$t('base.run')+'</span>'
  35. // } else if(dataItem.state == '2'){
  36. // state = '<span style="color:#F56C6C">'+_this.$t('base.warning')+'</span>'
  37. // } else if(dataItem.state == '3'){
  38. // state = '<span style="color:#909399">'+_this.$t('base.beScrapped')+'</span>'
  39. // } else if(dataItem.state == '4'){
  40. // state = '<span style="color:#909399">'+_this.$t('base.scraped')+'</span>'
  41. // } else if(dataItem.state == '5'){
  42. // state = '<span style="color:#F56C6C">'+_this.$t('base.abnormal')+'</span>'
  43. // } else if(dataItem.state == '6'){
  44. // state = '<span style="color:#F56C6C">'+_this.$t('base.dropped')+'</span>'
  45. // }
  46. // hovertitle = "<div>模具名称:&nbsp;&nbsp;&nbsp;"+ dataItem.modelName +"</div><div>运行状态:&nbsp;&nbsp;&nbsp;"+ state +"</div>"
  47. // return hovertitle;
  48. // },
  49. // renderOptions: {
  50. // //点的样式
  51. // pointStyle: {
  52. // width: 6,
  53. // height: 6,
  54. // fillStyle:'rgba(255, 0, 0, 1)',
  55. // },
  56. // //鼠标hover时的title信息
  57. // hoverTitleStyle: {
  58. // position: 'top'
  59. // }
  60. // }
  61. // });
  62. function MyRender(distClusterIns, opts) {
  63. //直接调用父类的构造函数
  64. MyRender.__super__.constructor.apply(this, arguments);
  65. }
  66. //继承默认引擎
  67. utils.inherit(MyRender, DistrictCluster.Render.Default);
  68. utils.extend(MyRender.prototype, {
  69. drawFeaturePolygons: function(ctx, polygons, styleOptions, feature, dataItems) {
  70. //调用父类方法
  71. MyRender.__super__.drawFeaturePolygons.apply(this, arguments);
  72. //直接绘制聚合信息
  73. this.drawMyLabel(feature, dataItems);
  74. },
  75. _initContainter: function() {
  76. //调用父类方法
  77. MyRender.__super__._initContainter.apply(this, arguments);
  78. //创建一个新的canvas
  79. this._createCanvas('mylabel', this._container);
  80. },
  81. /**
  82. * 绘制一个蓝底白字的label替代聚合标注
  83. */
  84. drawMyLabel: function(feature, dataItems) {
  85. var pixelRatio = this.getPixelRatio(); //高清下存在比例放大
  86. var containerPos = _this.map.lngLatToContainer(feature.properties.centroid || feature.properties.center);
  87. var labelCtx = this._getCanvasCxt('mylabel');
  88. //文字的中心点
  89. var centerX = containerPos.getX() * pixelRatio,
  90. centerY = containerPos.getY() * pixelRatio;
  91. labelCtx.save();
  92. labelCtx.font = 12 * pixelRatio + 'px 微软雅黑';
  93. var name = feature.properties.name;
  94. if(feature.properties.level == "province") {
  95. if(name == "黑龙江省" || name == "内蒙古自治区"){
  96. name = name.substring(0,3)
  97. } else {
  98. name = name.substring(0,2)
  99. }
  100. }
  101. var text = name;
  102. var textMetrics = labelCtx.measureText(text);
  103. var halfTxtWidth = textMetrics.width / 2;
  104. var num = dataItems.length;
  105. var numMetrics = labelCtx.measureText(num);
  106. var halfNumWidth = numMetrics.width / 2;
  107. labelCtx.beginPath();
  108. labelCtx.fillStyle = 'rgba(12 , 239 , 231 , 0.8)';//'#3366cc';
  109. //绘制一个蓝色背景
  110. labelCtx.strokeStyle= "rgba(12 , 239 , 231 , 0.65)"
  111. labelCtx.fillStyle = 'rgba(12 , 239 , 231 , 0.65)';
  112. var radius = 0;
  113. if(textMetrics.width > numMetrics.width) {
  114. radius = (textMetrics.width + 6 * pixelRatio) / 2;
  115. } else {
  116. radius = (numMetrics.width + 6 * pixelRatio) / 2;
  117. }
  118. labelCtx.arc(centerX , centerY, radius + 6,
  119. 0*Math.PI, 2*Math.PI, false);
  120. labelCtx.closePath();
  121. labelCtx.fill();
  122. labelCtx.fillStyle = '#ffffff';
  123. labelCtx.textBaseline = 'middle';
  124. labelCtx.fillText(text, centerX - halfTxtWidth, centerY - 5);
  125. labelCtx.fillText(num , centerX - halfNumWidth, centerY + 12);
  126. labelCtx.stroke();
  127. labelCtx.restore();
  128. }
  129. });
  130. var distCluster = new DistrictCluster({
  131. zIndex: 100,
  132. map: _this.map, //所属的地图实例
  133. getPosition: function(item) {
  134. if (!item) {
  135. return null;
  136. }
  137. //返回经纬度
  138. return [parseFloat(item.lng) + (0.0001 * Math.random() * 10), parseFloat(item.lat) - (0.0001 * Math.random() * 10)];
  139. },
  140. renderConstructor: MyRender,
  141. //特定区划级别的默认样式
  142. renderOptions: {
  143. getClusterMarker: null,
  144. featureClickToShowSub: true,
  145. featureStyle: {
  146. lineWidth: 2, //描边线宽
  147. strokeStyle: 'rgba(75, 143, 239, 0.5)', //描边色
  148. //鼠标Hover后的样式
  149. hoverOptions: {
  150. fillStyle: 'rgba(255,255,255,0.2)'
  151. }
  152. },
  153. getFeatureStyle: function(feature, dataItems) {
  154. return {
  155. fillStyle: ''
  156. };
  157. }
  158. },
  159. });
  160. window.distCluster = distCluster;
  161. // function refresh() {
  162. // var zoom = _this.map.getZoom();
  163. // //获取 pointStyle
  164. // var pointStyle = pointSimplifierIns.getRenderOptions().pointStyle;
  165. // //根据当前zoom调整点的尺寸
  166. // pointStyle.width = pointStyle.height = 2 * Math.pow(1.2, _this.map.getZoom() - 3);
  167. // }
  168. // _this.map.on('zoomend', function() {
  169. // refresh();
  170. // });
  171. // refresh();
  172. if (!SvgMarker.supportSvg) {
  173. //当前环境并不支持SVG,此时SvgMarker会回退到父类,即SimpleMarker
  174. alert('当前环境不支持SVG');
  175. }
  176. var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -10)});
  177. function setPoint(data) {
  178. for (var i = 0, marker; i < data.length; i++) {
  179. // var marker = new AMap.Marker({
  180. // position: [parseFloat(data[i].lng), parseFloat(data[i].lat)],
  181. // map: _this.map
  182. // });
  183. var shape = new SvgMarker.Shape['Circle']({
  184. height: 20 * (1 * 0.3),
  185. strokeWidth: 1,
  186. strokeColor: 'rgba(255, 0, 0, 1)',
  187. fillColor: 'rgba(255, 0, 0, 1)'
  188. });
  189. var marker = new SvgMarker(
  190. shape, {
  191. map: _this.map,
  192. position: [parseFloat(data[i].lng) + (0.0001 * Math.random() * 10), parseFloat(data[i].lat) - (0.0001 * Math.random() * 10)],
  193. }
  194. );
  195. var state = "";
  196. if(data[i].state == '0'){
  197. state = '<span style="color:#909399">'+_this.$t('base.static')+'</span>'//_this.$t('base.static') // 静止
  198. } else if(data[i].state == '1'){
  199. state = '<span style="color:#00CD66">'+_this.$t('base.run')+'</span>'
  200. } else if(data[i].state == '2'){
  201. state = '<span style="color:#F56C6C">'+_this.$t('base.warning')+'</span>'
  202. } else if(data[i].state == '3'){
  203. state = '<span style="color:#909399">'+_this.$t('mold.beScrapped')+'</span>'
  204. } else if(data[i].state == '4'){
  205. state = '<span style="color:#909399">'+_this.$t('mold.scraped')+'</span>'
  206. } else if(data[i].state == '5'){
  207. state = '<span style="color:#F56C6C">'+_this.$t('mold.abnormal')+'</span>'
  208. } else if(data[i].state == '6'){
  209. state = '<span style="color:#F56C6C">'+_this.$t('mold.dropped')+'</span>'
  210. }
  211. var str = "<div>模具名称:&nbsp;&nbsp;&nbsp;<router-link :to='{ path: '/moldList/"+ data[i].id +"/0' }'>"+ data[i].modelName +"</router-link></div>"
  212. +"<div>运行状态:&nbsp;&nbsp;&nbsp;"+ state +"</div>"
  213. marker.content = str;
  214. marker.id = data[i].id;
  215. marker.on('click', jumpTo);
  216. marker.on('mouseover', openInfo);
  217. marker.on('mouseout', closeInfo);
  218. }
  219. }
  220. function jumpTo(e) {
  221. _this.$router.push("/moldList/" + e.target.id + "/0");
  222. }
  223. function openInfo(e) {
  224. infoWindow.setContent(e.target.content);
  225. infoWindow.open(_this.map, e.target.getPosition());
  226. }
  227. function closeInfo() {
  228. infoWindow.close();
  229. }
  230. $('<div id="loadingTip">加载数据,请稍候...</div>').appendTo(document.body);
  231. _this.http.post( _this.port.map.newMap, {},
  232. res => {
  233. $('#loadingTip').remove();
  234. var data = res.data;
  235. _this.moldList = data;
  236. distCluster.setData(data);
  237. // pointSimplifierIns.setData(data);
  238. setPoint(data);
  239. _this.map.setZoomAndCenter(5, [105.205467, 34.907761]);
  240. },
  241. error => {
  242. _this.$message({
  243. message: error,
  244. type: "error"
  245. });
  246. })
  247. });
  248. })
  249. },
  250. jumpTo(id) {
  251. console.log(id)
  252. }
  253. },
  254. mounted() {
  255. this.$el.style.height = (window.innerHeight - 100) + "px";
  256. const that = this;
  257. window.onresize = function temp() {
  258. that.$el.style.height = (window.innerHeight - 100) + "px";
  259. };
  260. //新地图
  261. that.map = new AMap.Map('container', {
  262. zoom: 4,
  263. mapStyle: "amap://styles/8016abec658e1132508723183f848f14",
  264. features: ['bg']
  265. });
  266. that.setMap();
  267. },
  268. // mounted() {
  269. // this.$el.style.height = (window.innerHeight - 100) + "px";
  270. // const that = this;
  271. // window.onresize = function temp() {
  272. // that.$el.style.height = (window.innerHeight - 100) + "px";
  273. // };
  274. // //新地图
  275. // var map = new AMap.Map('container', {
  276. // zoom: 4,
  277. // mapStyle: "amap://styles/8016abec658e1132508723183f848f14",
  278. // features: ['bg']
  279. // });
  280. // var _this = this;
  281. // //加载相关组件
  282. // AMapUI.load(['ui/geo/DistrictCluster', 'ui/misc/PointSimplifier', 'lib/$' , 'lib/utils'], function(DistrictCluster, PointSimplifier , $ , utils) {
  283. // window.DistrictCluster = DistrictCluster;
  284. // //启动页面
  285. // var pointSimplifierIns = new PointSimplifier({
  286. // map: map, //所属的地图实例
  287. // autoSetFitView: false, //禁止自动更新地图视野
  288. // zIndex: 110,
  289. // getPosition: function(item) {
  290. // if (!item) {
  291. // return null;
  292. // }
  293. // console.log(item)
  294. // //返回经纬度
  295. // // return [parseFloat(item.lng), parseFloat(item.lat)];
  296. // return [item.lng, item.lat];
  297. // },
  298. // getHoverTitle: function(dataItem, idx) {
  299. // var state = "";
  300. // if(dataItem.state == '0'){
  301. // state = '<span style="color:#909399">'+_this.$t('base.static')+'</span>'//_this.$t('base.static') // 静止
  302. // } else if(dataItem.state == '1'){
  303. // state = '<span style="color:#00CD66">'+_this.$t('base.run')+'</span>'
  304. // // state = _this.$t('base.run') // 运行
  305. // } else if(dataItem.state == '2'){
  306. // state = '<span style="color:#F56C6C">'+_this.$t('base.warning')+'</span>'
  307. // // state = _this.$t('base.warning') // 告警
  308. // } else if(dataItem.state == '3'){
  309. // state = '<span style="color:#909399">'+_this.$t('base.beScrapped')+'</span>'
  310. // // state = _this.$t('base.scrap') // 报废
  311. // } else if(dataItem.state == '4'){
  312. // state = '<span style="color:#909399">'+_this.$t('base.scraped')+'</span>'
  313. // } else if(dataItem.state == '5'){
  314. // state = '<span style="color:#F56C6C">'+_this.$t('base.abnormal')+'</span>'
  315. // } else if(dataItem.state == '6'){
  316. // state = '<span style="color:#F56C6C">'+_this.$t('base.dropped')+'</span>'
  317. // }
  318. // var str = "<div>模具名称:&nbsp;&nbsp;&nbsp;"+ dataItem.modelName +"</div><div>运行状态:&nbsp;&nbsp;&nbsp;"+ state +"</div>"
  319. // return str;
  320. // },
  321. // renderOptions: {
  322. // //点的样式
  323. // pointStyle: {
  324. // width: 6,
  325. // height: 6,
  326. // // fillStyle:'rgba(0, 236, 252, 0.9)'
  327. // fillStyle:'rgba(255, 0, 0, 1)',
  328. // },
  329. // //鼠标hover时的title信息
  330. // hoverTitleStyle: {
  331. // position: 'top'
  332. // },
  333. // }
  334. // });
  335. // function MyRender(distClusterIns, opts) {
  336. // //直接调用父类的构造函数
  337. // MyRender.__super__.constructor.apply(this, arguments);
  338. // }
  339. // //继承默认引擎
  340. // utils.inherit(MyRender, DistrictCluster.Render.Default);
  341. // utils.extend(MyRender.prototype, {
  342. // drawFeaturePolygons: function(ctx, polygons, styleOptions, feature, dataItems) {
  343. // //调用父类方法
  344. // MyRender.__super__.drawFeaturePolygons.apply(this, arguments);
  345. // //直接绘制聚合信息
  346. // this.drawMyLabel(feature, dataItems);
  347. // },
  348. // _initContainter: function() {
  349. // //调用父类方法
  350. // MyRender.__super__._initContainter.apply(this, arguments);
  351. // //创建一个新的canvas
  352. // this._createCanvas('mylabel', this._container);
  353. // },
  354. // /**
  355. // * 绘制一个蓝底白字的label替代聚合标注
  356. // */
  357. // drawMyLabel: function(feature, dataItems) {
  358. // var pixelRatio = this.getPixelRatio(); //高清下存在比例放大
  359. // var containerPos = map.lngLatToContainer(feature.properties.centroid || feature.properties.center);
  360. // var labelCtx = this._getCanvasCxt('mylabel');
  361. // //文字的中心点
  362. // var centerX = containerPos.getX() * pixelRatio,
  363. // centerY = containerPos.getY() * pixelRatio;
  364. // labelCtx.save();
  365. // labelCtx.font = 12 * pixelRatio + 'px 微软雅黑';
  366. // var name = feature.properties.name;
  367. // if(feature.properties.level == "province") {
  368. // if(name == "黑龙江省" || name == "内蒙古自治区"){
  369. // name = name.substring(0,3)
  370. // } else {
  371. // name = name.substring(0,2)
  372. // }
  373. // }
  374. // var text = name;//feature.properties.name ;//+ '\n' + dataItems.length;
  375. // var textMetrics = labelCtx.measureText(text);
  376. // var halfTxtWidth = textMetrics.width / 2;
  377. // var num = dataItems.length;
  378. // var numMetrics = labelCtx.measureText(num);
  379. // var halfNumWidth = numMetrics.width / 2;
  380. // // labelCtx.fillStyle = 'rgba(3 , 94 , 255 , 0.5)';//'#3366cc';
  381. // labelCtx.beginPath();
  382. // labelCtx.fillStyle = 'rgba(12 , 239 , 231 , 0.8)';//'#3366cc';
  383. // //绘制一个蓝色背景
  384. // labelCtx.strokeStyle= "rgba(12 , 239 , 231 , 0.65)"
  385. // labelCtx.fillStyle = 'rgba(12 , 239 , 231 , 0.65)';
  386. // var radius = 0;
  387. // if(textMetrics.width > numMetrics.width) {
  388. // radius = (textMetrics.width + 6 * pixelRatio) / 2;
  389. // } else {
  390. // radius = (numMetrics.width + 6 * pixelRatio) / 2;
  391. // }
  392. // labelCtx.arc(centerX , centerY, radius + 6,
  393. // 0*Math.PI, 2*Math.PI, false);
  394. // labelCtx.closePath();
  395. // labelCtx.fill();
  396. // labelCtx.fillStyle = '#ffffff';
  397. // labelCtx.textBaseline = 'middle';
  398. // labelCtx.fillText(text, centerX - halfTxtWidth, centerY - 5);
  399. // labelCtx.fillText(num , centerX - halfNumWidth, centerY + 12);
  400. // labelCtx.stroke();
  401. // labelCtx.restore();
  402. // }
  403. // });
  404. // var distCluster = new DistrictCluster({
  405. // zIndex: 100,
  406. // map: map, //所属的地图实例
  407. // getPosition: function(item) {
  408. // if (!item) {
  409. // return null;
  410. // }
  411. // //返回经纬度
  412. // // return [parseFloat(item.lng), parseFloat(item.lat)];
  413. // return [item.lng, item.lat];
  414. // },
  415. // renderConstructor: MyRender,
  416. // //特定区划级别的默认样式
  417. // renderOptions: {
  418. // getClusterMarker: null,
  419. // featureClickToShowSub: true,
  420. // featureStyle: {
  421. // lineWidth: 2, //描边线宽
  422. // // strokeStyle: 'rgba(0, 119, 180, 0.5)', //描边色
  423. // strokeStyle: 'rgba(75, 143, 239, 0.5)', //描边色
  424. // //鼠标Hover后的样式
  425. // hoverOptions: {
  426. // fillStyle: 'rgba(255,255,255,0.2)'
  427. // }
  428. // },
  429. // getFeatureStyle: function(feature, dataItems) {
  430. // return {
  431. // fillStyle: ''
  432. // };
  433. // }
  434. // },
  435. // });
  436. // window.distCluster = distCluster;
  437. // map.on('zoomend', function() {
  438. // var zoom = map.getZoom();
  439. // //获取 pointStyle
  440. // var pointStyle = pointSimplifierIns.getRenderOptions().pointStyle;
  441. // //根据当前zoom调整点的尺寸
  442. // pointStyle.width = pointStyle.height = 2 * Math.pow(1.2, map.getZoom() - 3);
  443. // });
  444. // var zoom = map.getZoom();
  445. // //获取 pointStyle
  446. // var pointStyle = pointSimplifierIns.getRenderOptions().pointStyle;
  447. // //根据当前zoom调整点的尺寸
  448. // pointStyle.width = pointStyle.height = 2 * Math.pow(1.2, map.getZoom() - 3);
  449. // $('<div id="loadingTip">加载数据,请稍候...</div>').appendTo(document.body);
  450. // _this.http.post( _this.port.map.newMap, {},
  451. // res => {
  452. // $('#loadingTip').remove();
  453. // var data = res.data;
  454. // _this.moldList = data;
  455. // distCluster.setData(data);
  456. // pointSimplifierIns.setData(data);
  457. // map.setZoomAndCenter(5, [105.205467, 34.907761]);
  458. // },
  459. // error => {
  460. // _this.$message({
  461. // message: error,
  462. // type: "error"
  463. // });
  464. // })
  465. // });
  466. // }
  467. }
  468. </script>
  469. <style scoped lang="scss">
  470. #container {
  471. width: 100%;
  472. margin-top: 10px;
  473. }
  474. </style>