Map.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <div>
  3. <div v-if="!hasBmView" ref="view" style="width: 100%; height: 100%">
  4. </div>
  5. <slot></slot>
  6. </div>
  7. </template>
  8. <script>
  9. import bindEvents from '../base/bindEvent.js'
  10. import {checkType} from '../base/util.js'
  11. export default {
  12. name: 'bm-map',
  13. props: {
  14. ak: {
  15. type: String
  16. },
  17. center: {
  18. type: [Object, String]
  19. },
  20. zoom: {
  21. type: Number
  22. },
  23. minZoom: {
  24. type: Number
  25. },
  26. maxZoom: {
  27. type: Number
  28. },
  29. highResolution: {
  30. type: Boolean,
  31. default: true
  32. },
  33. mapClick: {
  34. type: Boolean,
  35. default: true
  36. },
  37. mapType: {
  38. type: String
  39. },
  40. dragging: {
  41. type: Boolean,
  42. default: true
  43. },
  44. scrollWheelZoom: {
  45. type: Boolean,
  46. default: false
  47. },
  48. doubleClickZoom: {
  49. type: Boolean,
  50. default: true
  51. },
  52. keyboard: {
  53. type: Boolean,
  54. default: true
  55. },
  56. inertialDragging: {
  57. type: Boolean,
  58. default: true
  59. },
  60. continuousZoom: {
  61. type: Boolean,
  62. default: true
  63. },
  64. pinchToZoom: {
  65. type: Boolean,
  66. default: true
  67. },
  68. autoResize: {
  69. type: Boolean,
  70. default: true
  71. },
  72. theme: {
  73. type: Array
  74. },
  75. mapStyle: {
  76. type: Object
  77. }
  78. },
  79. watch: {
  80. center (val, oldVal) {
  81. const {map, zoom} = this
  82. if (checkType(val) === 'String' && val !== oldVal) {
  83. map.centerAndZoom(val, zoom)
  84. }
  85. },
  86. 'center.lng' (val, oldVal) {
  87. const {BMap, map, zoom, center} = this
  88. if (val !== oldVal && val >= -180 && val <= 180) {
  89. map.centerAndZoom(new BMap.Point(val, center.lat), zoom)
  90. }
  91. },
  92. 'center.lat' (val, oldVal) {
  93. const {BMap, map, zoom, center} = this
  94. if (val !== oldVal && val >= -74 && val <= 74) {
  95. map.centerAndZoom(new BMap.Point(center.lng, val), zoom)
  96. }
  97. },
  98. zoom (val, oldVal) {
  99. const {map} = this
  100. if (val !== oldVal && val >= 3 && val <= 19) {
  101. map.setZoom(val)
  102. }
  103. },
  104. minZoom (val) {
  105. const {map} = this
  106. map.setMinZoom(val)
  107. },
  108. maxZoom (val) {
  109. const {map} = this
  110. map.setMaxZoom(val)
  111. },
  112. highResolution () {
  113. this.reset()
  114. },
  115. mapClick () {
  116. this.reset()
  117. },
  118. mapType (val) {
  119. const {map} = this
  120. map.setMapType(global[val])
  121. },
  122. dragging (val) {
  123. const {map} = this
  124. val ? map.enableDragging() : map.disableDragging()
  125. },
  126. scrollWheelZoom (val) {
  127. const {map} = this
  128. val ? map.enableScrollWheelZoom() : map.disableScrollWheelZoom()
  129. },
  130. doubleClickZoom (val) {
  131. const {map} = this
  132. val ? map.enableDoubleClickZoom() : map.disableDoubleClickZoom()
  133. },
  134. keyboard (val) {
  135. const {map} = this
  136. val ? map.enableKeyboard() : map.disableKeyboard()
  137. },
  138. inertialDragging (val) {
  139. const {map} = this
  140. val ? map.enableInertialDragging() : map.disableInertialDragging()
  141. },
  142. continuousZoom (val) {
  143. const {map} = this
  144. val ? map.enableContinuousZoom() : map.disableContinuousZoom()
  145. },
  146. pinchToZoom (val) {
  147. const {map} = this
  148. val ? map.enablePinchToZoom() : map.disablePinchToZoom()
  149. },
  150. autoResize (val) {
  151. const {map} = this
  152. val ? map.enableAutoResize() : map.disableAutoResize()
  153. },
  154. theme (val) {
  155. const {map} = this
  156. map.setMapStyle({styleJson: val})
  157. },
  158. 'mapStyle.features': {
  159. handler (val, oldVal) {
  160. const {map, mapStyle} = this
  161. const {style, styleJson} = mapStyle
  162. map.setMapStyle({
  163. styleJson,
  164. features: val,
  165. style
  166. })
  167. },
  168. deep: true
  169. },
  170. 'mapStyle.style' (val, oldVal) {
  171. const {map, mapStyle} = this
  172. const {features, styleJson} = mapStyle
  173. map.setMapStyle({
  174. styleJson,
  175. features,
  176. style: val
  177. })
  178. },
  179. 'mapStyle.styleJson': {
  180. handler (val, oldVal) {
  181. const {map, mapStyle} = this
  182. const {features, style} = mapStyle
  183. map.setMapStyle({
  184. styleJson: val,
  185. features,
  186. style
  187. })
  188. },
  189. deep: true
  190. },
  191. mapStyle (val) {
  192. const {map, theme} = this
  193. !theme && map.setMapStyle(val)
  194. }
  195. },
  196. methods: {
  197. setMapOptions () {
  198. const {map, minZoom, maxZoom, mapType, dragging, scrollWheelZoom, doubleClickZoom, keyboard, inertialDragging, continuousZoom, pinchToZoom, autoResize} = this
  199. minZoom && map.setMinZoom(minZoom)
  200. maxZoom && map.setMaxZoom(maxZoom)
  201. mapType && map.setMapType(global[mapType])
  202. dragging ? map.enableDragging() : map.disableDragging()
  203. scrollWheelZoom ? map.enableScrollWheelZoom() : map.disableScrollWheelZoom()
  204. doubleClickZoom ? map.enableDoubleClickZoom() : map.disableDoubleClickZoom()
  205. keyboard ? map.enableKeyboard() : map.disableKeyboard()
  206. inertialDragging ? map.enableInertialDragging() : map.disableInertialDragging()
  207. continuousZoom ? map.enableContinuousZoom() : map.disableContinuousZoom()
  208. pinchToZoom ? map.enablePinchToZoom() : map.disablePinchToZoom()
  209. autoResize ? map.enableAutoResize() : map.disableAutoResize()
  210. },
  211. init (BMap) {
  212. if (this.map) {
  213. return
  214. }
  215. let $el = this.$refs.view
  216. for (let $node of this.$slots.default || []) {
  217. if ($node.componentOptions && $node.componentOptions.tag === 'bm-view') {
  218. this.hasBmView = true
  219. $el = $node.elm
  220. }
  221. }
  222. const map = new BMap.Map($el, {enableHighResolution: this.highResolution, enableMapClick: this.mapClick})
  223. this.map = map
  224. const {setMapOptions, zoom, getCenterPoint, theme, mapStyle} = this
  225. theme ? map.setMapStyle({styleJson: theme}) : map.setMapStyle(mapStyle)
  226. setMapOptions()
  227. bindEvents.call(this, map)
  228. // 此处强行初始化一次地图 回避一个由于错误的 center 字符串导致初始化失败抛出的错误
  229. map.reset()
  230. map.centerAndZoom(getCenterPoint(), zoom)
  231. this.$emit('ready', {BMap, map})
  232. // Debug
  233. // global.map = map
  234. // global.mapComponent = this
  235. },
  236. getCenterPoint () {
  237. const {center, BMap} = this
  238. switch (checkType(center)) {
  239. case 'String': return center
  240. case 'Object': return new BMap.Point(center.lng, center.lat)
  241. default: return new BMap.Point()
  242. }
  243. },
  244. initMap (BMap) {
  245. this.BMap = BMap
  246. this.init(BMap)
  247. },
  248. getMapScript () {
  249. if (!global.BMap) {
  250. const ak = this.ak || this._BMap().ak
  251. global.BMap = {}
  252. global.BMap._preloader = new Promise((resolve, reject) => {
  253. global._initBaiduMap = function () {
  254. resolve(global.BMap)
  255. global.document.body.removeChild($script)
  256. global.BMap._preloader = null
  257. global._initBaiduMap = null
  258. }
  259. const $script = document.createElement('script')
  260. global.document.body.appendChild($script)
  261. $script.src = `https://api.map.baidu.com/api?v=2.0&ak=${ak}&callback=_initBaiduMap`
  262. })
  263. return global.BMap._preloader
  264. } else if (!global.BMap._preloader) {
  265. return Promise.resolve(global.BMap)
  266. } else {
  267. return global.BMap._preloader
  268. }
  269. },
  270. reset () {
  271. const {getMapScript, initMap} = this
  272. getMapScript()
  273. .then(initMap)
  274. }
  275. },
  276. mounted () {
  277. this.reset()
  278. },
  279. data () {
  280. return {
  281. hasBmView: false
  282. }
  283. }
  284. }
  285. </script>