mapdrag.vue 14 KB

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