zac*_*dav 7 javascript reactjs deck.gl
我正在按照deck.gl github存储库中提供的示例显示geojson中的多边形。
从那以后,我改变了地图的初始焦点并提供了我自己的 geojson 进行可视化,我替换示例的数据具有我想通过操作范围输入来可视化的时间组件。
GeoJSON 结构示例
{
"type": "FeatureCollection",
"name": "RandomData",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature",
"properties": { "id": 1,"hr00": 10000, "hr01": 12000, "hr02": 12000, "hr03": 30000, "hr04": 40000, "hr05": 10500, "hr06": 50000}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 103.73992, 1.15903 ], [ 103.74048, 1.15935 ], [ 103.74104, 1.15903 ], [ 103.74104, 1.15837 ], [ 103.74048, 1.15805 ], [ 103.73992, 1.15837 ], [ 103.73992, 1.15903 ] ] ] } } ] }
Run Code Online (Sandbox Code Playgroud)
我没有为每个时间点重复每个几何图形,而是将数据的时间方面转移到属性。这使得文件大小可以在完整数据集上进行管理(~50mb 对 ~500mb)。
为了可视化单个时间点,我知道我可以将属性提供给getElevation和getFillColor。
_renderLayers() {
const {data = DATA_URL} = this.props;
return [
new GeoJsonLayer({
id: 'geojson',
data,
opacity: 0.8,
stroked: false,
filled: true,
extruded: true,
wireframe: true,
fp64: true,
getElevation: f => f.properties.hr00,
getFillColor: f => COLOR_SCALE(f.properties.hr00),
getLineColor: [255, 255, 255],
lightSettings: LIGHT_SETTINGS,
pickable: true,
onHover: this._onHover,
transitions: {
duration: 300
}
})
];
}
Run Code Online (Sandbox Code Playgroud)
所以我继续使用range.slider,将代码添加到我的app.js,添加了以下代码段。我相信我也可能把它放在错误的位置,这应该存在于render()吗?
import ionRangeSlider from 'ion-rangeslider';
// Code for slider input
$("#slider").ionRangeSlider({
min: 0,
max: 24,
from: 12,
step: 1,
grid: true,
grid_num: 1,
grid_snap: true
});
$(".js-range-slider").ionRangeSlider();
Run Code Online (Sandbox Code Playgroud)
添加到我的 index.html
<input type="text" id="slider" class="js-range-slider" name="my_range" value=""/>
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能让滑块更改我的 geojson 的哪个属性被提供给getElevationand getFillColor?
我的 JavaScript/JQuery 缺乏,我无法找到任何关于如何根据输入更改数据属性的明确示例,非常感谢任何帮助。
这是一个代码和框链接 - 但是似乎不喜欢它。
在本地使用npm install并且npm start应该让它按预期运行。
首先,您需要告诉依赖访问器滑块将更改的值。这可以通过使用来完成updateTriggers:
_renderLayers() {
const { data = DATA_URL } = this.props;
return [
new GeoJsonLayer({
// ...
getElevation: f => f.properties[this.state.geoJsonValue],
getFillColor: f => COLOR_SCALE(f.properties[this.state.geoJsonValue]),
updateTriggers: {
getElevation: [this.state.geoJsonValue],
getFillColor: [this.state.geoJsonValue]
}
// ...
})
];
}
Run Code Online (Sandbox Code Playgroud)
要实际使用范围滑块更改此值,您需要onChange在初始化期间添加回调:
constructor(props) {
super(props);
this.state = { hoveredObject: null, geoJsonValue: "hr01" };
this.sliderRef = React.createRef();
this._handleChange = this._handleChange.bind(this);
// ...
}
componentDidMount() {
// Code for slider input
$(this.sliderRef.current).ionRangeSlider({
// ...
onChange: this._handleChange
});
}
_handleChange(data) {
this.setState({
geoJsonValue: `hr0${data.from}`
});
}
render() {
...
<DeckGL ...>
...
</DeckGL>
<div id="sliderstyle">
<input
ref={this.sliderRef}
id="slider"
className="js-range-slider"
name="my_range"
/>
</div>
...
}
Run Code Online (Sandbox Code Playgroud)
基本上就是这样。这是完整的代码
| 归档时间: |
|
| 查看次数: |
1908 次 |
| 最近记录: |