map.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <div class="mapItems">
  3. <modular-loading :loadding="loadding" tipsText="地图加载中"></modular-loading>
  4. <div class="topSelect">
  5. <div></div>
  6. </div>
  7. <div class="mapInit" id="mapInit"></div>
  8. </div>
  9. </template>
  10. <script>
  11. import { mapState } from 'vuex';
  12. export default {
  13. data() {
  14. return {
  15. map: '',
  16. disCountry: '',
  17. placeSearch: '',
  18. district: null,
  19. marker: '',
  20. polygons: [],
  21. addressName: '', //地址查询名称
  22. poisArray: [],
  23. loadding: true,
  24. centet: []
  25. };
  26. },
  27. mounted() {},
  28. computed: {
  29. ...mapState(['globalCommunity', 'findCompanyCommunityTree']),
  30. poisArrays() {
  31. let arrs = [];
  32. this.fitersValue(arrs, this.findCompanyCommunityTree);
  33. return arrs;
  34. }
  35. },
  36. watch: {
  37. globalCommunity: {
  38. handler(newVal, oldVal) {
  39. if (!!this.map) {
  40. this.map.clearMap();
  41. }
  42. if (this.poisArrays.length > 0) {
  43. this.addIcon(this.poisArrays);
  44. }
  45. },
  46. deep: true
  47. }
  48. },
  49. methods: {
  50. init() {
  51. this.map = new AMap.Map('mapInit', {
  52. resizeEnable: true, //是否监控地图容器尺寸变化
  53. center: !!this.centet[0] ? this.centet : '',
  54. zoom: !!this.centet[0] ? 8 : 5, //初始化地图层级
  55. mapStyle: 'amap://styles/darkblue',
  56. defaultCursor: 'pointer'
  57. });
  58. //地图加载完成监听点击事件
  59. this.map.on('complete', () => {
  60. this.loadding = false;
  61. if (this.poisArrays.length > 0) {
  62. this.addIcon(this.poisArrays);
  63. }
  64. this.mapClick();
  65. });
  66. },
  67. mapClick() {
  68. this.map.on('click', (ev) => {
  69. this.getAddress([ev.lnglat.lng, ev.lnglat.lat]);
  70. });
  71. },
  72. addIcon(arrs) {
  73. let self = this;
  74. arrs.map((item) => {
  75. self.marker = new AMap.Marker({
  76. icon: new AMap.Icon({
  77. image: require('@assets/img/icon_point_community.png'),
  78. size: new AMap.Size(40, 50),
  79. imageSize: new AMap.Size(40, 50)
  80. }),
  81. position: [item.longitude, item.latitude],
  82. map: self.map,
  83. communityData: item
  84. });
  85. self.marker.setLabel({
  86. offset: new AMap.Pixel(-15, 50),
  87. content: `${item.name}`
  88. });
  89. if (self.poisArrays.length < 2) {
  90. self.map.setFitView();
  91. }
  92. self.marker.on('click', function () {
  93. self.setCommunity(this.De.communityData);
  94. });
  95. });
  96. },
  97. setCommunity(its) {
  98. this.$store.commit('setGlobalCommunity', {
  99. value: [its.value]
  100. });
  101. let newarr = this.finterParent(this.findCompanyCommunityTree, its.value);
  102. newarr.push(its.value);
  103. this.$store.commit('setThisArr', newarr);
  104. this.$parent.showMap = false;
  105. },
  106. finterParent(data, id) {
  107. // 深度遍历查找
  108. function dfs(data, id, parents) {
  109. for (var i = 0; i < data.length; i++) {
  110. var item = data[i];
  111. // 找到id则返回父级id
  112. if (item.value === id) return parents;
  113. // children不存在或为空则不递归
  114. if (!item.children || !item.children.length) continue;
  115. // 往下查找时将当前id入栈
  116. parents.push(item.value);
  117. if (dfs(item.children, id, parents).length) return parents;
  118. // 深度遍历查找未找到时当前id 出栈
  119. parents.pop();
  120. }
  121. // 未找到时返回空数组
  122. return [];
  123. }
  124. return dfs(data, id, []);
  125. },
  126. //搜索区域
  127. districtMask(val) {
  128. if (!this.district) {
  129. //实例化DistrictSearch
  130. var opts = {
  131. subdistrict: 0, //获取边界不需要返回下级行政区
  132. extensions: 'all', //返回行政区边界坐标组等具体信息
  133. level: 'district' //查询行政级别为 市
  134. };
  135. this.district = new AMap.DistrictSearch(opts);
  136. }
  137. // this.district.setLevel(val);
  138. this.district.search(val, (status, result) => {
  139. this.map.remove(this.polygons); //清除上次结果
  140. this.polygons = [];
  141. var bounds = result.districtList[0].boundaries;
  142. if (bounds) {
  143. for (var i = 0, l = bounds.length; i < l; i++) {
  144. //生成行政区划polygon
  145. var polygon = new AMap.Polygon({
  146. strokeWeight: 1, //边界宽度
  147. path: bounds[i],
  148. fillOpacity: 0.3, // 透明度
  149. fillColor: '#2887f2',
  150. strokeColor: '#2887f2' //边界颜色
  151. });
  152. this.polygons.push(polygon);
  153. }
  154. }
  155. this.map.add(this.polygons);
  156. this.map.setFitView(this.polygons); //视口自适应
  157. });
  158. },
  159. getAddress(points) {
  160. //获取准确地址
  161. let geocoder = new AMap.Geocoder({ radius: 1000 });
  162. geocoder.getAddress(points, (status, result) => {
  163. if (status === 'complete' && result.regeocode) {
  164. this.districtMask(result.regeocode.addressComponent.district);
  165. } else {
  166. this.map.remove(this.polygons);
  167. }
  168. });
  169. },
  170. getPoin(resolve) {
  171. this.$http.get('/sc-community/assets/tree/community/findCompanyCommunityTree').then(({ data, status, msg }) => {
  172. if (status == 0) {
  173. let arrs = [];
  174. this.fitersValue(arrs, data);
  175. this.centet = this.getPointsCenter(arrs);
  176. resolve && resolve(true);
  177. }
  178. });
  179. },
  180. getLoc(resolve) {
  181. this.$http.get('/sc-user-center/user/findUserArea').then(({ data, status, msg }) => {
  182. if (status == 0) {
  183. this.centet[0] = data.lng;
  184. this.centet[1] = data.lat;
  185. resolve && resolve(true);
  186. }
  187. });
  188. },
  189. getPointsCenter(points) {
  190. var point_num = points.length; //坐标点个数
  191. var X = 0,
  192. Y = 0,
  193. Z = 0;
  194. for (let i = 0; i < points.length; i++) {
  195. if (points[i] == '') {
  196. continue;
  197. }
  198. var lat, lng, x, y, z;
  199. lat = (parseFloat(points[i].latitude) * Math.PI) / 180;
  200. lng = (parseFloat(points[i].longitude) * Math.PI) / 180;
  201. x = Math.cos(lat) * Math.cos(lng);
  202. y = Math.cos(lat) * Math.sin(lng);
  203. z = Math.sin(lat);
  204. X += x;
  205. Y += y;
  206. Z += z;
  207. }
  208. X = X / point_num;
  209. Y = Y / point_num;
  210. Z = Z / point_num;
  211. var tmp_lng = Math.atan2(Y, X);
  212. var tmp_lat = Math.atan2(Z, Math.sqrt(X * X + Y * Y));
  213. return [(tmp_lng * 180) / Math.PI, (tmp_lat * 180) / Math.PI];
  214. return [
  215. Math.floor(((tmp_lng * 180) / Math.PI) * 1000000) / 1000000,
  216. Math.floor(((tmp_lat * 180) / Math.PI) * 1000000) / 1000000
  217. ];
  218. },
  219. fitersValue(arrs, value) {
  220. let thisValue = this.globalCommunity.value;
  221. value.map((item) => {
  222. if (item.type == 'org') {
  223. if (!!item.children) {
  224. this.fitersValue(arrs, item.children);
  225. }
  226. } else {
  227. if (!!thisValue && thisValue.includes(item.parentId)) {
  228. arrs.push(item);
  229. }
  230. }
  231. });
  232. }
  233. },
  234. destroyed() {
  235. this.map && this.map.destroy();
  236. },
  237. created() {
  238. new Promise((resolve) => {
  239. this.getPoin(resolve);
  240. }).then(() => {
  241. new Promise((resolve) => {
  242. if (!!AMap) {
  243. resolve(true);
  244. }
  245. }).then(() => {
  246. this.init();
  247. });
  248. });
  249. }
  250. };
  251. </script>
  252. <style scoped lang="scss">
  253. @import '@assets/css/public-style.scss';
  254. .mapItems {
  255. height: 100%;
  256. position: relative;
  257. color: white;
  258. .topSelect {
  259. position: absolute;
  260. top: rem(15);
  261. left: 0;
  262. padding: 0 rem(15);
  263. width: 100%;
  264. }
  265. }
  266. #mapInit {
  267. width: 100%;
  268. height: 100%;
  269. /deep/ .amap-marker-label {
  270. background: rgba(0, 0, 0, 0.5);
  271. text-align: center;
  272. min-width: rem(80);
  273. min-height: rem(20);
  274. line-height: rem(20);
  275. box-shadow: 0px 2px 10px 0px rgba(66, 70, 86, 0.08);
  276. border: 0 none;
  277. white-space: nowrap;
  278. box-shadow: 0 0 5px 0rgba (0, 0, 0, 0.3);
  279. }
  280. }
  281. </style>