Mapbox.js 4.2 KB

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