map.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 permissionComponent from './permissionComponent';
  12. export default {
  13. mixins: [permissionComponent],
  14. data() {
  15. return {
  16. map: '',
  17. disCountry: '',
  18. placeSearch: '',
  19. district: null,
  20. marker: '',
  21. polygons: [],
  22. addressName: '', //地址查询名称
  23. poisArray: [],
  24. loadding: true
  25. };
  26. },
  27. mounted() {},
  28. computed: {},
  29. methods: {
  30. init() {
  31. this.map = new AMap.Map('mapInit', {
  32. resizeEnable: true, //是否监控地图容器尺寸变化
  33. zoom: 8, //初始化地图层级
  34. mapStyle: 'amap://styles/darkblue',
  35. defaultCursor: 'pointer'
  36. });
  37. //地图加载完成监听点击事件
  38. this.map.on('complete', () => {
  39. this.loadding = false;
  40. this.mapMak();
  41. this.mapClick();
  42. });
  43. },
  44. mapClick() {
  45. this.map.on('click', (ev) => {
  46. this.getAddress([ev.lnglat.lng, ev.lnglat.lat]);
  47. });
  48. },
  49. mapMak() {
  50. if (this.poisArray.length > 0) {
  51. this.poisArray.map((item) => {
  52. this.addIcon(item);
  53. });
  54. }
  55. },
  56. addIcon(item) {
  57. this.marker = new AMap.Marker({
  58. icon: new AMap.Icon({
  59. image: require('@assets/img/homePage/icon_point_community@2x.png'),
  60. size: new AMap.Size(40, 50),
  61. imageSize: new AMap.Size(40, 50)
  62. }),
  63. position: [item.longitude, item.latitude],
  64. map: this.map
  65. });
  66. this.marker.setLabel({
  67. offset: new AMap.Pixel(-15, 50),
  68. content: `${item.communityName}`
  69. });
  70. },
  71. //搜索区域
  72. districtMask(val) {
  73. if (!this.district) {
  74. //实例化DistrictSearch
  75. var opts = {
  76. subdistrict: 0, //获取边界不需要返回下级行政区
  77. extensions: 'all', //返回行政区边界坐标组等具体信息
  78. level: 'district' //查询行政级别为 市
  79. };
  80. this.district = new AMap.DistrictSearch(opts);
  81. }
  82. // this.district.setLevel(val);
  83. this.district.search(val, (status, result) => {
  84. this.map.remove(this.polygons); //清除上次结果
  85. this.polygons = [];
  86. var bounds = result.districtList[0].boundaries;
  87. if (bounds) {
  88. for (var i = 0, l = bounds.length; i < l; i++) {
  89. //生成行政区划polygon
  90. var polygon = new AMap.Polygon({
  91. strokeWeight: 1, //边界宽度
  92. path: bounds[i],
  93. fillOpacity: 0.3, // 透明度
  94. fillColor: '#2887f2',
  95. strokeColor: '#2887f2' //边界颜色
  96. });
  97. this.polygons.push(polygon);
  98. }
  99. }
  100. this.map.add(this.polygons);
  101. this.map.setFitView(this.polygons); //视口自适应
  102. });
  103. },
  104. getAddress(points) {
  105. //获取准确地址
  106. let geocoder = new AMap.Geocoder({ radius: 1000 });
  107. geocoder.getAddress(points, (status, result) => {
  108. if (status === 'complete' && result.regeocode) {
  109. this.districtMask(result.regeocode.addressComponent.district);
  110. } else {
  111. this.map.remove(this.polygons);
  112. }
  113. });
  114. },
  115. getPoin() {
  116. this.$http.get('/sc-community/assets/community/list').then(({ data, status, msg }) => {
  117. if (status == 0) {
  118. this.poisArray = data;
  119. }
  120. });
  121. }
  122. },
  123. destroyed() {
  124. this.map && this.map.destroy();
  125. },
  126. created() {
  127. this.getPoin();
  128. new Promise((resolve) => {
  129. if (!!AMap) {
  130. resolve(true);
  131. }
  132. }).then(() => {
  133. this.init();
  134. });
  135. }
  136. };
  137. </script>
  138. <style scoped lang="scss">
  139. @import '@assets/css/public-style.scss';
  140. .mapItems {
  141. height: 100%;
  142. position: relative;
  143. .topSelect {
  144. position: absolute;
  145. top: rem(15);
  146. left: 0;
  147. padding: 0 rem(15);
  148. width: 100%;
  149. }
  150. }
  151. #mapInit {
  152. width: 100%;
  153. height: 100%;
  154. /deep/ .amap-marker-label {
  155. background: rgba(0, 0, 0, 0.5);
  156. text-align: center;
  157. min-width: rem(80);
  158. min-height: rem(20);
  159. line-height: rem(20);
  160. box-shadow: 0px 2px 10px 0px rgba(66, 70, 86, 0.08);
  161. }
  162. }
  163. </style>