gpsLocation.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <div>
  3. <!-- <el-amap-search-box class="search-box"
  4. value="searchValue"
  5. :search-option="searchOption"
  6. :on-search-result="onSearchResult"></el-amap-search-box> -->
  7. <el-amap :zoom="zoom"
  8. ref='map'
  9. vid="amapDemo"
  10. :amap-manager="amapManager"
  11. :center="center"
  12. :events="markerEvents"
  13. class="amap-demo">
  14. <!-- :plugin="plugin" -->
  15. <el-amap-marker v-for="(marker, index) in markers"
  16. :key="index"
  17. :position="marker.location"
  18. :title="marker.title"></el-amap-marker>
  19. <el-amap-text v-for="(marker,index) in markers"
  20. :key="index+'xxx'"
  21. :text="marker.title"
  22. :position="marker.location"
  23. :offset="[40,-10]"></el-amap-text>
  24. <el-amap-polyline :path="path"
  25. strokeColor='#4196fc'></el-amap-polyline>
  26. <el-amap-text v-for="(text,index) in texts"
  27. :key="index+'ooo'"
  28. :text="text.text"
  29. :position="text.position"
  30. :offset="[-50,0]"></el-amap-text>
  31. </el-amap>
  32. </div>
  33. </template>
  34. <script>
  35. import VueAMap from 'vue-amap'
  36. // 地图
  37. (function () {
  38. let func = EventTarget.prototype.addEventListener;
  39. let supportsPassive = false
  40. try {
  41. let opts = Object.defineProperty({}, 'passive', {
  42. get: function () {
  43. supportsPassive = true;
  44. }
  45. })
  46. document.addEventListener("testPassive", null, opts);
  47. document.removeEventListener('testPassive', null, opts);
  48. } catch (e) { }
  49. EventTarget.prototype.addEventListener = function (type, fn, capture) {
  50. this.func = func;
  51. capture = capture instanceof Object ? capture : {};
  52. capture.passive = supportsPassive;
  53. this.func(type, fn, capture);
  54. };
  55. }());
  56. // Vue.use(VueAMap)
  57. VueAMap.initAMapApiLoader({
  58. key: 'b1e6ac2eb28902ce91a490edf194e000',
  59. plugin: ['Autocomplete', 'PlaceSearch', 'Scale', 'OverView', 'ToolBar', 'MapType', 'PolyEditor', 'AMap.CircleEditor'],
  60. v: '1.4.4',
  61. })
  62. let amapManager = new VueAMap.AMapManager();
  63. import { getTeacherPersonalAttendance } from '@/api/buildTeam'
  64. export default {
  65. props: ['activeRow'],
  66. data () {
  67. return {
  68. zoom: 20,
  69. amapManager,
  70. center: [114.34371, 30.55939],
  71. markers: [],
  72. distance: '-',
  73. schoolLongitudeLatitude: null,
  74. signInLongitudeLatitude: null,
  75. markerEvents: {
  76. complete: (o) => {
  77. this.$refs.map.$amap.setFitView()
  78. }
  79. },
  80. path: [],
  81. texts: []
  82. }
  83. },
  84. watch: {
  85. activeRow (val) {
  86. if (val) {
  87. console.log(val)
  88. this.init()
  89. }
  90. }
  91. },
  92. mounted () {
  93. this.init()
  94. },
  95. activated () {
  96. // this.init()
  97. },
  98. methods: {
  99. // 地点搜索
  100. // searchOption () { },
  101. // 重置
  102. init () {
  103. if (this.activeRow.schoolLongitudeLatitude) {
  104. let add = {
  105. title: '教学点',
  106. location: this.activeRow.schoolLongitudeLatitude.split(','),
  107. }
  108. this.center = this.activeRow.schoolLongitudeLatitude.split(',');
  109. this.schoolLongitudeLatitude = this.activeRow.schoolLongitudeLatitude.split(',')
  110. this.markers.push(add)
  111. }
  112. if (this.activeRow.signInLongitudeLatitude) {
  113. let add = {
  114. title: '签到点',
  115. location: this.activeRow.signInLongitudeLatitude.split(',')
  116. }
  117. this.center = this.activeRow.signInLongitudeLatitude.split(',');
  118. this.signInLongitudeLatitude = this.activeRow.signInLongitudeLatitude.split(',')
  119. this.markers.push(add)
  120. }
  121. if (this.activeRow.signOutLongitudeLatitude) {
  122. let add = {
  123. title: '签退点',
  124. location: this.activeRow.signOutLongitudeLatitude.split(',')
  125. }
  126. this.center = this.activeRow.signOutLongitudeLatitude.split(',');
  127. this.signOutLongitudeLatitude = this.activeRow.signOutLongitudeLatitude.split(',')
  128. this.markers.push(add)
  129. }
  130. // if (this.$refs.map) {
  131. // this.$refs.map.$amap.setFitView(this.markers)
  132. // }
  133. this.getDistance(this.signInLongitudeLatitude, this.schoolLongitudeLatitude)
  134. this.getDistance(this.signOutLongitudeLatitude, this.schoolLongitudeLatitude)
  135. },
  136. onSearchResult () { },
  137. getDistance (sigin, school) {
  138. if (sigin && sigin.length > 0 && school && school.length > 0) {
  139. let o = amapManager.getMap();
  140. let marker1 = new AMap.Marker({
  141. position: sigin
  142. });
  143. let marker2 = new AMap.Marker({
  144. position: school
  145. });
  146. let p1 = marker1.getPosition();
  147. let p2 = marker2.getPosition();
  148. var textPos = p1.divideBy(2).add(p2.divideBy(2));
  149. console.log(textPos)
  150. let position = [textPos.lng, textPos.lat]
  151. var distance = Math.round(p1.distance(p2));
  152. this.path.push(sigin, school)
  153. this.texts.push({ text: `两点相距${distance}米`, position })
  154. this.distance = distance;
  155. }
  156. }
  157. }
  158. }
  159. </script>
  160. <style lang="scss" scoped>
  161. .title {
  162. margin-bottom: 20px;
  163. }
  164. .amap-demo {
  165. height: 500px;
  166. }
  167. </style>