Pigeon.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. export default class Pigeon extends React.Component{
  10. constructor(props) {
  11. super(props)
  12. this.state = {
  13. zoom: 15,
  14. path: [[latitude, longitude]],
  15. coords: [latitude, longitude],
  16. defaultZoom: 15,
  17. distance: 0,
  18. }
  19. }
  20. providers = {
  21. osm: (x, y, z) => {
  22. // use 3 urls to download the tiles faster
  23. const s = String.fromCharCode(97 + ((x + y + z) % 3))
  24. return `https://${s}.tile.openstreetmap.org/${z}/${x}/${y}.png`
  25. },
  26. stamenTerrain: (x, y, z, dpr) => {
  27. return `https://stamen-tiles.a.ssl.fastly.net/terrain/${z}/${x}/${y}${dpr >= 2 ? '@2x' : ''}.jpg`
  28. }
  29. }
  30. updateState = (key, value) => {
  31. this.setState({[key]: value})
  32. this.getDistance()
  33. }
  34. zoomIn = () => {
  35. let newZoom = Math.min(this.state.zoom + 1, 22)
  36. this.setState({zoom: newZoom})
  37. }
  38. zoomOut = () => {
  39. let newZoom = Math.max(this.state.zoom - 1, 0)
  40. this.setState({zoom: newZoom})
  41. }
  42. removeMarkers = () => {
  43. this.setState({path: []})
  44. }
  45. addMarker = (event) => {
  46. const lat = event.latLng[0]
  47. const lng = event.latLng[1]
  48. let pathCopy = this.state.path.slice();
  49. pathCopy.push([lat, lng]);
  50. this.setState({path: pathCopy})
  51. this.getDistance()
  52. }
  53. updateMarker = (event, idx) => {
  54. const lat = event[0]
  55. const lng = event[1]
  56. let pathCopy = this.state.path.slice();
  57. pathCopy[idx] = [lat, lng];
  58. this.setState({path: pathCopy})
  59. this.getDistance()
  60. }
  61. getDistance = () => {
  62. let dst = calcDistance(this.state.path)
  63. this.setState({distance: dst})
  64. }
  65. render(){
  66. let testMarkers = this.props.markers
  67. let markers = <></>
  68. if (testMarkers){
  69. markers = this.props.markers.map((pos, idx) =>
  70. <Marker anchor={pos} key={idx}/>
  71. )
  72. }else{
  73. markers = this.state.path.map((pos, idx) =>
  74. <Draggable
  75. anchor={pos}
  76. key={idx}
  77. offset={[15, 32]}
  78. onDragEnd={(event) => this.updateMarker(event, idx)}
  79. >
  80. <Marker anchor={pos} key={idx} color="grey"/>
  81. </Draggable>
  82. )
  83. }
  84. return (
  85. <div>
  86. <div className="map">
  87. <Map
  88. defaultCenter={this.state.coords}
  89. defaultZoom={this.state.defaultZoom}
  90. zoom={this.state.zoom}
  91. width={mapWidth}
  92. height={mapHeight}
  93. attribution="Pigeon Map"
  94. attributionPrefix={false}
  95. animation={true}
  96. provider={this.providers["osm"]}
  97. dprs={[1, 2]}
  98. animation={true}
  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. </div>
  129. );
  130. }
  131. }