forked from alex3165/react-mapbox-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall-shapes.js
171 lines (147 loc) · 4.2 KB
/
all-shapes.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, { Component } from "react";
import ReactMapboxGl, { Layer, Feature, ScaleControl, ZoomControl } from "../src/index";
import route from "./route.json";
import config from "./config.json";
import style from "./style.json";
const { accessToken } = config;
const containerStyle = {
height: "100vh",
width: "100%"
};
const polygonCoords = [[
[-0.13235092163085938,51.518250335096376],
[-0.1174163818359375,51.52433860667918],
[-0.10591506958007812,51.51974577545329],
[-0.10831832885742188,51.51429786349477],
[-0.12531280517578122,51.51429786349477],
[-0.13200759887695312,51.517823057404094]
]];
const multiPolygonCoords = [
[[
[-0.18235092163085938,51.518250335096376],
[-0.1674163818359375,51.52433860667918],
[-0.15591506958007812,51.51974577545329],
[-0.15831832885742188,51.51429786349477],
[-0.17531280517578122,51.51429786349477],
[-0.18200759887695312,51.517823057404094]
]],
[[
[-0.18235092163085938,51.538250335096376],
[-0.1674163818359375,51.54433860667918],
[-0.15591506958007812,51.53974577545329],
[-0.15831832885742188,51.53429786349477],
[-0.17531280517578122,51.53429786349477],
[-0.18200759887695312,51.537823057404094]
]]
];
const markerCoord = [
-0.2416815,
51.5285582
];
const mappedRoute = route.points.map(point => [ point.lat, point.lng ]);
export default class AllShapes extends Component {
state = {
popup: null,
center: [0.2174037, 51.6476704],
circleRadius: 30,
routeIndex: 0
};
componentWillMount() {
setTimeout(() => {
this.setState({
center: [-0.120736, 51.5118219],
circleRadius: 10
});
}, 6000);
setInterval(() => {
this.setState({
routeIndex: this.state.routeIndex + 1
});
}, 8000);
}
_onClickMarker = ({ feature }) => {
this.setState({
center: feature.geometry.coordinates
});
};
_onClickMap(map) {
console.log("Clicked on the map : ", map);
}
_onStyleLoad(map) {
console.log("Style loaded: ", map);
}
_onHover({ map }) {
map.getCanvas().style.cursor = "pointer";
}
_onZoom(map) {
console.log("Zoom level changed to ", map.getZoom());
}
_onEndHover({ map }) {
map.getCanvas().style.cursor = "";
}
_polygonClicked = ({ feature }) => {
console.log("Polygon clicked", feature.geometry.coordinates);
};
render() {
return (
<ReactMapboxGl
style={style}
onClick={this._onClickMap}
onZoom={this._onZoom}
onStyleLoad={this._onStyleLoad}
accessToken={accessToken}
center={this.state.center}
movingMethod="jumpTo"
containerStyle={containerStyle}>
<ScaleControl/>
<ZoomControl/>
<Layer
type="symbol"
layout={{ "icon-image": "harbor-15" }}>
<Feature
coordinates={markerCoord}
onHover={this._onHover}
onEndHover={this._onEndHover}
onClick={this._onClickMarker}/>
</Layer>
<Layer
id="mapbox-route-example"
type="line"
sourceId="route"
layout={{
"line-join": "round",
"line-cap": "round"
}}
paint={{
"line-color": "#888",
"line-width": 8
}}/>
<Layer
type="line"
layout={{ "line-cap": "round", "line-join": "round" }}
paint={{ "line-color": "#4790E5", "line-width": 12 }}>
<Feature coordinates={mappedRoute}/>
</Layer>
<Layer
type="circle"
paint={{ "circle-radius": this.state.circleRadius, "circle-color": "#E54E52", "circle-opacity": .8 }}>
<Feature coordinates={mappedRoute[this.state.routeIndex]}/>
</Layer>
<Layer
type="fill"
paint={{ "fill-color": "#6F788A", "fill-opacity": .7 }}>
<Feature
onClick={this._polygonClicked}
coordinates={polygonCoords}/>
</Layer>
<Layer
type="fill"
paint={{ "fill-color": "#3bb2d0", "fill-opacity": .5 }}>
<Feature
onClick={this._polygonClicked}
coordinates={multiPolygonCoords}/>
</Layer>
</ReactMapboxGl>
);
}
}