Pigeon.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import React from "react";
  2. import Map from "pigeon-maps";
  3. import {Marker, Overlay} from "pigeon-maps";
  4. import Draggable from "pigeon-draggable";
  5. import {longitude, latitude, mapWidth, mapHeight} from "./../config/variables";
  6. import {Button} from "reactstrap";
  7. import PathButtons from "./../PathButtons";
  8. import {calcDistance} from "./../helpers/distance";
  9. import EvaluationTable from "./../EvaluationTable";
  10. export default class Pigeon extends React.Component{
  11. constructor(props) {
  12. super(props)
  13. this.state = {
  14. zoom: 15,
  15. path: [[latitude, longitude]],
  16. coords: [latitude, longitude],
  17. defaultZoom: 15,
  18. distance: 0,
  19. }
  20. }
  21. providers = {
  22. osm: (x, y, z) => {
  23. // use 3 urls to download the tiles faster
  24. const s = String.fromCharCode(97 + ((x + y + z) % 3))
  25. return `https://${s}.tile.openstreetmap.org/${z}/${x}/${y}.png`
  26. },
  27. stamenTerrain: (x, y, z, dpr) => {
  28. return `https://stamen-tiles.a.ssl.fastly.net/terrain/${z}/${x}/${y}${dpr >= 2 ? '@2x' : ''}.jpg`
  29. }
  30. }
  31. updateState = (key, value) => {
  32. this.setState({[key]: value})
  33. this.getDistance()
  34. }
  35. zoomIn = () => {
  36. let newZoom = Math.min(this.state.zoom + 1, 22)
  37. this.setState({zoom: newZoom})
  38. }
  39. zoomOut = () => {
  40. let newZoom = Math.max(this.state.zoom - 1, 0)
  41. this.setState({zoom: newZoom})
  42. }
  43. removeMarkers = () => {
  44. this.setState({path: []})
  45. }
  46. addMarker = (event) => {
  47. const lat = event.latLng[0]
  48. const lng = event.latLng[1]
  49. let pathCopy = this.state.path.slice();
  50. pathCopy.push([lat, lng]);
  51. this.setState({path: pathCopy})
  52. this.getDistance()
  53. }
  54. updateMarker = (event, idx) => {
  55. const lat = event[0]
  56. const lng = event[1]
  57. let pathCopy = this.state.path.slice();
  58. pathCopy[idx] = [lat, lng];
  59. this.setState({path: pathCopy})
  60. this.getDistance()
  61. }
  62. getDistance = () => {
  63. let dst = calcDistance(this.state.path)
  64. this.setState({distance: dst})
  65. }
  66. render(){
  67. let testMarkers = this.props.markers
  68. let markers = <></>
  69. if (testMarkers){
  70. markers = this.props.markers.map((pos, idx) =>
  71. <Marker anchor={pos} key={idx}/>
  72. )
  73. }else{
  74. markers = this.state.path.map((pos, idx) =>
  75. <Draggable
  76. anchor={pos}
  77. key={idx}
  78. offset={[15, 32]}
  79. onDragEnd={(event) => this.updateMarker(event, idx)}
  80. >
  81. <Marker anchor={pos} key={idx} color="grey"/>
  82. </Draggable>
  83. )
  84. }
  85. return (
  86. <div>
  87. <div className="map">
  88. <Map
  89. defaultCenter={this.state.coords}
  90. defaultZoom={this.state.defaultZoom}
  91. zoom={this.state.zoom}
  92. width={mapWidth}
  93. height={mapHeight}
  94. attribution="Pigeon Map"
  95. attributionPrefix={false}
  96. animation={true}
  97. provider={this.providers["osm"]}
  98. dprs={[1, 2]}
  99. mouseEvents={true}
  100. onClick={(event) => this.addMarker(event)}
  101. >
  102. {markers}
  103. <Overlay anchor={[latitude, longitude]} offset={[120, 79]}>
  104. {/*<img src='pigeon.png' width={240} height={158} alt='pigeon' />*/}
  105. </Overlay>
  106. {/*<Overlay
  107. anchor={[latitude, longitude]}
  108. offset={[60, 87]}
  109. style={{
  110. clipPath:
  111. 'polygon(100% 0, 83% 0, 79% 15%, 0 68%, 0 78%, 39% 84%, 43% 96%, 61% 100%, 79% 90%, 69% 84%, 88% 71%, 100% 15%)',
  112. }}
  113. >
  114. <p style={{backgroundColor: "red"}}>test overlay</p>
  115. </Overlay>*/}
  116. </Map>
  117. </div>
  118. <div className="mapInfo">
  119. <Button onClick={this.zoomIn}>+</Button>
  120. <Button onClick={this.zoomOut}>-</Button>
  121. <Button onClick={this.removeMarkers}>Clear path</Button>
  122. <Button onClick={this.getDistance}>Calculate Distance</Button>
  123. <p>
  124. Distanz: {Math.round(this.state.distance*100)/100} km
  125. </p>
  126. <PathButtons updateState={this.updateState} path={this.state.path}/>
  127. </div>
  128. <EvaluationTable mapId="pigeon"/>
  129. </div>
  130. );
  131. }
  132. }