Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event passed from map.on('moveend' missing originalEvent on some interactions #6405

Closed
hryanjones opened this issue Mar 27, 2018 · 8 comments
Labels

Comments

@hryanjones
Copy link

Context: I'm looking for a way to tell whether user interaction caused the map to move. The use case is that we use .fitBounds to set the initial state and when the user changes the bounding box (via scroll zoom, double click zoom, drag to pan, the navigation controls, keyboard commands, etc.) we want to persist the bounds, but we don't want to do this when it's a fitBounds.

Is there a way to tell that it was user initiated? The only time I've seen where I can distinguish is when I also see an .originalEvent property in the event in addition to .target which happens on some user interactions, but not on all of them.

mapbox-gl-js version: (using version that was in the examples on mapboxGL website)

Steps to Trigger Behavior

  1. Using HTML below with console open
  2. notice that .fitBounds triggers the event and it doesn't have an .originalEvent property
  3. notice that the event logged from these interactions have an .originalEvent:
  • drag
  • double click
  • if you use the keyboard +/- buttons
  1. notice that .originalEvent is missing if you do the following:
  • click on the navigation +/- zoom buttons
  • if you scroll to change zoom
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css' rel='stylesheet' />
<style>
    body { margin:0; padding:0; }
    #map { position:absolute; top:0; bottom:0; width:100%; }
</style>

<div id='map'></div>
<script>
mapboxgl.accessToken = ''; // Put your own mapbox token here

var map = new mapboxgl.Map({container: 'map', style: 'mapbox://styles/mapbox/streets-v9'});
map.fitBounds([[-133.4261895047858, 27.268363255653668], [-89.21205651435507, 46.38482102526038]]);
map.addControl(new mapboxgl.NavigationControl());

map.on('moveend', function(event) {
    console.log(event);
});
</script>
@kristophM
Copy link

Any response to this please? I'm having the same issue

@soldt
Copy link

soldt commented Jun 2, 2018

A solution would be to add eventData to the fitBounds method.

eventData(Object)Additional properties to be added to event objects of events triggered by this method.

Example fitBounds:

map.fitBounds(lngLatBounds, {}, {breakonfitbounds: true});

Moveend:

map.on('moveend', function (e) {
  if (e.breakonfitbounds) {
    return;
  }
  // Do something
});

@sheerun
Copy link

sheerun commented Jan 3, 2019

I'm using library that doesn't allow to pass extra args to fitBounds, it would be nice if originalEvent was passed by default by mapboxgl, currently there's no way to tell whether zoomend has beeen triggered by user or fitting bounds

@stepankuzmin
Copy link
Contributor

Hi everyone!

I've created PR that adds originalEvent property to NavigationControl events. Also originalEvent was added to user-initiated scroll zoomend and moveend events in #6175

@mourner
Copy link
Member

mourner commented Sep 9, 2019

@stepankuzmin what else should be covered to consider this ticket resolved?

@stepankuzmin
Copy link
Contributor

I think nothing more is needed. @hryanjones can you please try to reproduce your issue with the master build of mapbox-gl-js?

@mourner
Copy link
Member

mourner commented Sep 9, 2019

OK, let's close for now and reopen if there are any more items left to do.

@mourner mourner closed this as completed Sep 9, 2019
@derwaldgeist
Copy link

Is there already a release including this?

I stumbled upon the problem here
urbica/react-map-gl#276

If I understand things correctly, this missing event causes react-map-gl not to report back viewport changes after clicking on the GeolocationControl. Which feels pretty weird because the zoom gets out of sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants