mapdrag.vue 17 KB

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