LocalSearch.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div v-show="panel">
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import { createPoint, createBounds } from '../base/factory.js'
  8. import { isPoint } from '../base/util.js'
  9. import commonMixin from '../base/mixins/common.js'
  10. export default {
  11. name: 'bm-local-search',
  12. emits: ['markersset', 'infohtmlset', 'resultshtmlset', 'searchcomplete'],
  13. mixins: [commonMixin('search')],
  14. props: {
  15. location: {
  16. type: [Object, String]
  17. },
  18. keyword: {
  19. type: [Array, String]
  20. },
  21. panel: {
  22. type: Boolean,
  23. default: true
  24. },
  25. forceLocal: {
  26. type: Boolean
  27. },
  28. customData: {
  29. type: Object
  30. },
  31. bounds: {
  32. type: Object
  33. },
  34. nearby: {
  35. type: Object
  36. },
  37. // page: {
  38. // type: Number
  39. // },
  40. pageCapacity: {
  41. type: Number
  42. },
  43. autoViewport: {
  44. type: Boolean
  45. },
  46. selectFirstResult: {
  47. type: Boolean
  48. }
  49. },
  50. watch: {
  51. location: {
  52. handler(val) {
  53. const { originInstance, search } = this
  54. originInstance.setLocation(val || this.map)
  55. search()
  56. },
  57. deep: true
  58. },
  59. keyword() {
  60. this.search()
  61. },
  62. bounds: {
  63. handler(val) {
  64. const { searchInBounds } = this
  65. searchInBounds(val)
  66. },
  67. deep: true
  68. },
  69. nearby: {
  70. handler(val) {
  71. const { searchNearby } = this
  72. searchNearby(val)
  73. },
  74. deep: true
  75. },
  76. forceLocal() {
  77. this.reload()
  78. },
  79. customData: {
  80. deep: true,
  81. handler() {
  82. this.reload()
  83. }
  84. },
  85. // panel () {
  86. // this.reload()
  87. // },
  88. pageCapacity(val) {
  89. this.originInstance && this.originInstance.setPageCapacity(val)
  90. },
  91. autoViewport(val) {
  92. this.originInstance && (val ? this.originInstance.enableAutoViewport() : this.originInstance.disableAutoViewport())
  93. },
  94. selectFirstResult(val) {
  95. this.originInstance && (val ? this.originInstance.enableFirstResultSelection() : this.originInstance.disableFirstResultSelection())
  96. },
  97. highlightMode() {
  98. this.reload()
  99. }
  100. },
  101. methods: {
  102. searchNearby(nearby) {
  103. const { originInstance, keyword, customData, BMap } = this
  104. originInstance.searchNearby(keyword, createPoint(BMap, nearby.center), nearby.radius, customData)
  105. },
  106. searchInBounds(bounds) {
  107. const { originInstance, keyword, customData, BMap } = this
  108. originInstance.searchInBounds(keyword, createBounds(BMap, bounds), customData)
  109. },
  110. search() {
  111. const { originInstance, keyword, forceLocal, customData, nearby, bounds, searchNearby, searchInBounds } = this
  112. nearby ? searchNearby(nearby) : bounds ? searchInBounds(bounds) : originInstance.search(keyword, {
  113. forceLocal,
  114. customData
  115. })
  116. },
  117. load() {
  118. const instance = this
  119. const { map, BMap, search, pageCapacity, autoViewport, selectFirstResult, highlightMode, location, originInstance } = this
  120. const _location = location ? isPoint(location) ? createPoint(BMap, location) : location : map
  121. const route = this.originInstance = new BMap.LocalSearch(_location, {
  122. onMarkersSet(e) {
  123. instance.$emit('markersset', e)
  124. },
  125. onInfoHtmlSet(e) {
  126. instance.$emit('infohtmlset', e)
  127. },
  128. onResultsHtmlSet(e) {
  129. instance.$emit('resultshtmlset', e)
  130. },
  131. onSearchComplete(e) {
  132. if (originInstance && originInstance !== route) {
  133. originInstance.clearResults()
  134. }
  135. instance.$emit('searchcomplete', e)
  136. },
  137. pageCapacity: pageCapacity,
  138. renderOptions: {
  139. map,
  140. // panel: panel && this.$el,
  141. panel: this.$el,
  142. selectFirstResult,
  143. autoViewport,
  144. highlightMode
  145. }
  146. })
  147. search()
  148. }
  149. }
  150. }
  151. </script>