Mapbox.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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} 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: [[longitude, latitude], [longitude+0.003, latitude]],
  28. distance: 0,
  29. center: [longitude, latitude]
  30. }
  31. this.getRoute = getRoute.bind(this)
  32. }
  33. updateState = (key, value) => {
  34. this.setState({[key]: value})
  35. this.getDistance()
  36. }
  37. addMarker = (event, click) => {
  38. const lng = click.lngLat.lng
  39. const lat = click.lngLat.lat
  40. this.getRoute(lng, lat);
  41. // let pathCopy = this.state.path.slice();
  42. // pathCopy.push([lng, lat]);
  43. // this.setState({path: pathCopy})
  44. this.getDistance()
  45. }
  46. updateMarker = (idx, event) => {
  47. const lng = event.lngLat.lng
  48. const lat = event.lngLat.lat
  49. let pathCopy = this.state.path.slice();
  50. pathCopy[idx] = [lng, lat]
  51. this.setState({path: pathCopy})
  52. this.getDistance()
  53. }
  54. getDistance = () => {
  55. let dst = calcDistance(this.state.path)
  56. this.setState({distance: dst})
  57. }
  58. clearPath = () => {
  59. this.setState({path: [], distance: 0})
  60. }
  61. render() {
  62. let testMarkers = this.props.markers
  63. let testMarkerType = this.props.markerType
  64. let markers = <></>
  65. if (testMarkers){
  66. if (testMarkerType === "feature"){
  67. markers = (
  68. <Layer type="symbol" id="marker" layout={{"icon-image": "marker-15"}}>
  69. {testMarkers.map((pos, idx) =>
  70. <Feature
  71. coordinates={pos}
  72. key={idx}
  73. />
  74. )}
  75. </Layer>
  76. )
  77. } else {
  78. markers = testMarkers.map((pos, idx) =>
  79. <Marker
  80. coordinates={pos}
  81. key={idx}
  82. anchor="bottom"
  83. >
  84. <svg height={15} viewBox="0 0 24 24" style={{fill: "#343a40"}}>
  85. <path d={markerIcon} />
  86. </svg>
  87. </Marker>
  88. )}
  89. } else {
  90. markers = (
  91. <Layer type="symbol" id="marker"
  92. layout={{"icon-image": "marker-15", "icon-anchor": "bottom", "icon-offset": [0,3]}}>
  93. {this.state.path.map((pos, idx) =>
  94. <Feature
  95. coordinates={pos}
  96. key={idx}
  97. draggable={true}
  98. onDragEnd={(e) => this.updateMarker(idx, e)}
  99. />
  100. )}
  101. </Layer>
  102. )
  103. }
  104. return (
  105. <div>
  106. <div className="map">
  107. <Map
  108. style={mapStyles.bright}
  109. containerStyle={{
  110. height: mapHeight,
  111. width: mapWidth
  112. }}
  113. center={this.state.center}
  114. zoom={this.state.zoom}
  115. onClick={(e, c) => this.addMarker(e, c)}
  116. >
  117. <Layer type="line"
  118. layout={{"line-join": "round", "line-cap": "round"}}
  119. paint={{"line-color": "#888", "line-width": 3}}
  120. >
  121. <Feature coordinates = {this.state.path}/>
  122. </Layer>
  123. {markers}
  124. <ZoomControl position={"top-right"}/>
  125. <ScaleControl position={"bottom-right"}/>
  126. <RotationControl position={"top-right"}/>
  127. <DrawControl/>
  128. </Map>
  129. </div>
  130. <div className="mapInfo">
  131. <Button onClick={this.clearPath}>Clear path</Button>
  132. <Button onClick={this.getDistance}>Calculate Distance</Button>
  133. <p>
  134. Distanz: {Math.round(this.state.distance*100)/100} km
  135. </p>
  136. <PathButtons updateState={this.updateState} path={this.state.path} switchLatLng={true}/>
  137. </div>
  138. <EvaluationTable mapId="mapbox"/>
  139. </div>
  140. );
  141. }
  142. }