MapGL.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import React from "react";
  2. import ReactMapGL from "react-map-gl";
  3. import {
  4. Marker,
  5. NavigationControl,
  6. FullscreenControl,
  7. ScaleControl,
  8. GeolocateControl,
  9. Layer,
  10. Source,
  11. } from "react-map-gl";
  12. import "./../css/maps.css";
  13. import mapboxToken from "./../config/mapboxToken.js";
  14. import {calcDistance} from "./../helpers/distance";
  15. import {longitude, latitude, mapWidth, mapHeight, markerIcon} from "./../config/variables";
  16. import {Button, Input, Label, Row, Col, Container} from "reactstrap";
  17. import {getRoute} from "./../helpers/mapboxHelper";
  18. import PathButtons from "./../PathButtons";
  19. import {
  20. Editor,
  21. EditingMode,
  22. DrawLineStringMode,
  23. DrawPolygonMode,
  24. } from "react-map-gl-draw";
  25. import EvaluationTable from "./../EvaluationTable";
  26. const drawModes = [
  27. {id: "drawPolyline", text: "Draw Polyline", handler: DrawLineStringMode},
  28. {id: "drawPolygon", text: "Draw Polygon", handler: DrawPolygonMode},
  29. {id: "editing", text: "Edit Feature", handler: EditingMode},
  30. ];
  31. export default class MapGL extends React.Component{
  32. constructor(props){
  33. super(props);
  34. this.state = {
  35. viewport: {
  36. width: mapWidth,
  37. height: mapHeight,
  38. latitude: latitude,
  39. longitude: longitude,
  40. zoom: 14,
  41. bearing: 0,
  42. pitch: 0
  43. },
  44. path: [[longitude, latitude]],
  45. distance: 0,
  46. drawModeId: null,
  47. drawModeHandler: null,
  48. color: "rgb(189,189,189)",
  49. strokeDash: "4,2",
  50. strokeWidth: 2,
  51. fill: "rgb(189,189,189)",
  52. fillOpacity: 0.2
  53. }
  54. this.getRoute = getRoute.bind(this)
  55. }
  56. updateState = (key, value) => {
  57. this.setState({[key]: value})
  58. this.getDistance()
  59. }
  60. addMarker = event => {
  61. if (this.state.drawModeHandler===null){
  62. const lng = event.lngLat[0]
  63. const lat = event.lngLat[1]
  64. this.getRoute(lng, lat);
  65. }
  66. }
  67. updateMarker = (idx, event) => {
  68. const lng = event.lngLat[0]
  69. const lat = event.lngLat[1]
  70. let pathCopy = this.state.path.slice();
  71. pathCopy[idx] = [lng, lat]
  72. this.setState({path: pathCopy})
  73. this.getDistance()
  74. }
  75. getDistance = () => {
  76. let dst = calcDistance(this.state.path)
  77. this.setState({distance: dst})
  78. }
  79. clearPath = () => {
  80. this.setState({path: [], distance: 0})
  81. }
  82. switchDrawMode = (event) => {
  83. const modeId = event.target.value===this.state.drawModeId ? null:event.target.value;
  84. const mode = drawModes.find((m) => m.id===modeId);
  85. const modeHandler = mode ? new mode.handler() : null;
  86. this.setState({"drawModeid": modeId, "drawModeHandler": modeHandler});
  87. };
  88. drawToolbar = () => {
  89. return (
  90. <div style={{maxWidth: "320px"}}>
  91. <Input type="select" onChange={this.switchDrawMode}>
  92. <option value="">-</option>
  93. {drawModes.map((mode) => (
  94. <option key={mode.id} value={mode.id}>
  95. {mode.text}
  96. </option>
  97. ))}
  98. </Input>
  99. </div>
  100. )
  101. }
  102. render() {
  103. let testMarkers = this.props.markers
  104. let markers = <></>
  105. if (testMarkers){
  106. markers = testMarkers.map((pos, idx) => {
  107. return(
  108. <Marker
  109. longitude={pos[0]}
  110. latitude={pos[1]}
  111. captureClick={false}
  112. offsetTop={-20}
  113. offsetLeft={-8}
  114. key={idx}
  115. >
  116. <svg height={20} viewBox="0 0 24 24" style={{fill: "#343a40", stroke: "none", position: "absolute", top: "3px"}}>
  117. <path d={markerIcon} />
  118. </svg>
  119. </Marker>
  120. )}
  121. )
  122. }else{
  123. markers = this.state.path.map((pos, idx) => {
  124. return(
  125. <Marker
  126. longitude={pos[0]}
  127. latitude={pos[1]}
  128. captureClick={false}
  129. draggable={true}
  130. offsetTop={-20}
  131. offsetLeft={-8}
  132. key={idx}
  133. onDragEnd={(e) => this.updateMarker(idx, e)}
  134. >
  135. <svg height={20} viewBox="0 0 24 24" style={{fill: "#343a40", stroke: "none", position: "absolute", top: "3px"}}>
  136. {(idx===0 || idx===this.state.path.length-1 ?
  137. (<path d={markerIcon} /> )
  138. : (<circle cx="10" cy="19" r="5"/>)
  139. )}
  140. </svg>
  141. </Marker>
  142. )
  143. })
  144. }
  145. return (
  146. <div>
  147. <div className="map">
  148. <ReactMapGL
  149. {...this.state.viewport}
  150. onViewportChange={(viewport) => this.setState({viewport})}
  151. mapboxApiAccessToken={mapboxToken}
  152. mapStyle="mapbox://styles/mapbox/streets-v11"
  153. onClick={this.addMarker}
  154. >
  155. <Source
  156. id="routeSource"
  157. type="geojson"
  158. data={{type: "LineString", coordinates: this.state.path}}
  159. >
  160. <Layer
  161. source="routeSource"
  162. id="route"
  163. type="line"
  164. paint={{"line-color": "#888", "line-width": 3}}
  165. layout= {{"line-join": "round", "line-cap": "round"}}
  166. />
  167. </Source>
  168. {markers}
  169. <Editor
  170. clickRadius={12}
  171. mode={this.state.drawModeHandler}
  172. onSelect={(_) => {console.log("on select")}}
  173. featureStyle={({ feature, state }) => {
  174. return {
  175. stroke: this.state.color,
  176. strokeDasharray: this.state.strokeDash,
  177. strokeWidth: this.state.strokeWidth,
  178. fill: this.state.fill,
  179. fillOpacity: this.state.fillOpacity
  180. }
  181. }}
  182. />
  183. <div className="controllers">
  184. <GeolocateControl/>
  185. <FullscreenControl/>
  186. <NavigationControl/>
  187. <ScaleControl/>
  188. </div>
  189. </ReactMapGL>
  190. </div>
  191. <div className="mapInfo">
  192. <Button onClick={this.clearPath}>Clear path</Button>
  193. <Button onClick={this.getDistance}>Calculate Distance</Button>
  194. <p>
  195. Distanz: {Math.round(this.state.distance*100)/100} km
  196. </p>
  197. <PathButtons updateState={this.updateState} path={this.state.path} switchLatLng={true}/>
  198. <br/>
  199. <div>
  200. <Container>
  201. <Row>
  202. <Col xs="4">
  203. {this.drawToolbar()}
  204. </Col>
  205. <Col xs="2">
  206. <Label for="colorPicker">Stroke Dash</Label>
  207. </Col>
  208. <Col xs="6">
  209. <Input type="text" onChange={(e) => this.setState({"strokeDash": e.target.value})} name="strokeDash" id="strokeDash" placeholder="4,2"/>
  210. </Col>
  211. </Row>
  212. <Row>
  213. <Col xs="2">
  214. <Label for="colorPicker">Line Color</Label>
  215. </Col>
  216. <Col xs="2">
  217. <Input type="color" onChange={(e) => this.setState({"color": e.target.value})} name="color" id="colorPicker" placeholder="colorPicker"/>
  218. </Col>
  219. <Col xs="2">
  220. <Label for="weight">Weight</Label>
  221. </Col>
  222. <Col xs="6">
  223. <Input type="range" onChange={(e) => this.setState({"strokeWidth": e.target.value})} name="weight" id="weight" placeholder="weight" min="1" max="20" value={this.state.weight}/>
  224. </Col>
  225. </Row>
  226. <Row>
  227. <Col xs="2">
  228. <Label for="fillColorPicker">Fill Color</Label>
  229. </Col>
  230. <Col xs="2">
  231. <Input type="color" onChange={(e) => this.setState({"fill": e.target.value})} name="fillColorPicker" id="fillColorPicker" placeholder="fillColorPicker"/>
  232. </Col>
  233. <Col xs="2">
  234. <Label for="fillOpacity">Fill Opacity</Label>
  235. </Col>
  236. <Col xs="6">
  237. <Input type="range" onChange={(e) => this.setState({"fillOpacity": e.target.value})} name="fillOpacity" id="fillOpacity" placeholder="fillOpacity" min="0" max="1" step="0.1" value={this.state.fillOpacity}/>
  238. </Col>
  239. </Row>
  240. </Container>
  241. </div>
  242. </div>
  243. <EvaluationTable mapId="mapgl"/>
  244. </div>
  245. );
  246. }
  247. }