Skip to content

Commit 5e40b24

Browse files
committed
Only ask for data when needed.
The idea here is that if the current bounds are contained within the previous bounds, mapbox-gl-js still contains the data and will display it without another server roundtrip. The insight that springs from this is that there are two event handlers that need to be given to the map component: onBoundsChange: called every time the bounds change. This would be used to update the url. onMissingData: called when the current map bounds are outside of previous bounds. We then have area within the map bounds for which we need data. These two callbacks seem to form the basis of a saner API for the map component. TODO: Currently we recalculate the summary, after loading data for the current bounds. If we are now longer hitting the server for each change of bounds, we now need to ask the map which elements of data are currently visible and recalculate the summary based on that. The function that does that is `queryRenderedFeatures`: `map.queryRenderedFeatures(map.getBounds().toArray(), {layers: [ "markers" , "selected" ]})` The problem here is that it returns nothing, duplicates or the right thing seemingly at random. issue: mapbox/mapbox-gl-js#2647 When that issue is resolved, revisit this.
1 parent bab0480 commit 5e40b24

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
"react-router": "^2.4.0",
7070
"supertest": "^1.2.0",
7171
"transform-loader": "^0.2.3",
72+
"turf-inside": "^3.0.12",
73+
"turf-point": "^2.0.1",
7274
"underscore": "^1.8.3",
7375
"urijs": "^1.18.0",
7476
"webpack": "^1.13.0",

public/javascripts/components/Map.js

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Map extends React.Component {
6868
'center': e.target.getCenter(),
6969
'zoom': e.target.getZoom()
7070
}
71+
72+
7173
this.props.onBoundsChange(boundsObj)
7274
}
7375

@@ -126,6 +128,8 @@ class Map extends React.Component {
126128
});
127129

128130

131+
console.log(`hl:`, this.props.highlight)
132+
129133
this.map.addLayer({
130134
"id": "selected",
131135
"type": "symbol",
@@ -145,6 +149,11 @@ class Map extends React.Component {
145149
});
146150

147151

152+
// The goal here is to extract the visible features
153+
// so we can create a summary with them.
154+
console.log('rendered features:', this.map.queryRenderedFeatures(this.map.getBounds().toArray(), {layers: [ "markers" , "selected" ]}))
155+
156+
148157
}
149158
}
150159

public/javascripts/main.js

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import MediaQuery from 'react-responsive'
2+
import bboxPolygon from 'turf-bbox-polygon'
3+
import point from 'turf-point'
4+
import inside from 'turf-inside'
25
import summary from './summary'
36
import Map from './components/Map'
47
import React, { PropTypes } from 'react'
@@ -71,8 +74,11 @@ class MapView extends React.Component {
7174
document.dispatchEvent(event)
7275
}
7376

74-
handleMapBoundsChange(newBounds) {
77+
flashMessageOnMap(message) {
78+
this.dispatchEvent('mapbox.setflash', {message, info: true, fadeout: 3})
79+
}
7580

81+
getDataForBounds(bounds) {
7682
client.query(`
7783
query getLocations($neLat: Float, $neLng: Float, $swLat: Float, $swLng: Float) {
7884
locations_within_bounds(ne_lat: $neLat, ne_lng: $neLng, sw_lat: $swLat, sw_lng: $swLng){
@@ -89,13 +95,37 @@ class MapView extends React.Component {
8995
}
9096
}
9197
}
92-
`, newBounds).then((result) => {
98+
`, bounds).then((result) => {
9399
this.setState({mapData: result.locations_within_bounds})
94100
}, (e) => {
95-
this.dispatchEvent('mapbox.setflash', {message: e.message.split(':')[1], info: true, fadeout: 3})
101+
this.flashMessageOnMap(e.message.split(':')[1])
96102
})
103+
}
104+
105+
handleMapBoundsChange(currentBounds) {
106+
107+
// Do we need to fetch new data?
108+
if(!(typeof this.previousBounds == 'undefined')){
109+
let previousPolygon = bboxPolygon([this.previousBounds.neLng, this.previousBounds.neLat, this.previousBounds.swLng, this.previousBounds.swLat])
110+
let currentNE = point([currentBounds.neLng, currentBounds.neLat])
111+
let currentSW = point([currentBounds.swLng, currentBounds.swLat])
112+
// If the current bounds are within the previous polygon
113+
// we don't need new data.
114+
if(!(inside(currentNE, previousPolygon) && inside(currentSW, previousPolygon))){
115+
console.log(`Need data!!`)
116+
this.getDataForBounds(currentBounds)
117+
this.previousBounds = currentBounds
118+
} else {
119+
console.log(`Don't Need data!!`)
120+
}
121+
} else {
122+
console.log(`First load?`)
123+
this.getDataForBounds(currentBounds)
124+
this.previousBounds = currentBounds
125+
}
126+
97127

98-
this.updateRoute(newBounds.zoom, newBounds.center.lat, newBounds.center.lng, this.props.location.query.highlight)
128+
this.updateRoute(currentBounds.zoom, currentBounds.center.lat, currentBounds.center.lng, this.props.location.query.highlight)
99129
}
100130

101131
updateRoute(zoom, lat, lng, highlight = '') {

0 commit comments

Comments
 (0)