Vue系列:在vux的popup組件中使用百度地圖遇到顯示不全的問題


問題描述:
將百度地圖封裝成一個獨立的組件BMapComponent,具體見  Vue系列:如何將百度地圖包裝成Vue的組件 http://www.cnblogs.com/strinkbug/p/5769075.html),然后將BMapComponent作為vux的popup組件的子組件,代碼如下:

   
   
   
           
  1. <popup :show.sync="showPositionContainer" style="position:absolute">
  2. <b-map-component v-ref:mapviewer :map-height="mapH"></b-map-component>
  3. </popup>

   
   
   
           
  1. selectPosition() {
  2. this.showPositionContainer = true
  3. var that = this
  4. that.$refs.mapviewer.showMap(that.mapInfo)
  5. }
  6. },
  7. //BMapComponent的showMap方法定義如下:
  8. showMap(mapInfo) {
  9. console.log('enter initMap')
  10. this.mapInfo = this.cloneMapInfo(mapInfo)
  11. this.map = new BMap.Map("allmap")
  12. var point = new BMap.Point(this.mapInfo.longitude, this.mapInfo.latitude)
  13. var marker = new BMap.Marker(point)
  14. this.map.addOverlay(marker)
  15. this.map.centerAndZoom(point, 15)
  16. this.needCenter = false
  17. var infoWindow = new BMap.InfoWindow(this.mapInfo.address, this.opts) // 創建信息窗口對象
  18. this.map.enableScrollWheelZoom(true)
  19. this.map.openInfoWindow(infoWindow, point) //開啟信息窗口
  20. },
發現地圖總是顯示不全。

原因分析:
    popup通過show屬性來控制顯示和隱藏,然后在內部通過watch該show屬性的變化,再響應事件來執行相關的顯示和隱藏的動作,由於vue是在獨立的線程中去輪訓那些被watch的變量的變化,這個輪訓是有一定的間隔的,所以屬性變化和動作執行之間是異步的。但是我們在代碼中,showPositionContainer改為true之后馬上就執行showMap,此時popup還沒顯示出來。

解決方法:
    把selectPosition改為如下方式即可.
   
   
   
           
  1. selectPosition() {
  2. this.showPositionContainer = true
  3. var that = this
  4. //此處加了個延時處理,因為popup的show屬性響應沒有那么及時
  5. window.setTimeout(function() { that.$refs.mapviewer.showMap(that.mapInfo)}, 150)
  6. }














免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM