Can't undestand how to update a layer position
I don't know if this is the right place to ask, but we are facing an issue in our project. We would simply like to use a scatterplot layer to represent our position provided by GPS on the map. The problem is that it will only update the position once, when the app is launched and not everytime we move. The variable used to store coordinates is working as intended, updating them with navigator.geolocation.watchPosition but I can't update the layer to follow the gps because it will only render once at launch on the map. We saw some update functions in documentation but, considering that we are pretty new to this kind of things, we are not able to implement them.
This is our deckgl-overlay.js
import React, {Component} from 'react';
import DeckGL, {PathLayer} from 'deck.gl';
import DeckGL, {ScatterplotLayer} from 'deck.gl';
var currPos = null;
//Position tracker
navigator.geolocation.watchPosition(function(position) {
currPos = position;
});
export default class DeckGLOverlay extends Component {
static get defaultViewport() {
return {
longitude: 11.25,
latitude: 43.77,
zoom: 16,
maxZoom: 18,
pitch: 40, //viewport tilt
bearing: 0
};
}
_initialize(gl) {
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
}
//Add in const { }, new layers's ID
render() {
const {viewport, scatters, pos, icon, paths, trailLength, time} = this.props;
//Add another instance of a new layer here:
const layers = [
new ScatterplotLayer({
id: 'scatters',
data: scatters,
radiusScale: 100,
outline: false
}),
new ScatterplotLayer({
id: 'pos',
data: pos,
getPosition: pos => [currPos.coords.longitude, currPos.coords.latitude, 1],
radiusScale: 100,
outline: false
}),
new PathLayer({
id: 'paths',
data: paths,
rounded: true,
getColor: paths => [255, 0, 0, 255],
getWidth: paths => 0.03,
widthScale: 100
})
];
return (
<DeckGL {...viewport} layers={layers} onWebGLInitialized={this._initialize} />
);
}
}
Thank you in advance if you can help us and sorry if this isn't the right section to ask this kind of things
Hi @Nedo93, deck.gl's layer manager uses shallow compare to determine if the data prop of a layer has changed or not. If you are updating the element of value inside data but not changing the data variable itself, you probably need to use updateTriggers
(https://uber.github.io/deck.gl/#/documentation/advanced-topics/attribute-management?section=automatic-attribute-generation)
Please refer to #633 for more details.
@Nedo93 Some additional context to @shaojingli's excellent answer:
See the data comparison props section in the base Layer docs.
Accordingly, there are at least three ways to handle this case: you can either use a custom dataComparator prop, or add an updateTrigger on getPosition, or in this case of just a single value, you could simply mint a new array every time you render the layer in question.
new ScatterplotLayer({
id: 'pos',
data: [...pos], // deck.gl uses shallow comparison: a completely new array will be considered different and trigger regeneration of WebGL buffers
getPosition: pos => [currPos.coords.longitude, currPos.coords.latitude, 1],
radiusScale: 100,
outline: false
}),
Ok, thank you guys we are working on it to right now.
Thank you @shaojingli your answer was very helpful.
The updated link for updateTriggers is at:
http://deck.gl/#/documentation/deckgl-api-reference/layers/layer?section=data-properties
updateTriggers docs is now at https://deck.gl/docs/api-reference/core/layer#updatetriggers