mapdrag.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <style lang='scss' scoped>
  2. .mapContent {
  3. position: relative;
  4. margin-top: 40px;
  5. }
  6. #mapContainer {
  7. width: 300px;
  8. height: 300px;
  9. }
  10. #tip {
  11. position: absolute;
  12. top: -60px;
  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. </style>
  32. <template>
  33. <div class="mapContent">
  34. <div id="mapContainer"></div>
  35. <div id="tip" v-show="showFlag === false ? false : true">
  36. <el-select v-model="province" @change="selectP" placeholder="省">
  37. <el-option
  38. v-for="item in provinces"
  39. :key="item.adcode"
  40. :label="item.name"
  41. :value="item.adcode"
  42. >
  43. </el-option>
  44. </el-select>
  45. <el-select v-model="city" @change="selectC" placeholder="市">
  46. <el-option
  47. v-for="item in citys"
  48. :key="item.adcode"
  49. :label="item.name"
  50. :value="item.name"
  51. >
  52. </el-option>
  53. </el-select>
  54. <el-select v-model="district" @change="selectD" placeholder="区">
  55. <el-option
  56. v-for="item in districts"
  57. :key="item.adcode"
  58. :label="item.name"
  59. :value="item.adcode"
  60. >
  61. </el-option>
  62. </el-select>
  63. <el-input
  64. type="textarea"
  65. id="searchValue"
  66. placeholder="地址"
  67. v-model="address"
  68. clearable
  69. ></el-input>
  70. </div>
  71. </div>
  72. </template>
  73. <script>
  74. import image from '../../../public/img/ic_locationmarker.jpg'
  75. export default {
  76. data() {
  77. return {
  78. map: null,
  79. provinces: [],
  80. province: '',
  81. districts: [],
  82. district: '',
  83. citys: [],
  84. city: '',
  85. polygons: [],
  86. areacode: '',
  87. address: '',
  88. objPoint: [],
  89. markers: [],
  90. }
  91. },
  92. props: ['flagVisible', 'position'],
  93. computed: {
  94. showFlag() {
  95. return this.flagVisible
  96. },
  97. },
  98. async created() {
  99. // 已载入高德地图API,则直接初始化地图
  100. if (window.AMap && window.AMapUI) {
  101. this.initMap()
  102. // 未载入高德地图API,则先载入API再初始化
  103. } else {
  104. await remoteLoad(`http://webapi.amap.com/maps?v=1.3&key=${MapKey}`)
  105. await remoteLoad('http://webapi.amap.com/ui/1.0/main.js')
  106. this.initMap()
  107. }
  108. },
  109. mounted() {
  110. setTimeout(() => {
  111. var that = this
  112. this.map = new AMap.Map('mapContainer', {
  113. resizeEnable: true,
  114. zoom: 10,
  115. })
  116. this.map.on('click', function (e) {
  117. var marker = new AMap.Marker({
  118. position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  119. })
  120. that.map.remove(that.markers)
  121. that.$emit('marker', e)
  122. that.markers.push(marker)
  123. // 将创建的点标记添加到已有的地图实例:
  124. that.map.add(marker)
  125. })
  126. this.marker = new AMap.Marker({
  127. icon: new AMap.Icon({
  128. size: new AMap.Size(36, 44),
  129. image: image,
  130. }),
  131. map: that.map,
  132. })
  133. AMap.plugin(
  134. ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Geocoder'],
  135. function () {
  136. var autocomplete = new AMap.Autocomplete({
  137. input: 'searchValue',
  138. })
  139. var placeSearch = new AMap.PlaceSearch({
  140. city: that.acode,
  141. citylimit: true,
  142. pageSize: 1,
  143. pageIndex: 1,
  144. map: that.map,
  145. })
  146. AMap.event.addListener(autocomplete, 'select', function (e) {
  147. that.marker.setMap(that.map)
  148. that.marker.setPosition(e.poi.location)
  149. placeSearch.search() // 关键字查询查询
  150. geocoder.getAddress(e.poi.location, function (status, result) {
  151. console.log(status)
  152. if (status === 'complete') {
  153. var obj = {}
  154. that.province = result.regeocode.addressComponent.province
  155. that.city = result.regeocode.addressComponent.city
  156. that.district = result.regeocode.addressComponent.district
  157. obj.formattedAddress = result.regeocode.formattedAddress
  158. obj.areacode = result.regeocode.addressComponent.adcode
  159. that.address = result.regeocode.formattedAddress
  160. obj.lat = e.poi.location.lat
  161. obj.lng = e.poi.location.lng
  162. that.$emit('searchAddress', obj)
  163. } else {
  164. that.$emit('searchAddress', '')
  165. }
  166. })
  167. that.map.setFitView()
  168. })
  169. // 经纬度
  170. var geocoder = new AMap.Geocoder({
  171. radius: 1000,
  172. })
  173. that.map.on('click', function (e) {
  174. for (var i = 0, l = that.polygons.length; i < l; i++) {
  175. that.polygons[i].setMap(null)
  176. }
  177. that.marker.setMap(that.map)
  178. that.marker.setPosition(e.lnglat)
  179. geocoder.getAddress(e.lnglat, function (status, result) {
  180. if (status === 'complete') {
  181. var obj = {}
  182. that.province = result.regeocode.addressComponent.province
  183. that.city = result.regeocode.addressComponent.city
  184. that.district = result.regeocode.addressComponent.district
  185. obj.formattedAddress = result.regeocode.formattedAddress
  186. obj.areacode = result.regeocode.addressComponent.adcode
  187. that.address = result.regeocode.formattedAddress
  188. obj.lat = e.lnglat.lat
  189. obj.lng = e.lnglat.lng
  190. that.$emit('pickedAddress', obj)
  191. } else {
  192. that.$emit('pickedAddress', '')
  193. }
  194. })
  195. })
  196. // 根据坐标点定位,获取地址的详细信息
  197. if (that.position) {
  198. that.marker.setMap(that.map)
  199. that.marker.setPosition(that.position)
  200. geocoder.getAddress(that.position, function (status, result) {
  201. if (status === 'complete') {
  202. var obj = {}
  203. obj.formattedAddress = result.regeocode.formattedAddress
  204. obj.areacode = result.regeocode.addressComponent.adcode
  205. that.address = result.regeocode.formattedAddress
  206. that.$emit('positionAddress', obj)
  207. that.province = result.regeocode.addressComponent.province
  208. that.city = result.regeocode.addressComponent.city
  209. that.district = result.regeocode.addressComponent.district
  210. }
  211. })
  212. that.map.setFitView()
  213. } else {
  214. return false
  215. }
  216. }
  217. )
  218. }, 500)
  219. this.loadmap()
  220. },
  221. methods: {
  222. loadmap(val) {
  223. let params = val
  224. var that = this // 插件
  225. for (var j = 0, k = that.polygons.length; j < k; j++) {
  226. that.polygons[j].setMap(null)
  227. }
  228. AMap.plugin(['AMap.DistrictSearch', 'AMap.Polygon'], function () {
  229. // 标注
  230. var opts = {
  231. subdistrict: 1,
  232. extensions: 'all',
  233. showbiz: false,
  234. }
  235. var district = new AMap.DistrictSearch(opts) // 注意:需要使用插件同步下发功能才能这样直接使用
  236. function getData(data) {
  237. that.$emit('selectedAddress', data)
  238. // 清除地图上的所有覆盖物
  239. that.areacode = data.citycode
  240. var bounds = data.boundaries
  241. if (data.level === 'country') {
  242. that.provinces = data.districtList
  243. that.citys = []
  244. that.districts = []
  245. } else if (data.level === 'province') {
  246. that.map.remove(that.marker)
  247. that.citys = data.districtList
  248. that.districts = []
  249. that.address = ''
  250. } else if (data.level === 'city') {
  251. that.districts = data.districtList
  252. }
  253. if (data.level === 'country' && bounds) {
  254. return false
  255. } else {
  256. for (var i = 0, l = bounds.length; i < l; i++) {
  257. var polygon = new AMap.Polygon({
  258. map: that.map,
  259. strokeWeight: 1,
  260. strokeColor: '#CC66CC',
  261. fillColor: '#CCF3FF',
  262. fillOpacity: 0.5,
  263. path: bounds[i],
  264. })
  265. that.polygons.push(polygon)
  266. }
  267. that.map.setFitView()
  268. }
  269. }
  270. let sear = val ? params : '中国'
  271. district.search(sear, function (status, result) {
  272. if (status === 'complete') {
  273. getData(result.districtList[0])
  274. }
  275. })
  276. })
  277. },
  278. selectP(val) {
  279. this.loadmap(val)
  280. this.city = ''
  281. this.district = ''
  282. },
  283. selectC(val) {
  284. this.loadmap(val)
  285. this.district = ''
  286. },
  287. selectD(val) {
  288. this.loadmap(val)
  289. },
  290. },
  291. }
  292. </script>