company-map.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <template>
  2. <el-dialog v-model="mapPop" title="地图定位" width="80%" @close="handleClose">
  3. <div class="map_box">
  4. <div class="map" id="map"></div>
  5. <div class="search">
  6. <!-- @input="handleInput(0)" -->
  7. <el-input placeholder="请输入地址" v-model="location" />
  8. <el-button class="btn" type="primary" @click="handleInput(0)">搜索</el-button>
  9. </div>
  10. <div class="scroll_box" v-if="searchPop">
  11. <div style="height: 30px; line-height: 30px;">
  12. <span style="font-weight:bold">搜索结果列表</span>
  13. <i class="el-icon-close" style="float: right;font-size: 20px;cursor: pointer;" @click="closeSearchList()" />
  14. </div>
  15. <el-scrollbar class="scroll" style="height: 250px;">
  16. <div v-show="searchList.length" class="item" v-for="(item, index) in searchList" :key="index"
  17. @click="handlePanTo(index)">
  18. <el-image class="img" :src="item.img" :alt="item.name" lazy>
  19. <div slot="error" class="image-slot">
  20. <i class="el-icon-picture-outline"></i>
  21. </div>
  22. </el-image>
  23. <div>
  24. <div class="text">{{ item.name }}</div>
  25. <div>{{ item.address }}</div>
  26. </div>
  27. </div>
  28. <div class="empty" v-show="!searchList.length" style="text-align: center;">没有搜索到内容</div>
  29. </el-scrollbar>
  30. <el-pagination background small :hide-on-single-page="true" layout="prev, pager, next" :total="total"
  31. :page-size="pageSize" :current-page="pageNum" style="margin-top: 10px;"
  32. @current-change="handleChangePage">
  33. </el-pagination>
  34. </div>
  35. </div>
  36. <template #footer>
  37. <div class="dialog-footer">
  38. <el-button @click="handleClose">取 消</el-button>
  39. <el-button type="primary" @click="sureMark">确 定</el-button>
  40. </div>
  41. </template>
  42. </el-dialog>
  43. </template>
  44. <script>
  45. import AMapLoader from '@amap/amap-jsapi-loader';
  46. export default {
  47. props: {
  48. address: {//公司地址
  49. type: String,
  50. default: () => {
  51. return '';
  52. }
  53. },
  54. latAndLong: {//经纬度
  55. type: Array,
  56. default: () => {
  57. return [];
  58. }
  59. },
  60. visible: {
  61. type: Boolean,
  62. default: () => {
  63. return false;
  64. }
  65. }
  66. },
  67. data() {
  68. return {
  69. // 地图对象
  70. map: {},
  71. amap: {},
  72. location: '',
  73. marker: null,//地图上的点标记
  74. contextMenu: null,
  75. lnglatPosition: null,//选中的新坐标
  76. pageNum: 1,
  77. pageSize: 10,
  78. total: 0,
  79. searchList: [],
  80. searchPop: false,
  81. placeSearch: null,
  82. form: {},
  83. open: false,
  84. mapPop: false,
  85. geocoder: {}
  86. };
  87. },
  88. watch: {
  89. async visible(n) {
  90. this.mapPop = n;
  91. if (n) {
  92. await this.initMap();
  93. this.location = this.address;
  94. // this.handleInput(0);
  95. }
  96. }
  97. },
  98. destroyed() {
  99. if (this.map) {
  100. this.map.destroy();
  101. this.map.off('rightclick');
  102. }
  103. },
  104. methods: {
  105. handleInput(flag) {
  106. if (!this.location) return;
  107. if (!flag) {//搜索
  108. this.total = 0;
  109. this.pageNum = 1;
  110. }
  111. const that = this;
  112. if (!this.placeSearch) {
  113. this.placeSearch = new this.amap.PlaceSearch({
  114. pageSize: 10, // 每页条数,默认10,范围1-50
  115. pageIndex: 1, // 页码
  116. extensions: 'all' // 默认base,返回基本地址信息;all:返回详细信息
  117. });
  118. }
  119. this.searchPop = true;
  120. this.placeSearch.setPageIndex(this.pageNum);
  121. this.placeSearch.search(that.location, (status, result) => {
  122. // console.log(result.poiList.pois, 'result')
  123. if (!!result.poiList && result.poiList.pois && result.poiList.pois.length > 0) {
  124. let arr = [];
  125. const pois = result.poiList.pois;
  126. that.total = result.poiList ? result.poiList.count : 0;
  127. arr = pois.map((item) => {
  128. return {
  129. name: item.name,
  130. address: item.address,
  131. img: item.photos[0]?.url,
  132. lnglat: [item.location.lng, item.location.lat]
  133. };
  134. });
  135. that.searchList = arr;
  136. } else {
  137. that.total = 0;
  138. that.searchList = [];
  139. }
  140. });
  141. },
  142. handleChangePage(newNum) {
  143. if (!this.searchPop) return;
  144. this.pageNum = newNum;
  145. this.handleInput(1);
  146. },
  147. closeSearchList() {
  148. this.searchPop = false;
  149. this.location = '';
  150. this.searchList = [];
  151. this.total = 0;
  152. this.pageNum = 1;
  153. },
  154. // 地图中心的平移至指定点位置
  155. handlePanTo(index) {
  156. let lnglat = this.searchList[index].lnglat;
  157. this.form = {
  158. longitude: lnglat[0],
  159. latitude: lnglat[1]
  160. };
  161. this.map.panTo(lnglat);
  162. this.setMarks(lnglat);
  163. this.closeSearchList();
  164. },
  165. async initMap() {
  166. let position = this.latAndLong.length ? this.latAndLong : [110.925175, 21.678955];
  167. this.form = {
  168. longitude: position[0],
  169. latitude: position[1]
  170. };
  171. window._AMapSecurityConfig = {
  172. securityJsCode: '4868bc1b8fac7d9e54e7279ed556879a'
  173. };
  174. const AMap = await AMapLoader.load({
  175. key: '9c5041381e5e824f9ee324d8f7a40150', // 申请好的Web端开发者Key,首次调用 load 时必填
  176. version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  177. plugins: ['AMap.PlaceSearch', 'AMap.ContextMenu', 'AMap.PolygonEditor', 'AMap.Geocoder'] // 插件列表
  178. });
  179. this.map = new AMap.Map('map', {
  180. viewMode: '3D', //是否为3D地图模式
  181. center: position,
  182. zoom: 15
  183. });
  184. this.amap = AMap;
  185. this.setMarks(position);
  186. this.geocoder = new AMap.Geocoder({
  187. // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
  188. city: '010'
  189. });
  190. // 创建右键菜单
  191. this.ContextMenu();
  192. this.map.on('rightclick', this.handleRightclick);
  193. },
  194. ContextMenu() {
  195. this.contextMenu = new AMap.ContextMenu();
  196. this.contextMenu.addItem('选择标点', () => {
  197. this.form = {
  198. longitude: this.lnglatPosition[0],
  199. latitude: this.lnglatPosition[1]
  200. };
  201. this.contextMenu.close();
  202. let lnglat = [this.form.longitude, this.form.latitude];
  203. this.setMarks(lnglat);
  204. }, 1);
  205. },
  206. // 右键事件
  207. handleRightclick(e) {
  208. let lnglat = [e.lnglat.getLng(), e.lnglat.getLat()];
  209. this.contextMenu.open(this.map, e.lnglat);
  210. this.lnglatPosition = lnglat;
  211. },
  212. sureMark() {
  213. let lnglat = [this.form.longitude, this.form.latitude];
  214. this.geocoder.getAddress(lnglat, (status, result) => {
  215. if (status === 'complete' && result.info === 'OK') {
  216. // result为对应的地理位置详细信息
  217. this.$emit('confirm', { lnglat: lnglat, address: result.regeocode.formattedAddress });
  218. }
  219. });
  220. },
  221. setMarks(lnglat) {//添加标记
  222. if (this.marker) this.map.remove(this.marker);
  223. let marker = new AMap.Marker({
  224. position: lnglat,
  225. icon: new AMap.Icon({
  226. size: new AMap.Size(22, 28), //图标所处区域大小
  227. imageSize: new AMap.Size(22, 28), //图标大小
  228. image: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png'
  229. }),
  230. anchor: 'bottom-center',
  231. offset: new AMap.Pixel(0, 0)
  232. });
  233. marker.setMap(this.map);
  234. this.marker = marker;
  235. },
  236. handleClose() {
  237. this.$emit('update:visible', false);
  238. }
  239. }
  240. };
  241. </script>
  242. <style lang="scss" scoped>
  243. .map_box {
  244. position: relative;
  245. width: 100%;
  246. height: 450px;
  247. background: rgba(0, 0, 0, 0.3);
  248. margin-bottom: 20px;
  249. }
  250. .map {
  251. width: 100%;
  252. height: 100%;
  253. }
  254. .search {
  255. width: 50%;
  256. position: absolute;
  257. right: 2%;
  258. top: 10px;
  259. background: #fff;
  260. padding: 8px 8px;
  261. border-radius: 3px;
  262. display: flex;
  263. }
  264. .btn {
  265. margin-left: 10px;
  266. }
  267. .scroll_box {
  268. width: 50%;
  269. padding-top: 30px;
  270. padding-bottom: 20px;
  271. background: #fff;
  272. position: absolute;
  273. right: 2%;
  274. top: 70px;
  275. padding: 15px;
  276. border-radius: 3px;
  277. .close {
  278. position: absolute;
  279. right: 2%;
  280. top: 10px;
  281. cursor: pointer;
  282. font-size: 20px;
  283. }
  284. }
  285. .scroll {
  286. width: 100%;
  287. max-height: 250px;
  288. .item {
  289. display: flex;
  290. font-size: 14px;
  291. cursor: pointer;
  292. padding: 8px;
  293. &:hover {
  294. background-color: #f6f6f6;
  295. }
  296. .img {
  297. width: 50px;
  298. height: 45px;
  299. min-width: 50px;
  300. margin-right: 10px;
  301. }
  302. :deep(.image-slot) {
  303. width: 100%;
  304. height: 100%;
  305. background-color: #f5f7fa;
  306. text-align: center;
  307. line-height: 50px;
  308. }
  309. :deep(.el-icon-picture-outline) {
  310. font-size: 25px;
  311. }
  312. .text {
  313. color: #3385ff;
  314. margin-bottom: 6px;
  315. }
  316. }
  317. }
  318. :deep(.el-scrollbar__wrap) {
  319. overflow-x: hidden !important;
  320. }
  321. :deep(.is-horizontal) {
  322. display: none;
  323. }
  324. .empty {
  325. margin: 20px 0;
  326. }
  327. </style>