warehouseReceiptMap.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <style lang='scss' scoped>
  2. .mapContent {
  3. position: relative;
  4. .center-add {
  5. position: absolute;
  6. top: 0;
  7. bottom: 0;
  8. left: 0;
  9. right: 0;
  10. margin: auto;
  11. z-index: 999;
  12. width: 30px;
  13. height: 30px;
  14. }
  15. }
  16. #mapContainer {
  17. width: 100%;
  18. height: 330px;
  19. }
  20. // #tip {
  21. // position: absolute;
  22. // top: -45px;
  23. // display: flex;
  24. // }
  25. // #tip .el-input {
  26. // // width: 179px;
  27. // margin-right: 0px;
  28. // }
  29. // #tip .el-select {
  30. // margin-right: 10px;
  31. // }
  32. // #tip .el-select {
  33. // width: 179px;
  34. // }
  35. // #tip .el-textarea {
  36. // width: 179px;
  37. // /deep/ .el-textarea__inner {
  38. // height: 40px;
  39. // resize: none;
  40. // }
  41. // }
  42. // .el-button--primary {
  43. // background-color: #5878e8;
  44. // border-color: #5878e8;
  45. // }
  46. // .el-button--default {
  47. // color: #5878e8;
  48. // border-color: #5473e8;
  49. // }
  50. // .address {
  51. // display: flex;
  52. // align-items: center;
  53. // margin-right: 20px;
  54. // // position:absolute;
  55. // // top: -35px;
  56. // .address-input {
  57. // display: inline-block;
  58. // width: 480px;
  59. // }
  60. // }
  61. </style>
  62. <template>
  63. <div class="mapContent">
  64. <div id="mapContainer"></div>
  65. <!-- <div id="tip" v-show="showFlag === false ? false : true">
  66. <el-select v-model="province" @change="selectP" placeholder="省">
  67. <el-option
  68. v-for="item in provinces"
  69. :key="item.adcode"
  70. :label="item.name"
  71. :value="item.adcode"
  72. >
  73. </el-option>
  74. </el-select>
  75. <el-select v-model="city" @change="selectC" placeholder="市">
  76. <el-option
  77. v-for="item in citys"
  78. :key="item.adcode"
  79. :label="item.name"
  80. :value="item.name"
  81. >
  82. </el-option>
  83. </el-select>
  84. <el-select v-model="district" @change="selectD" placeholder="区">
  85. <el-option
  86. v-for="item in districts"
  87. :key="item.adcode"
  88. :label="item.name"
  89. :value="item.adcode"
  90. >
  91. </el-option>
  92. </el-select>
  93. <div class="address">
  94. <el-input type="text" id="searchValue" placeholder="请输入关键字:" v-model="address" clearable class="address-input">
  95. </el-input>
  96. </div>
  97. <el-button>经纬度:{{center}}</el-button>
  98. <el-button @click="positions" type="primary" v-if='!isShowaddress'>确定定位</el-button>
  99. <el-button @click="anewselect" v-if='!isShowaddress'>重新选择</el-button>
  100. <el-button @click="submit" v-if='isShowaddress'>确定</el-button>
  101. </div> -->
  102. </div>
  103. </template>
  104. <script>
  105. import {
  106. regionData,
  107. CodeToText,
  108. TextToCode
  109. } from 'element-china-area-data'
  110. import image from '../../../public/img/ic_locationmarker.jpg'
  111. export default {
  112. data() {
  113. return {
  114. map: null,
  115. provinces: [],
  116. province: '',
  117. districts: [],
  118. district: '',
  119. citys: [],
  120. city: '',
  121. polygons: [],
  122. selectedOptions1: [],
  123. areacode: '',
  124. address: '',
  125. objPoint: [],
  126. markers: [],
  127. zoomEnable: true,
  128. dragEnable: true,
  129. status: true,
  130. center: '',
  131. }
  132. },
  133. props: ['flagVisible', 'position', 'selectedOptions', 'isShowaddress', 'type'],
  134. computed: {
  135. showFlag() {
  136. return this.flagVisible
  137. },
  138. },
  139. watch: {
  140. selectedOptions: {
  141. handler(newValue, oldValue) {
  142. this.province = CodeToText[newValue[0]]
  143. this.selectP(this.province)
  144. this.city = CodeToText[newValue[1]]
  145. this.selectC(CodeToText[newValue[1]])
  146. this.district = newValue[2]
  147. this.selectD(this.district)
  148. },
  149. deep: true
  150. }
  151. },
  152. async created() {
  153. // 已载入高德地图API,则直接初始化地图
  154. if (window.AMap && window.AMapUI) {
  155. this.initMap()
  156. // 未载入高德地图API,则先载入API再初始化
  157. } else {
  158. await remoteLoad(`http://webapi.amap.com/maps?v=1.3&key=${MapKey}`)
  159. await remoteLoad('http://webapi.amap.com/ui/1.0/main.js')
  160. this.initMap()
  161. }
  162. },
  163. mounted() {
  164. // console.log(this)
  165. setTimeout(() => {
  166. var that = this
  167. this.map = new AMap.Map('mapContainer', {
  168. resizeEnable: true,
  169. zoom: 10,
  170. zoomEnable: this.zoomEnable,
  171. dragEnable: this.dragEnable,
  172. })
  173. this.map.on('click', function(e) {
  174. if (this.status) {
  175. var marker = new AMap.Marker({
  176. position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat
  177. .getLat()), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  178. })
  179. that.map.remove(that.markers)
  180. that.$emit('marker', e)
  181. that.markers.push(marker)
  182. // 将创建的点标记添加到已有的地图实例:
  183. that.map.add(marker)
  184. }
  185. })
  186. this.marker = new AMap.Marker({
  187. icon: new AMap.Icon({
  188. size: new AMap.Size(0, 0),
  189. image: image,
  190. }),
  191. map: that.map,
  192. })
  193. AMap.plugin(
  194. ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Geocoder', 'AMap.DistrictSearch'],
  195. function() {
  196. var autocomplete = new AMap.Autocomplete({
  197. input: 'searchValue',
  198. })
  199. var placeSearch = new AMap.PlaceSearch({
  200. city: that.acode,
  201. citylimit: true,
  202. pageSize: 1,
  203. pageIndex: 1,
  204. map: that.map,
  205. })
  206. var districtSearch = new AMap.DistrictSearch({
  207. subdistrict: 1,
  208. level: 'city',
  209. showbiz: false,
  210. });
  211. AMap.event.addListener(autocomplete, 'select', function(e) {
  212. that.marker.setMap(that.map)
  213. if (e.poi.location) {
  214. placeSearch.search() // 关键字查询查询
  215. that.marker.setPosition(e.poi.location)
  216. geocoder.getAddress(e.poi.location, function(status, result) {
  217. console.log(e.poi.location, "klokolkjolk")
  218. if (status === 'complete') {
  219. var obj = {}
  220. that.province = result.regeocode.addressComponent.province
  221. that.city = result.regeocode.addressComponent.city
  222. that.district = result.regeocode.addressComponent.district
  223. obj.formattedAddress = result.regeocode.formattedAddress
  224. obj.areacode = result.regeocode.addressComponent.adcode
  225. that.address = result.regeocode.formattedAddress
  226. obj.lat = e.poi.location.lat
  227. obj.lng = e.poi.location.lng
  228. that.$emit('searchAddress', obj)
  229. } else {
  230. that.$emit('searchAddress', '')
  231. }
  232. that.map.setFitView()
  233. })
  234. } else {
  235. districtSearch.search(e.poi.name, (status, result1) => {
  236. if (status == 'complete') {
  237. that.marker.setPosition(result1.districtList[0].center)
  238. geocoder.getAddress(result1.districtList[0].center, function(status, result) {
  239. if (status === 'complete') {
  240. var obj = {}
  241. that.province = result.regeocode.addressComponent.province
  242. that.city = result.regeocode.addressComponent.city
  243. that.district = result.regeocode.addressComponent.district
  244. obj.formattedAddress = result.regeocode.formattedAddress
  245. obj.areacode = result.regeocode.addressComponent.adcode
  246. that.address = result.regeocode.formattedAddress
  247. obj.lat = result1.districtList[0].center.lat
  248. obj.lng = result1.districtList[0].center.lng
  249. that.$emit('searchAddress', obj)
  250. } else {
  251. that.$emit('searchAddress', '')
  252. }
  253. that.map.setFitView()
  254. })
  255. }
  256. })
  257. }
  258. })
  259. // 经纬度
  260. var geocoder = new AMap.Geocoder({
  261. radius: 1000,
  262. })
  263. that.map.on('moveend', function(e) {
  264. //地图变动之后需要重新赋值省市区,未完成
  265. that.center = that.map.getCenter(); //获取当前地图中心位置
  266. that.center.lat = Number(that.center.lat.toFixed(3))
  267. that.center.lng = Number(that.center.lng.toFixed(3))
  268. that.center.Q = Number(that.center.Q.toFixed(3))
  269. that.center.R = Number(that.center.R.toFixed(3))
  270. for (var i = 0, l = that.polygons.length; i < l; i++) {
  271. that.polygons[i].setMap(null)
  272. }
  273. that.marker.setMap(that.map)
  274. that.marker.setPosition(that.center)
  275. geocoder.getAddress(that.center, function(status, result) {
  276. if (status === 'complete') {
  277. var obj = {}
  278. that.province = result.regeocode.addressComponent.province
  279. that.city = result.regeocode.addressComponent.city
  280. that.district = result.regeocode.addressComponent.district
  281. obj.formattedAddress = result.regeocode.formattedAddress
  282. obj.areacode = result.regeocode.addressComponent.adcode
  283. that.address = result.regeocode.formattedAddress
  284. obj.lat = that.center.lat
  285. obj.lng = that.center.lng
  286. that.$emit('pickedAddress', obj)
  287. } else {
  288. that.$emit('pickedAddress', '')
  289. }
  290. })
  291. });
  292. that.map.on('zoomend', function() {
  293. //地图变动之后需要重新赋值省市区,未完成
  294. // this.zoom = map.getZoom(); //获取当前地图级别
  295. that.center = that.map.getCenter(); //获取当前地图中心位置
  296. that.center.lat = Number(that.center.lat.toFixed(3))
  297. that.center.lng = Number(that.center.lng.toFixed(3))
  298. that.center.Q = Number(that.center.Q.toFixed(3))
  299. that.center.R = Number(that.center.R.toFixed(3))
  300. for (var i = 0, l = that.polygons.length; i < l; i++) {
  301. that.polygons[i].setMap(null)
  302. }
  303. that.marker.setMap(that.map)
  304. that.marker.setPosition(that.center)
  305. geocoder.getAddress(that.center, function(status, result) {
  306. if (status === 'complete') {
  307. var obj = {}
  308. that.province = result.regeocode.addressComponent.province
  309. that.city = result.regeocode.addressComponent.city
  310. that.district = result.regeocode.addressComponent.district
  311. obj.formattedAddress = result.regeocode.formattedAddress
  312. obj.areacode = result.regeocode.addressComponent.adcode
  313. that.address = result.regeocode.formattedAddress
  314. obj.lat = that.center.lat
  315. obj.lng = that.center.lng
  316. that.$emit('pickedAddress', obj)
  317. } else {
  318. that.$emit('pickedAddress', '')
  319. }
  320. })
  321. });
  322. // that.map.on('click', function (e) {
  323. // for (var i = 0, l = that.polygons.length; i < l; i++) {
  324. // that.polygons[i].setMap(null)
  325. // }
  326. // that.marker.setMap(that.map)
  327. // that.marker.setPosition(e.lnglat)
  328. // geocoder.getAddress(e.lnglat, function (status, result) {
  329. // if (status === 'complete') {
  330. // var obj = {}
  331. // that.province = result.regeocode.addressComponent.province
  332. // that.city = result.regeocode.addressComponent.city
  333. // that.district = result.regeocode.addressComponent.district
  334. // obj.formattedAddress = result.regeocode.formattedAddress
  335. // obj.areacode = result.regeocode.addressComponent.adcode
  336. // that.address = result.regeocode.formattedAddress
  337. // obj.lat = e.lnglat.lat
  338. // obj.lng = e.lnglat.lng
  339. // that.$emit('pickedAddress', obj)
  340. // } else {
  341. // that.$emit('pickedAddress', '')
  342. // }
  343. // })
  344. // })
  345. // 根据坐标点定位,获取地址的详细信息
  346. if (that.position) {
  347. that.marker.setMap(that.map)
  348. that.marker.setPosition(that.position)
  349. geocoder.getAddress(that.position, function(status, result) {
  350. if (status === 'complete') {
  351. var obj = {}
  352. obj.formattedAddress = result.regeocode.formattedAddress
  353. obj.areacode = result.regeocode.addressComponent.adcode
  354. that.address = result.regeocode.formattedAddress
  355. that.$emit('positionAddress', obj)
  356. that.province = result.regeocode.addressComponent.province
  357. that.city = result.regeocode.addressComponent.city
  358. that.district = result.regeocode.addressComponent.district
  359. }
  360. })
  361. that.map.setFitView()
  362. } else {
  363. return false
  364. }
  365. }
  366. )
  367. }, 500)
  368. this.loadmap()
  369. },
  370. methods: {
  371. positions() {
  372. this.status = false
  373. var status = {
  374. zoomEnable: false,
  375. dragEnable: false,
  376. }
  377. this.map.setStatus(status)
  378. this.loadmap()
  379. let _data = []
  380. _data.push(this.province)
  381. _data.push(this.city)
  382. _data.push(this.district)
  383. _data.push(this.address)
  384. _data.push(this.type)
  385. this.$emit('addressListen', _data)
  386. },
  387. anewselect() {
  388. this.status = true
  389. var status = {
  390. zoomEnable: true,
  391. dragEnable: true,
  392. }
  393. this.map.setStatus(status)
  394. this.loadmap()
  395. },
  396. loadmap(val) {
  397. let params = val
  398. var that = this // 插件
  399. for (var j = 0, k = that.polygons.length; j < k; j++) {
  400. that.polygons[j].setMap(null)
  401. }
  402. AMap.plugin(['AMap.DistrictSearch', 'AMap.Polygon'], function() {
  403. // 标注
  404. var opts = {
  405. subdistrict: 1,
  406. extensions: 'all',
  407. showbiz: false,
  408. }
  409. var district = new AMap.DistrictSearch(opts) // 注意:需要使用插件同步下发功能才能这样直接使用
  410. function getData(data) {
  411. console.log(data)
  412. that.$emit('selectedAddress', data)
  413. // 清除地图上的所有覆盖物
  414. that.areacode = data.citycode
  415. var bounds = data.boundaries
  416. if (data.level === 'country') {
  417. that.provinces = data.districtList
  418. that.citys = []
  419. that.districts = []
  420. } else if (data.level === 'province') {
  421. that.map.remove(that.marker)
  422. that.citys = data.districtList
  423. that.districts = []
  424. that.address = ''
  425. } else if (data.level === 'city') {
  426. that.districts = data.districtList
  427. }
  428. if (data.level === 'country' && bounds) {
  429. return false
  430. } else {
  431. for (var i = 0, l = bounds.length; i < l; i++) {
  432. var polygon = new AMap.Polygon({
  433. map: that.map,
  434. strokeWeight: 1,
  435. strokeColor: '#CC66CC',
  436. fillColor: '#CCF3FF',
  437. fillOpacity: 0.5,
  438. path: bounds[i],
  439. })
  440. that.polygons.push(polygon)
  441. }
  442. that.map.setFitView()
  443. }
  444. }
  445. let sear = val ? params : '中国'
  446. district.search(sear, function(status, result) {
  447. if (status === 'complete') {
  448. getData(result.districtList[0])
  449. }
  450. })
  451. })
  452. },
  453. selectP(val) {
  454. this.province = CodeToText[val]
  455. if (this.status) {
  456. this.$emit('provinceChange', val)
  457. this.loadmap(val)
  458. this.city = ''
  459. this.district = ''
  460. }
  461. },
  462. selectC(val) {
  463. console.log(val)
  464. if (this.status) {
  465. this.loadmap(val)
  466. this.district = ''
  467. }
  468. },
  469. selectD(val) {
  470. this.district = CodeToText[val]
  471. if (this.status) {
  472. this.loadmap(val)
  473. }
  474. },
  475. submit() {
  476. let _data = []
  477. _data.push(this.province)
  478. _data.push(this.city)
  479. _data.push(this.district)
  480. _data.push(this.address)
  481. _data.push(this.type)
  482. this.$emit('addressListen', _data)
  483. this.$parent.blurMap()
  484. console.log("submit")
  485. }
  486. },
  487. }
  488. </script>