map.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <div id="container"></div>
  3. </template>
  4. <script>
  5. import Vue from 'vue';
  6. export default {
  7. data() {
  8. return {
  9. }
  10. },
  11. methods: {
  12. getMap() {
  13. var _this = this
  14. this.http.post( this.port.map.mapList, {},
  15. res => {
  16. if (res.code == "ok") {
  17. var list = res.data;
  18. if(list != null && list.length > 0){
  19. var map = new AMap.Map('container', {
  20. resizeEnable: true,
  21. zoom: 5
  22. });
  23. map.clearMap();
  24. var markers = [] ,
  25. infoWindow ,
  26. data = list;
  27. var marker;
  28. for(var i in data){
  29. var jfong=[ data[i].ylng , data[i].xlat];
  30. marker = new AMap.Marker({
  31. position: jfong,
  32. zIndex: 101,
  33. map:map
  34. });
  35. marker.setMap(map);
  36. marker.msg = data[i];
  37. marker.on('click', function(data){
  38. var str = "<div class='window'>" +
  39. "<div class='info-top'><div>"+ data.target.msg.companyName +"</div><i class='el-icon-close' @click='closeInfoWindow()'></i></div>" +
  40. "<div class='info-middle'>";
  41. for(var i in data.target.msg.list){
  42. str += "<div class='info-item'><a @click='jumpToMold("+ data.target.msg.list[i].id +")'>" + data.target.msg.list[i].modelName + "(" + data.target.msg.list[i].modelNo + ")</a>"
  43. if(data.target.msg.list[i].state=='0'){
  44. str += "<span class='info-state'><span class='info-ball' style='background:#999999;'></span>静止</span>"
  45. } else {
  46. str += "<span class='info-state'><span class='info-ball' style='background:#00CD66;'></span>运行</span>"
  47. }
  48. str += "</div>"
  49. }
  50. str += "</div>" +
  51. "</div>"
  52. var MyComponent = Vue.extend({
  53. template: str ,
  54. methods:{
  55. closeInfoWindow:function() {
  56. map.clearInfoWindow();
  57. },
  58. jumpToMold:function(id) {
  59. _this.$router.push("/moldList/" + id);
  60. }
  61. }
  62. });
  63. var component= new MyComponent().$mount();
  64. infoWindow.setContent(component.$el);
  65. infoWindow.open(map, data.lnglat);
  66. });
  67. }
  68. infoWindow = new AMap.InfoWindow({
  69. isCustom: true,
  70. draggable: true, //是否可拖动
  71. showShadow: true,
  72. autoMove: true,
  73. offset: new AMap.Pixel(0, -31),
  74. content:""
  75. });
  76. marker.setMap(map);
  77. }
  78. } else {
  79. this.$message({
  80. message: res.msg,
  81. type: "error"
  82. });
  83. }
  84. },
  85. error => {
  86. this.$message({
  87. message: error,
  88. type: "error"
  89. });
  90. });
  91. },
  92. },
  93. mounted() {
  94. //this.getMap();
  95. this.$el.style.height = (window.innerHeight - 100) + "px";
  96. const that = this;
  97. window.onresize = function temp() {
  98. this.$el.style.height = (window.innerHeight - 100) + "px";
  99. };
  100. //新地图
  101. var map = new AMap.Map('container', {
  102. zoom: 4,
  103. mapStyle: "amap://styles/grey",
  104. features: ['bg']
  105. });
  106. var _this = this;
  107. //加载相关组件
  108. AMapUI.load(['ui/geo/DistrictCluster', 'ui/misc/PointSimplifier', 'lib/$' , 'lib/utils'], function(DistrictCluster, PointSimplifier , $ , utils) {
  109. window.DistrictCluster = DistrictCluster;
  110. //启动页面
  111. var pointSimplifierIns = new PointSimplifier({
  112. map: map, //所属的地图实例
  113. autoSetFitView: false, //禁止自动更新地图视野
  114. zIndex: 110,
  115. getPosition: function(item) {
  116. if (!item) {
  117. return null;
  118. }
  119. //var parts = item.split(',');
  120. //返回经纬度
  121. //return [parseFloat(parts[0]), parseFloat(parts[1])];
  122. return [parseFloat(item.lng), parseFloat(item.lat)];
  123. },
  124. getHoverTitle: function(dataItem, idx) {
  125. var state = "";
  126. if(dataItem.state == '0'){
  127. state = "静止"
  128. } else if(dataItem.state == '1'){
  129. state = "运动"
  130. } else if(dataItem.state == '2'){
  131. state = "告警"
  132. } else if(dataItem.state == '3'){
  133. state = "报废"
  134. }
  135. return dataItem.modelName + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + state;
  136. },
  137. renderOptions: {
  138. //点的样式
  139. pointStyle: {
  140. width: 6,
  141. height: 6,
  142. fillStyle:'rgba(0, 236, 252, 0.38)'
  143. },
  144. //鼠标hover时的title信息
  145. hoverTitleStyle: {
  146. position: 'top'
  147. }
  148. }
  149. });
  150. function MyRender(distClusterIns, opts) {
  151. //直接调用父类的构造函数
  152. MyRender.__super__.constructor.apply(this, arguments);
  153. }
  154. //继承默认引擎
  155. utils.inherit(MyRender, DistrictCluster.Render.Default);
  156. utils.extend(MyRender.prototype, {
  157. drawFeaturePolygons: function(ctx, polygons, styleOptions, feature, dataItems) {
  158. //调用父类方法
  159. MyRender.__super__.drawFeaturePolygons.apply(this, arguments);
  160. //直接绘制聚合信息
  161. this.drawMyLabel(feature, dataItems);
  162. },
  163. _initContainter: function() {
  164. //调用父类方法
  165. MyRender.__super__._initContainter.apply(this, arguments);
  166. //创建一个新的canvas
  167. this._createCanvas('mylabel', this._container);
  168. },
  169. /**
  170. * 绘制一个蓝底白字的label替代聚合标注
  171. */
  172. drawMyLabel: function(feature, dataItems) {
  173. if(dataItems.length != 0){
  174. var pixelRatio = this.getPixelRatio(); //高清下存在比例放大
  175. var containerPos = map.lngLatToContainer(feature.properties.centroid || feature.properties.center);
  176. var labelCtx = this._getCanvasCxt('mylabel');
  177. //文字的中心点
  178. var centerX = containerPos.getX() * pixelRatio,
  179. centerY = containerPos.getY() * pixelRatio;
  180. labelCtx.save();
  181. labelCtx.font = 12 * pixelRatio + 'px 微软雅黑';
  182. var text = feature.properties.name + '\n' + dataItems.length + '';
  183. var textMetrics = labelCtx.measureText(text);
  184. var halfTxtWidth = textMetrics.width / 2;
  185. labelCtx.fillStyle = 'rgba(3 , 94 , 255 , 0.5)';//'#3366cc';
  186. //绘制一个蓝色背景
  187. labelCtx.fillRect(centerX - halfTxtWidth - 3 * pixelRatio,
  188. centerY - 11 * pixelRatio,
  189. textMetrics.width + 6 * pixelRatio,
  190. 22 * pixelRatio);
  191. labelCtx.fillStyle = '#ffffff';
  192. labelCtx.textBaseline = 'middle';
  193. labelCtx.stroke();
  194. labelCtx.fillText(text, centerX - halfTxtWidth, centerY);
  195. labelCtx.restore();
  196. }
  197. }
  198. });
  199. var distCluster = new DistrictCluster({
  200. zIndex: 100,
  201. map: map, //所属的地图实例
  202. getPosition: function(item) {
  203. if (!item) {
  204. return null;
  205. }
  206. // var parts = item.split(',');
  207. // console.log(item)
  208. //返回经纬度
  209. //return [parseFloat(parts[0]), parseFloat(parts[1])];
  210. return [parseFloat(item.lng), parseFloat(item.lat)];
  211. },
  212. renderConstructor: MyRender,
  213. //特定区划级别的默认样式
  214. renderOptions: {
  215. getClusterMarker: null,
  216. featureClickToShowSub: true,
  217. featureStyle: {
  218. lineWidth: 2, //描边线宽
  219. strokeStyle: 'rgba(31, 119, 180, 0.8)', //描边色
  220. //鼠标Hover后的样式
  221. hoverOptions: {
  222. fillStyle: 'rgba(255,255,255,0.2)'
  223. }
  224. },
  225. getFeatureStyle: function(feature, dataItems) {
  226. return {
  227. fillStyle: ''
  228. };
  229. }
  230. },
  231. });
  232. window.distCluster = distCluster;
  233. map.on('zoomend', function() {
  234. var zoom = map.getZoom();
  235. //获取 pointStyle
  236. var pointStyle = pointSimplifierIns.getRenderOptions().pointStyle;
  237. //根据当前zoom调整点的尺寸
  238. pointStyle.width = pointStyle.height = 2 * Math.pow(1.2, map.getZoom() - 3);
  239. });
  240. var zoom = map.getZoom();
  241. //获取 pointStyle
  242. var pointStyle = pointSimplifierIns.getRenderOptions().pointStyle;
  243. //根据当前zoom调整点的尺寸
  244. pointStyle.width = pointStyle.height = 2 * Math.pow(1.2, map.getZoom() - 3);
  245. $('<div id="loadingTip">加载数据,请稍候...</div>').appendTo(document.body);
  246. // $.get('https://a.amap.com/amap-ui/static/data/10w.txt', function(csv) {
  247. // $('#loadingTip').remove();
  248. // var data = csv.split('\n');
  249. // distCluster.setData(data);
  250. // pointSimplifierIns.setData(data);
  251. // });
  252. _this.http.post( _this.port.map.newMap, {},
  253. res => {
  254. $('#loadingTip').remove();
  255. var data = res.data;
  256. distCluster.setData(data);
  257. pointSimplifierIns.setData(data);
  258. },
  259. error => {
  260. _this.$message({
  261. message: error,
  262. type: "error"
  263. });
  264. })
  265. });
  266. }
  267. }
  268. </script>
  269. <style scoped lang="scss">
  270. #container {
  271. width: 100%;
  272. margin-top: 10px;
  273. }
  274. </style>