index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <div class="app-container map-contain">
  3. <el-dialog title="选择坐标" :visible.sync="mapOpen" @close="handleClose" width="1200px">
  4. <div>
  5. <div>
  6. <el-form ref="form" :model="form" label-width="50px">
  7. <el-row>
  8. <el-col :span="6" >
  9. <el-form-item label="地址" prop="lng">
  10. <el-input v-model="form.address" readonly="readonly" />
  11. </el-form-item>
  12. </el-col>
  13. <el-col :span="6" >
  14. <el-form-item label="经度" prop="lng">
  15. <el-input v-model="form.lng" />
  16. </el-form-item>
  17. </el-col>
  18. <el-col :span="6" >
  19. <el-form-item label="纬度" prop="lat">
  20. <el-input v-model="form.lat" />
  21. </el-form-item>
  22. </el-col>
  23. </el-row>
  24. </el-form>
  25. <div class="map-coord">
  26. <el-select
  27. class="address-select"
  28. v-model="searchValue"
  29. filterable
  30. remote
  31. value-key="id"
  32. placeholder="请输入关键词"
  33. :remote-method="remoteMethod"
  34. @change="changeArea"
  35. :loading="loading">
  36. <el-option
  37. v-for="item in areaOptions"
  38. :key="item.id"
  39. :label="item.title"
  40. :value="item.location"
  41. />
  42. </el-select>
  43. <div v-if="mapOpen" id="mapCoord"></div>
  44. </div>
  45. </div>
  46. <div id="container"></div>
  47. </div>
  48. <div slot="footer" class="dialog-footer">
  49. <el-button type="primary" @click="closeMap">确 定</el-button>
  50. </div>
  51. </el-dialog>
  52. </div>
  53. </template>
  54. <script>
  55. import markerDefault from '@/assets/images/markerDefault.png';
  56. export default {
  57. data() {
  58. return {
  59. map: null, // 地图实例
  60. markerDefault: markerDefault, // 默认图片
  61. mapOpen: false,
  62. markerLayer: null,
  63. form: {
  64. address: '',
  65. lat: 36.060249948,
  66. lng: 103.826524828,
  67. },
  68. searchValue: '', // 地图搜索值
  69. loading: false,
  70. areaOptions: [] // 地图搜索地址
  71. }
  72. },
  73. methods: {
  74. init() {
  75. this.form.address = ''
  76. // 默认省政府
  77. this.form.lat = 36.060249948
  78. this.form.lng = 103.826524828
  79. this.form.map = null
  80. this.form.markerLayer = null
  81. this.mapOpen = true
  82. this.$nextTick(() => {
  83. let that = this
  84. // 初始化地图
  85. this.map = new TMap.Map('mapCoord', {
  86. center: new TMap.LatLng(this.form.lat,this.form.lng), // 地图显示中心点
  87. zoom:18 // 缩放级别
  88. });
  89. this.getAddress()
  90. this.creatPoint()
  91. this.map.on("click",function(evt){
  92. if(evt.poi) {
  93. that.form.address = evt.poi.name
  94. } else {
  95. that.form.address = ''
  96. }
  97. that.form.lat = evt.latLng.getLat().toFixed(6);
  98. that.form.lng= evt.latLng.getLng().toFixed(6);
  99. that.getAddress()
  100. that.creatPoint()
  101. })
  102. })
  103. },
  104. // 逆地址解析
  105. getAddress() {
  106. let location = this.form.lat + ',' + this.form.lng
  107. this.$jsonp("https://apis.map.qq.com/ws/geocoder/v1/", {
  108. location: location,
  109. key: "UZQBZ-SYQL3-LYF3K-Y7FAL-N3656-2DBJ4",
  110. output:"jsonp"
  111. })
  112. .then(res => {
  113. // 结合知名地点形成的描述性地址,更具人性化特点
  114. this.form.address = res.result.formatted_addresses.recommend
  115. })
  116. },
  117. // 绘点
  118. creatPoint(){
  119. if (this.markerLayer) {
  120. this.markerLayer.setMap(null);
  121. this.markerLayer = null;
  122. }
  123. this.markerLayer = new TMap.MultiMarker({
  124. map: this.map, //指定地图容器
  125. //样式定义
  126. styles: {
  127. //创建一个styleId为"myStyle"的样式(styles的子属性名即为styleId)
  128. "myStyle": new TMap.MarkerStyle({
  129. "width": 25, // 点标记样式宽度(像素)
  130. "height": 35, // 点标记样式高度(像素)
  131. "src": markerDefault, //图片路径
  132. "anchor": { x: 16, y: 32 }
  133. })
  134. },
  135. //点标记数据数组
  136. geometries: [{
  137. "id": "1", //点标记唯一标识,后续如果有删除、修改位置等操作,都需要此id
  138. "styleId": 'myStyle', //指定样式id
  139. "position": new TMap.LatLng(this.form.lat,this.form.lng), //点标记坐标位置
  140. "properties": {//自定义属性
  141. "title": "marker1"
  142. }
  143. }]
  144. });
  145. },
  146. // 地图搜索事件
  147. remoteMethod(query) {
  148. if (query !== '') {
  149. this.loading = true
  150. this.$jsonp("https://apis.map.qq.com/ws/place/v1/suggestion/", {
  151. region: "兰州",
  152. keyword: query,
  153. key: "UZQBZ-SYQL3-LYF3K-Y7FAL-N3656-2DBJ4",
  154. output:"jsonp"
  155. })
  156. .then(res => {
  157. this.loading = false
  158. if(res.data.length > 0) {
  159. this.areaOptions = res.data
  160. }
  161. })
  162. .catch(err => {
  163. this.loading = false
  164. });
  165. } else {
  166. this.options = [];
  167. }
  168. },
  169. // 选择地区
  170. changeArea() {
  171. if(this.searchValue) {
  172. this.form.lat = this.searchValue.lat
  173. this.form.lng = this.searchValue.lng
  174. this.map.setCenter(new TMap.LatLng(this.searchValue.lat,this.searchValue.lng));
  175. this.creatPoint()
  176. this.getAddress()
  177. }
  178. },
  179. // 提交数据
  180. closeMap(){
  181. this.map.destroy();
  182. this.mapOpen=false;
  183. this.$emit('refreshDataList',this.form)
  184. },
  185. // 关闭弹窗事件
  186. handleClose() {
  187. this.map.destroy();
  188. this.mapOpen=false;
  189. }
  190. }
  191. }
  192. </script>
  193. <style lang="scss" scoped>
  194. .map-contain{
  195. /deep/.el-dialog{
  196. margin-top: 2vh !important;
  197. }
  198. }
  199. #mapCoord{
  200. width: 100%;
  201. height: 70vh;
  202. }
  203. .map-coord{
  204. position: relative;
  205. /deep/.address-select{
  206. position: absolute;
  207. top: 10px;
  208. left: 10px;
  209. z-index: 2000;
  210. }
  211. }
  212. </style>