question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update Features on bounds changed

See original GitHub issue

I wish to update the features of a layer each time the map changes bounds.

I have looked at @alex3165’s talk, and it’ similar to the effect at 14:56. However, one crucial difference: if I understood correctly in the video all the features are loaded at init, and the left pane is only filtering based on bounds.

What I wish to achieve is: since I have a lot of features, I don’t want to load them all, but only load the ones that are visible, i.e. in the bounds of the map. And every time I drag/zoom/rotate the map, the list of features updates.

Here’s how I would do it:

// In the render method
<MyMap
  listings={this.props.listings}
  boundsChanged={map => {
    console.log(map.getBounds()); // This works
    this.getListings(map.getBounds());
  }}
/>

Where this.getListings is an API call which retrieves listings in the bounds and updates this.props.listings.

And here is my implementation of <MyMap />:

// In the render method
      <Map
        style="mapbox://styles/mapbox/streets-v9" // eslint-disable-line
        containerStyle={{ width: '100vw', height: '100vh' }}
        onDrag={boundsChanged}
        onRotate={boundsChanged}
      >
        <Layer
          type="symbol"
          id="listings"
          layout={{ 'icon-image': 'marker-15' }}
        >
          {listings.map((listing) => (
            <Feature coordinates={[listing.longitude, listing.latitude]} key={listing._id} />
          ))}
        </Layer>
      </Map>

However, what happens is that when I drag, the map re-renders, and go back to the initial bounds (around London).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
alex3165commented, Sep 19, 2017

The center will change only if you break the reference of the array of your center prop. The solution if you want to leave mapbox handling the center is to keep your center in the state and do not break the reference between re-render. If you want to take over the control of the center then you just break the reference by creating a new array, even if the value are the same it will update the center to the value you pass as a prop (zoom work the same way).

It is not natural at first and not very well documented (I will work on it) but I think it is better than keeping the viewport in sync with mapbox’s viewport which would trigger lot of re-rendering of the map component like react-map-gl is doing.

0reactions
amaurymcommented, Sep 21, 2017

@Wykks Yes my bad, that did the trick. I defined Map inside a function component.

And thank you all for all the comments!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Display new markers when map bound changed and clear old ...
Markers are moving every time bounds updated and clearing the old position markers. I am exactly trying to do this. UPDATED 2. what...
Read more >
Add Bounds Changed Event Listener on Google Maps
In this example you will learn how to add Bounds Changed Event Listener to google map. This event fired when the viewport bounds...
Read more >
Scroll View School - Swift 4.2, iOS 12, Xcode 10 - YouTube
Programming Challenge: Update Frames and Bounds - Scroll View School ... If the timer fires without a significant change in position, ...
Read more >
maps zoom_changed event fires before bounds have been ...
I don't understand comment #4 . The issue is NOT fixed, it's still present. This is clearly a bug, because the map is...
Read more >
Place Autocomplete | Maps JavaScript API - Google Developers
The Strict Bounds option restricts the search to the area within the current viewport. If this option is not checked, then the API...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found