最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - OpenLayers: How can I redraw a map after a sidebar is collapsed? - Stack Overflow

programmeradmin0浏览0评论

I am using OpenLayers to display a map, and AdminLte for the interface.

My problem : When collapsing the main left sidebar on a given page, all the boxes and what it contains changes size (I don't really know how) so the maps gets bigger as well. The problem is that when it happens, all the features displayed on the maps apparently change position and are not where they are supposed to be anymore.

What I would like: To redraw the map after the sidebar collapses.

Any suggestion?

I tried:

$('.navbar-collapse').on('shown.bs.collapse', function() {
    map.updateSize();
});

and:

$('.sidebar').on('shown.bs.collapse', function() {
   map.updateSize();
});

but to no avail...

EDIT : My question is similar to this one: OpenLayers: How to re-align mouse coordinates and vector layers after fluid css rendering of map div but his solution doesn't work for me :)

EDIT 2 : Just to clarify: I think the solution to my problem would be to call the map.updateSize() method when the sidebar has finished collapsing. The problem is that I don't know how to catch the moment when the sidebar has finished collapsing/expanding!

I am using OpenLayers to display a map, and AdminLte for the interface.

My problem : When collapsing the main left sidebar on a given page, all the boxes and what it contains changes size (I don't really know how) so the maps gets bigger as well. The problem is that when it happens, all the features displayed on the maps apparently change position and are not where they are supposed to be anymore.

What I would like: To redraw the map after the sidebar collapses.

Any suggestion?

I tried:

$('.navbar-collapse').on('shown.bs.collapse', function() {
    map.updateSize();
});

and:

$('.sidebar').on('shown.bs.collapse', function() {
   map.updateSize();
});

but to no avail...

EDIT : My question is similar to this one: OpenLayers: How to re-align mouse coordinates and vector layers after fluid css rendering of map div but his solution doesn't work for me :)

EDIT 2 : Just to clarify: I think the solution to my problem would be to call the map.updateSize() method when the sidebar has finished collapsing. The problem is that I don't know how to catch the moment when the sidebar has finished collapsing/expanding!

Share Improve this question edited Jul 27, 2017 at 14:55 Raphaël asked Jul 27, 2017 at 14:06 RaphaëlRaphaël 1131 silver badge9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

A temporary solution I found was to start a timeout when the button triggering the sidebar collapse and then call the map.updateSize() method:

  $('.sidebar-toggle').click(function(){
      setTimeout(function(){ map.updateSize(); }, 500);             
  });

It works...but it's kind of meh :/

If you're trying to redraw the map after the sidebar collapses, change your event handler to the following:

$('.sidebar').on('hidden.bs.collapse', function() {
   map.updateSize();
});

According to the list of event handlers here, the hidden.bs.collapse event is fired when a collapse element has been hidden from the user.

I have the same problem, using React and a class ponent my (awful) solution is this:

shouldComponentUpdate = async () =>
  await new Promise(res =>
    setTimeout(() => {
      this.map.updateSize()
      res(false)
    }, 500)
  )

It's awful because the resize causes the map to jump. If there is a way of achieving the map resize without a jump that would be pretty cool.

(500 is the animation time for my drawer to close)

发布评论

评论列表(0)

  1. 暂无评论