map.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 + "/0");
  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. that.$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. mapStyle: "amap://styles/8016abec658e1132508723183f848f14",
  105. features: ['bg']
  106. });
  107. var _this = this;
  108. //加载相关组件
  109. AMapUI.load(['ui/geo/DistrictCluster', 'ui/misc/PointSimplifier', 'lib/$' , 'lib/utils'], function(DistrictCluster, PointSimplifier , $ , utils) {
  110. window.DistrictCluster = DistrictCluster;
  111. //启动页面
  112. var pointSimplifierIns = new PointSimplifier({
  113. map: map, //所属的地图实例
  114. autoSetFitView: false, //禁止自动更新地图视野
  115. zIndex: 110,
  116. getPosition: function(item) {
  117. if (!item) {
  118. return null;
  119. }
  120. //var parts = item.split(',');
  121. //返回经纬度
  122. //return [parseFloat(parts[0]), parseFloat(parts[1])];
  123. return [parseFloat(item.lng), parseFloat(item.lat)];
  124. },
  125. getHoverTitle: function(dataItem, idx) {
  126. var state = "";
  127. if(dataItem.state == '0'){
  128. state = "静止"
  129. } else if(dataItem.state == '1'){
  130. state = "运动"
  131. } else if(dataItem.state == '2'){
  132. state = "告警"
  133. } else if(dataItem.state == '3'){
  134. state = "报废"
  135. }
  136. return dataItem.modelName + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + state;
  137. },
  138. renderOptions: {
  139. //点的样式
  140. pointStyle: {
  141. width: 6,
  142. height: 6,
  143. fillStyle:'rgba(0, 236, 252, 0.9)'
  144. },
  145. //鼠标hover时的title信息
  146. hoverTitleStyle: {
  147. position: 'top'
  148. }
  149. }
  150. });
  151. function MyRender(distClusterIns, opts) {
  152. //直接调用父类的构造函数
  153. MyRender.__super__.constructor.apply(this, arguments);
  154. }
  155. //继承默认引擎
  156. utils.inherit(MyRender, DistrictCluster.Render.Default);
  157. utils.extend(MyRender.prototype, {
  158. drawFeaturePolygons: function(ctx, polygons, styleOptions, feature, dataItems) {
  159. //调用父类方法
  160. MyRender.__super__.drawFeaturePolygons.apply(this, arguments);
  161. //直接绘制聚合信息
  162. this.drawMyLabel(feature, dataItems);
  163. },
  164. _initContainter: function() {
  165. //调用父类方法
  166. MyRender.__super__._initContainter.apply(this, arguments);
  167. //创建一个新的canvas
  168. this._createCanvas('mylabel', this._container);
  169. },
  170. /**
  171. * 绘制一个蓝底白字的label替代聚合标注
  172. */
  173. drawMyLabel: function(feature, dataItems) {
  174. //if(dataItems.length != 0){
  175. var pixelRatio = this.getPixelRatio(); //高清下存在比例放大
  176. var containerPos = map.lngLatToContainer(feature.properties.centroid || feature.properties.center);
  177. var labelCtx = this._getCanvasCxt('mylabel');
  178. //文字的中心点
  179. var centerX = containerPos.getX() * pixelRatio,
  180. centerY = containerPos.getY() * pixelRatio;
  181. labelCtx.save();
  182. labelCtx.font = 12 * pixelRatio + 'px 微软雅黑';
  183. var name = feature.properties.name;
  184. if(feature.properties.level == "province") {
  185. if(name == "黑龙江省" || name == "内蒙古自治区"){
  186. name = name.substring(0,3)
  187. } else {
  188. name = name.substring(0,2)
  189. }
  190. }
  191. var text = name;//feature.properties.name ;//+ '\n' + dataItems.length;
  192. var textMetrics = labelCtx.measureText(text);
  193. var halfTxtWidth = textMetrics.width / 2;
  194. var num = dataItems.length;
  195. var numMetrics = labelCtx.measureText(num);
  196. var halfNumWidth = numMetrics.width / 2;
  197. // labelCtx.fillStyle = 'rgba(3 , 94 , 255 , 0.5)';//'#3366cc';
  198. labelCtx.beginPath();
  199. labelCtx.fillStyle = 'rgba(12 , 239 , 231 , 0.8)';//'#3366cc';
  200. //绘制一个蓝色背景
  201. labelCtx.strokeStyle= "rgba(12 , 239 , 231 , 0.65)"
  202. labelCtx.fillStyle = 'rgba(12 , 239 , 231 , 0.65)';
  203. var radius = 0;
  204. if(textMetrics.width > numMetrics.width) {
  205. radius = (textMetrics.width + 6 * pixelRatio) / 2;
  206. } else {
  207. radius = (numMetrics.width + 6 * pixelRatio) / 2;
  208. }
  209. labelCtx.arc(centerX , centerY, radius + 6,
  210. 0*Math.PI, 2*Math.PI, false);
  211. labelCtx.closePath();
  212. labelCtx.fill();
  213. // labelCtx.fillRect(centerX - halfTxtWidth - 3 * pixelRatio,
  214. // centerY - 11 * pixelRatio,
  215. // textMetrics.width + 6 * pixelRatio,
  216. // 22 * pixelRatio);
  217. labelCtx.fillStyle = '#ffffff';
  218. labelCtx.textBaseline = 'middle';
  219. //labelCtx.stroke();
  220. labelCtx.fillText(text, centerX - halfTxtWidth, centerY - 5);
  221. labelCtx.fillText(num , centerX - halfNumWidth, centerY + 12);
  222. labelCtx.stroke();
  223. labelCtx.restore();
  224. //}
  225. }
  226. });
  227. var distCluster = new DistrictCluster({
  228. zIndex: 100,
  229. map: map, //所属的地图实例
  230. getPosition: function(item) {
  231. if (!item) {
  232. return null;
  233. }
  234. // var parts = item.split(',');
  235. //返回经纬度
  236. //return [parseFloat(parts[0]), parseFloat(parts[1])];
  237. return [parseFloat(item.lng), parseFloat(item.lat)];
  238. },
  239. renderConstructor: MyRender,
  240. //特定区划级别的默认样式
  241. renderOptions: {
  242. getClusterMarker: null,
  243. featureClickToShowSub: true,
  244. featureStyle: {
  245. lineWidth: 2, //描边线宽
  246. // strokeStyle: 'rgba(0, 119, 180, 0.5)', //描边色
  247. strokeStyle: 'rgba(75, 143, 239, 0.5)', //描边色
  248. //鼠标Hover后的样式
  249. hoverOptions: {
  250. fillStyle: 'rgba(255,255,255,0.2)'
  251. }
  252. },
  253. getFeatureStyle: function(feature, dataItems) {
  254. return {
  255. fillStyle: ''
  256. };
  257. }
  258. },
  259. });
  260. window.distCluster = distCluster;
  261. map.on('zoomend', function() {
  262. var zoom = map.getZoom();
  263. //获取 pointStyle
  264. var pointStyle = pointSimplifierIns.getRenderOptions().pointStyle;
  265. //根据当前zoom调整点的尺寸
  266. pointStyle.width = pointStyle.height = 2 * Math.pow(1.2, map.getZoom() - 3);
  267. });
  268. var zoom = map.getZoom();
  269. //获取 pointStyle
  270. var pointStyle = pointSimplifierIns.getRenderOptions().pointStyle;
  271. //根据当前zoom调整点的尺寸
  272. pointStyle.width = pointStyle.height = 2 * Math.pow(1.2, map.getZoom() - 3);
  273. $('<div id="loadingTip">加载数据,请稍候...</div>').appendTo(document.body);
  274. // $.get('https://a.amap.com/amap-ui/static/data/10w.txt', function(csv) {
  275. // $('#loadingTip').remove();
  276. // var data = csv.split('\n');
  277. // distCluster.setData(data);
  278. // pointSimplifierIns.setData(data);
  279. // });
  280. _this.http.post( _this.port.map.newMap, {},
  281. res => {
  282. $('#loadingTip').remove();
  283. var data = res.data;
  284. distCluster.setData(data);
  285. pointSimplifierIns.setData(data);
  286. },
  287. error => {
  288. _this.$message({
  289. message: error,
  290. type: "error"
  291. });
  292. })
  293. });
  294. }
  295. }
  296. </script>
  297. <style scoped lang="scss">
  298. #container {
  299. width: 100%;
  300. margin-top: 10px;
  301. }
  302. </style>