Identifying if a map moveend event was user initia

2019-08-09 19:45发布

问题:

I registered a 'moveend' event listener on my ol.Map. It's firing when the map is moved around by user input, but also when I call ol.View.setCenter and ol.View.setResolution.

Is it possible to check the 'moveend' ol.MapEvent to determine if the event was triggered by user input or from manually changing the map view's properties?

回答1:

I ended up doing the following.

map.on('moveend', function(event) {
  var mapView = map.getView(),
      moveInitiatedProgrammatically = mapView.get('moveInitiatedProgrammatically') || false;

  mapView.unset('moveInitiatedProgrammatically');

  // evaluate moveInitiatedProgrammatically's value and behave accordingly...
});

map.getView().set('moveInitiatedProgrammatically', true);
map.getView().setCenter(coord);

It's not ideal for the following reasons:

  1. Introduces additional state information in the map's view.
  2. Carelessly replacing the map view will lose that state information.
  3. Requires setting a property before changing the view state and may be too easily forgotten.

However, it adresses my issue in the meantime.