2016-12-16 91 views
2

view.animate()我有以下代碼,爲的OpenLayers預先寫好3.20:view.fit()使用的OpenLayers 3.20+

fitViewToFeature: function (viewer, feature) { 
    var pan = ol.animation.pan({ 
     source: viewer.olView.getCenter(), 
     duration: 1000 
    }) 

    var zoom = ol.animation.zoom({ 
     resolution: viewer.olView.getResolution(), 
     duration: 1000 
    }) 

    viewer.olMap.beforeRender(pan, zoom) 

    viewer.olView.fit(feature.getGeometry(), viewer.olMap.getSize(), { 
     padding: [ 100, 100, 100, 100 ], 
     constrainResolution: false, 
     maxZoom: 4 
    }) 
} 

我的問題是如何將這種功能轉化爲新的視圖。 OpenLayers 3.20中引入的animate()語法?

或者,我應該打開一個GitHub問題並要求將一個新選項添加​​到view.animate中嗎?

回答

5

您應該能夠達到同樣的動畫在一個更簡單的方法,與duration選項ol.View#fit():在4.x版的OpenLayers

viewer.olView.fit(feature.getGeometry(), { 
    duration: 1000 
}); 

上述工程

+0

看空的財產「長度」可以更新這個答案OL4反映https://github.com/openlayers/openlayers/releases/tag/v4.0.0?我很樂意讓這一個被接受的答案,但它現在只與3.20兼容。 – zsero

+0

我已經更新了答案。 – ahocevar

0

@zsero,我使用相同的功能來放大圖層來源的範圍。我將view.animate()用於多達一半的路線,並在回調函數中使用view.fit()。

我需要的setTimeout的view.fit(),否則我有一個錯誤信息: 不能ol.View.updateAnimations_

var origine = view.getCenter(); 
var destination = ol.extent.getCenter(extent); 
var middleDestination = [(origine[0] + destination[0])/2, (origine[1] + destination[1])/2]; 
var viewFit = function() { 
    setTimeout(function() { 
     view.fit(extent, map.getSize(), { 
      duration: duration/2, 
      padding: [ 64, 10, 10, 328 ], 
      constrainResolution: false 
     }) 
    }, 0) 
} 
var animation = function() { 
    view.animate({ 
     center: middleDestination, 
     duration: duration/2 
    }); 
    view.animate({ 
     zoom: zoom - backZoom, 
     duration: duration/2 
    }, viewFit); 
}