I am currently using OL 4.6.5.
In my project, I have markers in a vector layer and I created a custom method to determine the zoom level based on the distance between the markers. While it's not the most elegant solution, it gets the job done...
Now, I'm on the lookout for a more efficient way to automatically set the zoom level based on the maximum and minimum latitude and longitude values. I attempted to use "fitExtent()" but couldn't get it to work as expected. After some research, I came across two potential solutions, but only one of them worked:
var onChangeKey = vectorSource.on('change', function() {
if(vectorSource.getState() == 'ready') {
vectorSource.unByKey(onChangeKey);
map.getView().fitExtent(vectorSource.getExtent(), map.getSize());
}
});
This code snippet utilizes "fitExtent()" in a different manner than I had tried before, although I encountered an issue where the zoom level remained at 2. The other solution involved strange syntax that resulted in a JS console error: "Uncaught SyntaxError: Unexpected identifier".
You can view an example page demonstrating this issue here: . The JavaScript code is located at the bottom of the page, showcasing both solutions I experimented with - the one mentioned above is currently active. Additionally, you'll find my initial, less sophisticated approach present within the code.
Surely there exists a smarter solution for this problem, right?