index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <div class="qqmap">
  3. <div class="map-tools">
  4. <!-- <Select class="map-select" v-model="form.city" size="large">
  5. <Option value="1">12</Option>
  6. </Select> -->
  7. <!-- <Input class="search-input" v-model="form.keyword" size="large" search enter-button @on-change="onChange" /> -->
  8. </div>
  9. <div class="map" id="map_container"></div>
  10. </div>
  11. </template>
  12. <script>
  13. import { getDistrict, getLocationByIp, getLocationByGeocoder } from 'api/qqmap'
  14. export default {
  15. props: {
  16. city: String,
  17. latitude: [Number, String],
  18. longitude: [Number, String]
  19. },
  20. data () {
  21. return {
  22. form: {
  23. keyword: ''
  24. },
  25. location: {},
  26. map: null,
  27. markerArr: [],
  28. searchList: {}
  29. }
  30. },
  31. mounted() {
  32. this.$nextTick(() => {
  33. this.initMap()
  34. })
  35. },
  36. methods: {
  37. async initMap() {
  38. await this.onChange()
  39. await this.getLocationByIp()
  40. let center = new TMap.LatLng(this.location.location.lat, this.location.location.lng);
  41. //定义map变量,调用 TMap.Map() 构造函数创建地图
  42. this.map = new TMap.Map(document.getElementById('map_container'), {
  43. center: center, //设置地图中心点坐标
  44. zoom: 17, //设置地图缩放级别
  45. });
  46. this.initMarkerEvent()
  47. },
  48. search ({lat, lng}) {
  49. this.map.setCenter(new TMap.LatLng(lat,lng))
  50. this.marker.remove(["1"])
  51. const latLng = {lat, lng}
  52. this.markerArr = [{
  53. id: '1',
  54. styleId: 'marker',
  55. position: latLng,
  56. properties: {
  57. title: '1'
  58. }
  59. }]
  60. this.marker.add(this.markerArr)
  61. getLocationByGeocoder(`${latLng.lat},${latLng.lng}`).then(res => {
  62. this.$emit('clickMap', {address_component: res.result.address_component,address: res.result.formatted_addresses.recommend, location: res.result.location})
  63. })
  64. },
  65. getLocationByIp () {
  66. if (this.location.location) {
  67. return
  68. }
  69. return getLocationByIp().then(res => {
  70. this.location = res.result
  71. })
  72. },
  73. onChange (e) {
  74. const keyword = this.city
  75. if (this.latitude && this.longitude) {
  76. return this.location = {
  77. location: {
  78. lat: this.latitude,
  79. lng: this.longitude
  80. }
  81. }
  82. }
  83. return getDistrict(keyword).then(res => {
  84. this.searchList = res.result[0]
  85. if (this.searchList) {
  86. this.location = {
  87. location: {
  88. lat: this.searchList[0].location.lat,
  89. lng: this.searchList[0].location.lng
  90. }
  91. }
  92. }
  93. })
  94. },
  95. initMarkerEvent () {
  96. this.marker = new TMap.MultiMarker({
  97. id: 'marker-layer',
  98. map: this.map,
  99. styles: {
  100. "marker": new TMap.MarkerStyle({
  101. "width": 25,
  102. "height": 35,
  103. "anchor": { x: 16, y: 32 },
  104. "src": 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png'
  105. })
  106. },
  107. geometries: this.markerArr
  108. })
  109. this.marker.remove(["1"])
  110. let latlng = {
  111. lat: this.latitude,
  112. lng: this.longitude
  113. }
  114. this.markerArr = [{
  115. id: '1',
  116. styleId: 'marker',
  117. position: latlng,
  118. properties: {
  119. title: '1'
  120. }
  121. }]
  122. this.marker.add(this.markerArr)
  123. this.map.on('click', (evt) => {
  124. this.marker.remove(["1"])
  125. const latLng = evt.latLng
  126. this.markerArr = [{
  127. id: '1',
  128. styleId: 'marker',
  129. position: latLng,
  130. properties: {
  131. title: '1'
  132. }
  133. }]
  134. this.marker.add(this.markerArr)
  135. getLocationByGeocoder(`${latLng.lat},${latLng.lng}`).then(res => {
  136. this.$emit('clickMap', {address_component: res.result.address_component,address: res.result.formatted_addresses.recommend, location: res.result.location})
  137. })
  138. })
  139. }
  140. }
  141. };
  142. </script>
  143. <style lang="less">
  144. .qqmap {
  145. position: relative;
  146. width: 100%;
  147. height: 400px;
  148. background-color: #28272a;
  149. margin-top: 20px;
  150. }
  151. .map-tools {
  152. position: absolute;
  153. top: 20px;
  154. left: 20px;
  155. }
  156. .search-input {
  157. z-index: 10000;
  158. background: #fff;
  159. width: 300px;
  160. position: absolute;
  161. left: 110px;
  162. top: 0;
  163. .ivu-input {
  164. background-color: #fff;
  165. color: #515a6e;
  166. border: 1px solid #dcdee2;
  167. z-index: 10000;
  168. }
  169. }
  170. .map {
  171. position: relative;
  172. width: 100%;
  173. height: 400px;
  174. }
  175. .map-select {
  176. display: inline-block;
  177. width: 100px;
  178. .ivu-select-selection {
  179. background-color: #fff;
  180. color: #515a6e;
  181. border: 1px solid #dcdee2;
  182. z-index: 10000;
  183. }
  184. .ivu-select-dropdown {
  185. background-color: #fff;
  186. z-index: 10000;
  187. }
  188. .ivu-select-item {
  189. color: #515a6e;
  190. z-index: 10000;
  191. background-color: #fff;
  192. &:hover {
  193. background-color: #fff;
  194. }
  195. }
  196. }
  197. </style>