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