-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApp.js
99 lines (86 loc) · 2.96 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React, { Component } from "react";
import ReactMapboxGl from "react-mapbox-gl";
import _ from "lodash";
import MapboxGl from "mapbox-gl";
import { ReactMapboxGlSpiderifier } from "./lib";
import "./App.css";
const Map = ReactMapboxGl({
accessToken: process.env.REACT_APP_MAPBOX_GL_TOKEN
});
const mapProps = {
style: "mapbox://styles/mapbox/streets-v8"
};
class App extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
map: null
};
}
componentDidMount() {
this.setState({ data: this.getRandomData() });
}
getEventHandlers() {
return {
onClick: (properties, coords, offset) => this.renderPopup(properties, coords, offset),
onMouseDown: (properties, coords, offset) =>
console.log(`Receive event onMouseDown at properties: ${properties}, coords: ${coords}, offset: ${offset}`),
onMouseEnter: (properties, coords, offset) =>
console.log(`Receive event onMouseEnter at properties: ${properties}, coords: ${coords}, offset: ${offset}`),
onMouseLeave: (properties, coords, offset) =>
console.log(`Receive event onMouseLeave at properties: ${properties}, coords: ${coords}, offset: ${offset}`),
onMouseMove: (properties, coords, offset) =>
console.log(`Receive event onMouseMove at properties: ${properties}, coords: ${coords}, offset: ${offset}`),
onMouseOut: (properties, coords, offset) =>
console.log(`Receive event onMouseOut at properties: ${properties}, coords: ${coords}, offset: ${offset}`),
onMouseOver: (properties, coords, offset) =>
console.log(`Receive event onMouseOver at properties: ${properties}, coords: ${coords}, offset: ${offset}`),
onMouseUp: (properties, coords, offset) =>
console.log(`Receive event at onMouseUp properties: ${properties}, coords: ${coords}, offset: ${offset}`)
};
}
getRandomData() {
const n = this.randomNumber(5, 30);
console.log(`Rendering new spiral with ${n} elements`);
return _.times(n, index => this.randomNumber(100, 10000));
}
onStyleLoad = map => {
this.setState({ map });
};
randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
renderPopup(properties, coordinates, offset) {
if (this.currentPopup) {
this.currentPopup.remove();
}
setTimeout(() => {
this.currentPopup = new MapboxGl.Popup({ offset })
.setLngLat(coordinates)
.setHTML(`Some description for node ${properties.value}`)
.addTo(this.state.map);
});
}
renderSpiderifierContent(key, value) {
return (
<div className="spiderifier-marker-content" key={key} properties={{ value }}>
<div>{value}</div>
</div>
);
}
render() {
return (
<div className="App">
<Map {...mapProps} onStyleLoad={this.onStyleLoad}>
{this.state.map && (
<ReactMapboxGlSpiderifier coordinates={[-0.2268, 51.5361]} {...this.getEventHandlers()}>
{this.state.data.map((n, index) => this.renderSpiderifierContent(index, n))}
</ReactMapboxGlSpiderifier>
)}
</Map>
</div>
);
}
}
export default App;