company-map.vue 8.9 KB

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