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. import AMapLoader from '@amap/amap-jsapi-loader';
  114. export default {
  115. data() {
  116. return {
  117. map: null,
  118. provinces: [],
  119. province: '',
  120. districts: [],
  121. district: '',
  122. citys: [],
  123. city: '',
  124. polygons: [],
  125. selectedOptions1: [],
  126. areacode: '',
  127. address: '',
  128. objPoint: [],
  129. markers: [],
  130. zoomEnable: true,
  131. dragEnable: true,
  132. status: true,
  133. center: '',
  134. }
  135. },
  136. props: ['flagVisible', 'position', 'selectedOptions', 'isShowaddress', 'type'],
  137. computed: {
  138. showFlag() {
  139. return this.flagVisible
  140. },
  141. },
  142. watch: {
  143. selectedOptions: {
  144. handler(newValue, oldValue) {
  145. this.province = CodeToText[newValue[0]]
  146. this.selectP(this.province)
  147. this.city = CodeToText[newValue[1]]
  148. this.selectC(CodeToText[newValue[1]])
  149. this.district = newValue[2]
  150. this.selectD(this.district)
  151. },
  152. deep: true
  153. }
  154. },
  155. async created() {
  156. },
  157. mounted() {
  158. // AMapLoader.reset();
  159. // 已载入高德地图API,则直接初始化地图
  160. if (window.AMap && window.AMapUI) {
  161. // 未载入高德地图API,则先载入API再初始化
  162. } else {
  163. // await remoteLoad(`http://webapi.amap.com/maps?v=1.3&key=${MapKey}&plugin=AMap.AutoComplete`)
  164. // await remoteLoad('http://webapi.amap.com/ui/1.0/main.js')
  165. // AMapLoader.reset();
  166. AMapLoader.load({
  167. "key": "211dd6f989e719022aaf47ddb0659c47", // 申请好的Web端开发者Key,首次调用 load 时必填
  168. "version": "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  169. "plugins": ['plugin=AMap.Scale', 'AMap.ToolBar','AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.Geocoder', 'AMap.DistrictSearch','AMap.Geolocation', 'Geolocation'],
  170. "Loca": { // 是否加载 Loca, 缺省不加载
  171. "version": '2.0.0' // Loca 版本,缺省 1.3.2
  172. },
  173. }).then((AMap) => {
  174. setTimeout(() => {
  175. var that = this
  176. this.map = new AMap.Map('mapContainer', {
  177. resizeEnable: true,
  178. zoom: 10,
  179. zoomEnable: this.zoomEnable,
  180. dragEnable: this.dragEnable,
  181. })
  182. this.map.on('click', function(e) {
  183. if (this.status) {
  184. var marker = new AMap.Marker({
  185. position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat
  186. .getLat()), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  187. })
  188. that.map.remove(that.markers)
  189. that.$emit('marker', e)
  190. that.markers.push(marker)
  191. // 将创建的点标记添加到已有的地图实例:
  192. that.map.add(marker)
  193. }
  194. })
  195. this.marker = new AMap.Marker({
  196. icon: new AMap.Icon({
  197. size: new AMap.Size(0, 0),
  198. image: image,
  199. }),
  200. map: that.map,
  201. })
  202. AMap.plugin(
  203. ['AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.Geocoder', 'AMap.DistrictSearch'],
  204. function() {
  205. var autocomplete = new AMap.AutoComplete({
  206. input: 'searchValue',
  207. })
  208. var placeSearch = new AMap.PlaceSearch({
  209. city: that.acode,
  210. citylimit: true,
  211. pageSize: 1,
  212. pageIndex: 1,
  213. map: that.map,
  214. })
  215. var districtSearch = new AMap.DistrictSearch({
  216. subdistrict: 1,
  217. level: 'city',
  218. showbiz: false,
  219. });
  220. autocomplete.on( 'select', function(e) {
  221. that.marker.setMap(that.map)
  222. if (e.poi.location) {
  223. placeSearch.search() // 关键字查询查询
  224. that.marker.setPosition(e.poi.location)
  225. geocoder.getAddress(e.poi.location, function(status, result) {
  226. console.log(e.poi.location, "klokolkjolk")
  227. if (status === 'complete') {
  228. var obj = {}
  229. that.province = result.regeocode.addressComponent.province
  230. that.city = result.regeocode.addressComponent.city
  231. that.district = result.regeocode.addressComponent.district
  232. obj.formattedAddress = result.regeocode.formattedAddress
  233. obj.areacode = result.regeocode.addressComponent.adcode
  234. that.address = result.regeocode.formattedAddress
  235. obj.lat = e.poi.location.lat
  236. obj.lng = e.poi.location.lng
  237. that.$emit('searchAddress', obj)
  238. } else {
  239. that.$emit('searchAddress', '')
  240. }
  241. that.map.setFitView()
  242. })
  243. } else {
  244. districtSearch.search(e.poi.name, (status, result1) => {
  245. if (status == 'complete') {
  246. that.marker.setPosition(result1.districtList[0].center)
  247. geocoder.getAddress(result1.districtList[0].center, function(status, result) {
  248. if (status === 'complete') {
  249. var obj = {}
  250. that.province = result.regeocode.addressComponent.province
  251. that.city = result.regeocode.addressComponent.city
  252. that.district = result.regeocode.addressComponent.district
  253. obj.formattedAddress = result.regeocode.formattedAddress
  254. obj.areacode = result.regeocode.addressComponent.adcode
  255. that.address = result.regeocode.formattedAddress
  256. obj.lat = result1.districtList[0].center.lat
  257. obj.lng = result1.districtList[0].center.lng
  258. that.$emit('searchAddress', obj)
  259. } else {
  260. that.$emit('searchAddress', '')
  261. }
  262. that.map.setFitView()
  263. })
  264. }
  265. })
  266. }
  267. })
  268. // 经纬度
  269. var geocoder = new AMap.Geocoder({
  270. radius: 1000,
  271. })
  272. that.map.on('moveend', function(e) {
  273. //地图变动之后需要重新赋值省市区,未完成
  274. that.center = that.map.getCenter(); //获取当前地图中心位置
  275. console.log(that.center)
  276. that.center.lat = Number(that.center.lat.toFixed(3))
  277. that.center.lng = Number(that.center.lng.toFixed(3))
  278. for (var i = 0, l = that.polygons.length; i < l; i++) {
  279. that.polygons[i].setMap(null)
  280. }
  281. that.marker.setMap(that.map)
  282. that.marker.setPosition(that.center)
  283. geocoder.getAddress(that.center, function(status, result) {
  284. if (status === 'complete') {
  285. var obj = {}
  286. that.province = result.regeocode.addressComponent.province
  287. that.city = result.regeocode.addressComponent.city
  288. that.district = result.regeocode.addressComponent.district
  289. obj.formattedAddress = result.regeocode.formattedAddress
  290. obj.areacode = result.regeocode.addressComponent.adcode
  291. that.address = result.regeocode.formattedAddress
  292. obj.lat = that.center.lat
  293. obj.lng = that.center.lng
  294. that.$emit('pickedAddress', obj)
  295. } else {
  296. that.$emit('pickedAddress', '')
  297. }
  298. })
  299. });
  300. that.map.on('zoomend', function() {
  301. //地图变动之后需要重新赋值省市区,未完成
  302. // this.zoom = map.getZoom(); //获取当前地图级别
  303. that.center = that.map.getCenter(); //获取当前地图中心位置
  304. console.log(that.center)
  305. that.center.lat = Number(that.center.lat.toFixed(3))
  306. that.center.lng = Number(that.center.lng.toFixed(3))
  307. for (var i = 0, l = that.polygons.length; i < l; i++) {
  308. that.polygons[i].setMap(null)
  309. }
  310. that.marker.setMap(that.map)
  311. that.marker.setPosition(that.center)
  312. geocoder.getAddress(that.center, function(status, result) {
  313. if (status === 'complete') {
  314. var obj = {}
  315. that.province = result.regeocode.addressComponent.province
  316. that.city = result.regeocode.addressComponent.city
  317. that.district = result.regeocode.addressComponent.district
  318. obj.formattedAddress = result.regeocode.formattedAddress
  319. obj.areacode = result.regeocode.addressComponent.adcode
  320. that.address = result.regeocode.formattedAddress
  321. obj.lat = that.center.lat
  322. obj.lng = that.center.lng
  323. that.$emit('pickedAddress', obj)
  324. } else {
  325. that.$emit('pickedAddress', '')
  326. }
  327. })
  328. });
  329. // 根据坐标点定位,获取地址的详细信息
  330. if (that.position) {
  331. that.marker.setMap(that.map)
  332. that.marker.setPosition(that.position)
  333. geocoder.getAddress(that.position, function(status, result) {
  334. if (status === 'complete') {
  335. var obj = {}
  336. obj.formattedAddress = result.regeocode.formattedAddress
  337. obj.areacode = result.regeocode.addressComponent.adcode
  338. that.address = result.regeocode.formattedAddress
  339. that.$emit('positionAddress', obj)
  340. that.province = result.regeocode.addressComponent.province
  341. that.city = result.regeocode.addressComponent.city
  342. that.district = result.regeocode.addressComponent.district
  343. }
  344. })
  345. that.map.setFitView()
  346. } else {
  347. return false
  348. }
  349. }
  350. )
  351. }, 500)
  352. this.loadmap()
  353. // this.loadmap()
  354. }).catch(e => {
  355. console.log(e);
  356. })
  357. }
  358. },
  359. methods: {
  360. clearVal () {
  361. this.address = ''
  362. this.center=''
  363. var status = {
  364. zoomEnable: true,
  365. dragEnable: true,
  366. }
  367. this.map.setStatus(status)
  368. this.loadmap()
  369. },
  370. positions() {
  371. this.status = false
  372. var status = {
  373. zoomEnable: false,
  374. dragEnable: false,
  375. }
  376. this.map.setStatus(status)
  377. this.loadmap()
  378. let _data = []
  379. _data.push(this.province)
  380. _data.push(this.city)
  381. _data.push(this.district)
  382. _data.push(this.address)
  383. _data.push(this.type)
  384. this.$emit('addressListen', _data)
  385. },
  386. anewselect() {
  387. this.status = true
  388. var status = {
  389. zoomEnable: true,
  390. dragEnable: true,
  391. }
  392. this.map.setStatus(status)
  393. this.loadmap()
  394. },
  395. loadmap(val) {
  396. let params = val
  397. var that = this // 插件
  398. for (var j = 0, k = that.polygons.length; j < k; j++) {
  399. that.polygons[j].setMap(null)
  400. }
  401. AMap.plugin(['AMap.DistrictSearch', 'AMap.Polygon'], function() {
  402. // 标注
  403. var opts = {
  404. subdistrict: 1,
  405. extensions: 'all',
  406. showbiz: false,
  407. }
  408. var district = new AMap.DistrictSearch(opts) // 注意:需要使用插件同步下发功能才能这样直接使用
  409. function getData(data) {
  410. console.log(data)
  411. that.$emit('selectedAddress', data)
  412. // 清除地图上的所有覆盖物
  413. that.areacode = data.citycode
  414. var bounds = data.boundaries
  415. if (data.level === 'country') {
  416. that.provinces = data.districtList
  417. that.citys = []
  418. that.districts = []
  419. } else if (data.level === 'province') {
  420. that.map.remove(that.marker)
  421. that.citys = data.districtList
  422. that.districts = []
  423. that.address = ''
  424. } else if (data.level === 'city') {
  425. that.districts = data.districtList
  426. }
  427. if (data.level === 'country' && bounds) {
  428. return false
  429. } else {
  430. for (var i = 0, l = bounds.length; i < l; i++) {
  431. var polygon = new AMap.Polygon({
  432. map: that.map,
  433. strokeWeight: 1,
  434. strokeColor: '#CC66CC',
  435. fillColor: '#CCF3FF',
  436. fillOpacity: 0.5,
  437. path: bounds[i],
  438. })
  439. that.polygons.push(polygon)
  440. }
  441. that.map.setFitView()
  442. }
  443. }
  444. let sear = val ? params : '中国'
  445. district.search(sear, function(status, result) {
  446. if (status === 'complete') {
  447. getData(result.districtList[0])
  448. }
  449. })
  450. })
  451. },
  452. selectP(val) {
  453. this.province = CodeToText[val]
  454. if (this.status) {
  455. this.$emit('provinceChange', val)
  456. this.loadmap(val)
  457. this.city = ''
  458. this.district = ''
  459. }
  460. },
  461. selectC(val) {
  462. console.log(val)
  463. if (this.status) {
  464. this.loadmap(val)
  465. this.district = ''
  466. }
  467. },
  468. selectD(val) {
  469. this.district = CodeToText[val]
  470. if (this.status) {
  471. this.loadmap(val)
  472. }
  473. },
  474. submit() {
  475. let _data = []
  476. _data.push(this.province)
  477. _data.push(this.city)
  478. _data.push(this.district)
  479. _data.push(this.address)
  480. _data.push(this.type)
  481. _data.push(this.center)
  482. this.$emit('addressListen', _data)
  483. this.$parent.blurMap()
  484. console.log("submit")
  485. }
  486. },
  487. }
  488. </script>