company-map.vue 8.7 KB

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