Mapbox.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import React from "react";
  2. import ReactMapboxGl, {
  3. Layer,
  4. Feature,
  5. ZoomControl,
  6. ScaleControl,
  7. RotationControl,
  8. Marker,
  9. } from "react-mapbox-gl";
  10. import DrawControl from "react-mapbox-gl-draw";
  11. import "@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css";
  12. import mapboxToken from "./../config/mapboxToken.js";
  13. import {calcDistance} from "./../helpers/distance";
  14. import {longitude, latitude, mapWidth, mapHeight, markerIcon} from "./../config/variables";
  15. import {Button, CustomInput, Label} from "reactstrap";
  16. import {getRoute, mapStyles} from "./../helpers/mapboxHelper";
  17. import PathButtons from "./../PathButtons";
  18. import EvaluationTable from "./../EvaluationTable";
  19. const Map = ReactMapboxGl({ accessToken: mapboxToken });
  20. export default class Mapbox extends React.Component{
  21. constructor(props){
  22. super(props);
  23. this.state = {
  24. latitude: latitude,
  25. longitude: longitude,
  26. zoom: [14],
  27. path: [],
  28. distance: 0,
  29. center: [longitude, latitude],
  30. drawing: false
  31. }
  32. this.getRoute = getRoute.bind(this)
  33. }
  34. updateState = (key, value) => {
  35. this.setState({[key]: value})
  36. this.getDistance()
  37. }
  38. addMarker = (event, click) => {
  39. if (!this.state.drawing){
  40. const lng = click.lngLat.lng
  41. const lat = click.lngLat.lat
  42. this.getRoute(lng, lat);
  43. // let pathCopy = this.state.path.slice();
  44. // pathCopy.push([lng, lat]);
  45. // this.setState({path: pathCopy})
  46. this.getDistance()
  47. }
  48. }
  49. updateMarker = (idx, event) => {
  50. const lng = event.lngLat.lng
  51. const lat = event.lngLat.lat
  52. let pathCopy = this.state.path.slice();
  53. pathCopy[idx] = [lng, lat]
  54. this.setState({path: pathCopy})
  55. this.getDistance()
  56. }
  57. getDistance = () => {
  58. let dst = calcDistance(this.state.path)
  59. this.setState({distance: dst})
  60. }
  61. clearPath = () => {
  62. this.setState({path: [], distance: 0})
  63. }
  64. render() {
  65. let testMarkers = this.props.markers
  66. let testMarkerType = this.props.markerType
  67. let markers = <></>
  68. if (testMarkers){
  69. if (testMarkerType === "feature"){
  70. markers = (
  71. <Layer type="symbol" id="marker" layout={{"icon-image": "marker-15"}}>
  72. {testMarkers.map((pos, idx) =>
  73. <Feature
  74. coordinates={pos}
  75. key={idx}
  76. />
  77. )}
  78. </Layer>
  79. )
  80. } else {
  81. markers = testMarkers.map((pos, idx) =>
  82. <Marker
  83. coordinates={pos}
  84. key={idx}
  85. anchor="bottom"
  86. >
  87. <svg height={15} viewBox="0 0 24 24" style={{fill: "#343a40"}}>
  88. <path d={markerIcon} />
  89. </svg>
  90. </Marker>
  91. )}
  92. } else {
  93. markers = (
  94. <Layer type="symbol" id="marker"
  95. layout={{"icon-image": "marker-15", "icon-anchor": "bottom", "icon-offset": [0,3]}}>
  96. {this.state.path.map((pos, idx) =>
  97. <Feature
  98. coordinates={pos}
  99. key={idx}
  100. draggable={true}
  101. onDragEnd={(e) => this.updateMarker(idx, e)}
  102. />
  103. )}
  104. </Layer>
  105. )
  106. }
  107. return (
  108. <div>
  109. <div className="map">
  110. <Map
  111. style={mapStyles.bright}
  112. containerStyle={{
  113. height: mapHeight,
  114. width: mapWidth
  115. }}
  116. center={this.state.center}
  117. zoom={this.state.zoom}
  118. onClick={(e, c) => this.addMarker(e, c)}
  119. >
  120. <Layer type="line"
  121. layout={{"line-join": "round", "line-cap": "round"}}
  122. paint={{"line-color": "#888", "line-width": 3}}
  123. >
  124. <Feature coordinates = {this.state.path}/>
  125. </Layer>
  126. {markers}
  127. <ZoomControl position={"top-right"}/>
  128. <ScaleControl position={"bottom-right"}/>
  129. <RotationControl position={"top-right"}/>
  130. <DrawControl/>
  131. </Map>
  132. </div>
  133. <div className="mapInfo">
  134. <Button onClick={this.clearPath}>Clear path</Button>
  135. <Button onClick={this.getDistance}>Calculate Distance</Button>
  136. <p>
  137. Distanz: {Math.round(this.state.distance*100)/100} km
  138. </p>
  139. <CustomInput type="switch" onChange={() => this.setState({"drawing": !this.state.drawing})} id="drawMode" name="drawMode"/>
  140. </div>
  141. <EvaluationTable mapId="mapbox"/>
  142. </div>
  143. );
  144. }
  145. }