mapdrag.vue 11 KB

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