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