Driving.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <div v-show="panel">
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import { createPoint } from '../base/factory.js'
  8. import { isPoint, getPosition } from '../base/util.js'
  9. import commonMixin from '../base/mixins/common.js'
  10. export default {
  11. name: 'bm-driving',
  12. emits: ['searchcomplete', 'markersset', 'infohtmlset', 'polylinesset', 'resultshtmlset'],
  13. mixins: [commonMixin('search')],
  14. props: {
  15. location: {
  16. type: [Object, String]
  17. },
  18. start: {
  19. type: [Object, String]
  20. },
  21. end: {
  22. type: [Object, String]
  23. },
  24. startCity: {
  25. type: [String, Number]
  26. },
  27. endCity: {
  28. type: [String, Number]
  29. },
  30. waypoints: {
  31. type: Array
  32. },
  33. policy: {
  34. type: String
  35. },
  36. panel: {
  37. type: Boolean,
  38. default: true
  39. },
  40. autoViewport: {
  41. type: Boolean
  42. },
  43. selectFirstResult: {
  44. type: Boolean
  45. }
  46. },
  47. watch: {
  48. location: {
  49. handler(val) {
  50. const { originInstance, map } = this
  51. originInstance.setLocation(val || map)
  52. },
  53. deep: true
  54. },
  55. start: {
  56. handler(val) {
  57. const { originInstance, end, startCity, endCity, waypoints, BMap, getWaypoints } = this
  58. originInstance.search(getPosition(BMap, val), getPosition(BMap, end), {
  59. startCity,
  60. endCity,
  61. waypoints: getWaypoints(waypoints)
  62. })
  63. },
  64. deep: true
  65. },
  66. end: {
  67. handler(val) {
  68. const { originInstance, start, startCity, endCity, waypoints, BMap, getWaypoints } = this
  69. originInstance.search(getPosition(BMap, start), getPosition(BMap, val), {
  70. startCity,
  71. endCity,
  72. waypoints: getWaypoints(waypoints)
  73. })
  74. },
  75. deep: true
  76. },
  77. startCity(val) {
  78. const { originInstance, start, end, endCity, waypoints, getWaypoints } = this
  79. originInstance.search(start, end, {
  80. val,
  81. endCity,
  82. waypoints: getWaypoints(waypoints)
  83. })
  84. },
  85. endCity(val) {
  86. const { originInstance, start, end, startCity, waypoints, getWaypoints } = this
  87. originInstance.search(start, end, {
  88. startCity,
  89. val,
  90. waypoints: getWaypoints(waypoints)
  91. })
  92. },
  93. waypoints: {
  94. handler(val) {
  95. const { originInstance, start, end, startCity, endCity, getWaypoints } = this
  96. originInstance.search(start, end, {
  97. startCity,
  98. endCity,
  99. waypoints: getWaypoints(val)
  100. })
  101. },
  102. deep: true
  103. },
  104. panel() {
  105. this.reload()
  106. },
  107. policy(val) {
  108. this.reload()
  109. },
  110. autoViewport() {
  111. this.reload()
  112. },
  113. selectFirstResult() {
  114. this.reload()
  115. },
  116. highlightMode() {
  117. this.reload()
  118. }
  119. },
  120. methods: {
  121. search(start, end, { startCity, endCity, waypoints }) {
  122. const { originInstance, getWaypoints } = this
  123. originInstance.search(start, end, {
  124. startCity,
  125. endCity,
  126. waypoints: getWaypoints(waypoints)
  127. })
  128. },
  129. getWaypoints(waypoints) {
  130. const { BMap } = this
  131. if (waypoints) {
  132. return waypoints.map(position => getPosition(BMap, position))
  133. }
  134. },
  135. load() {
  136. const instance = this
  137. const { map, BMap, location, policy, selectFirstResult, autoViewport, highlightMode, search, start, end, startCity, endCity, waypoints, originInstance, getWaypoints } = this
  138. const _location = location ? isPoint(location) ? createPoint(BMap, location) : location : map
  139. const route = this.originInstance = new BMap.DrivingRoute(_location, {
  140. renderOptions: {
  141. map,
  142. // panel: panel && this.$el,
  143. panel: this.$el,
  144. selectFirstResult,
  145. autoViewport,
  146. highlightMode
  147. },
  148. policy: window[policy],
  149. onSearchComplete(e) {
  150. if (originInstance && originInstance !== route) {
  151. originInstance.clearResults()
  152. }
  153. instance.$emit('searchcomplete', e)
  154. },
  155. onMarkersSet(e) {
  156. instance.$emit('markersset', e)
  157. },
  158. onInfoHtmlSet(e) {
  159. instance.$emit('infohtmlset', e)
  160. },
  161. onPolylinesSet(e) {
  162. instance.$emit('polylinesset', e)
  163. },
  164. onResultsHtmlSet(e) {
  165. instance.$emit('resultshtmlset', e)
  166. }
  167. })
  168. search(getPosition(BMap, start), getPosition(BMap, end), {
  169. startCity,
  170. endCity,
  171. waypoints: getWaypoints(waypoints)
  172. })
  173. }
  174. }
  175. }
  176. </script>