Pigeon.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React from "react";
  2. import Map from "pigeon-maps";
  3. import Marker from "pigeon-marker";
  4. import Overlay from "pigeon-overlay";
  5. import {longitude, latitude, mapWidth, mapHeight} from "./config/variables";
  6. import {Button} from "reactstrap";
  7. export default class Pigeon extends React.Component{
  8. providers = {
  9. osm: (x, y, z) => {
  10. // use 3 urls to download the tiles faster
  11. const s = String.fromCharCode(97 + ((x + y + z) % 3))
  12. return `https://${s}.tile.openstreetmap.org/${z}/${x}/${y}.png`
  13. },
  14. stamenTerrain: (x, y, z, dpr) => {
  15. return `https://stamen-tiles.a.ssl.fastly.net/terrain/${z}/${x}/${y}${dpr >= 2 ? '@2x' : ''}.jpg`
  16. }
  17. }
  18. zoomIn = () => {
  19. let newZoom = Math.min(this.props.mapConfig.zoom + 1, 22)
  20. this.props.updateMapState("zoom", newZoom)
  21. }
  22. zoomOut = () => {
  23. let newZoom = Math.max(this.props.mapConfig.zoom - 1, 0)
  24. this.props.updateMapState("zoom", newZoom)
  25. }
  26. removeMarkers = () => {
  27. this.props.updateMapState("path", [])
  28. }
  29. addMarker = (event) => {
  30. const lat = event.latLng[0]
  31. const lng = event.latLng[1]
  32. console.log("Clicked at", lat, "/", lng, event)
  33. let pathCopy = this.props.mapConfig.path.slice();
  34. pathCopy.push([lat, lng]);
  35. this.props.updateMapState("path", pathCopy)
  36. this.props.getDistance()
  37. }
  38. render(){
  39. const mapConfig = this.props.mapConfig;
  40. return (
  41. <div>
  42. <div className="map">
  43. <Map
  44. center={mapConfig.coords}
  45. defaultZoom={mapConfig.defaultZoom}
  46. zoom={mapConfig.zoom}
  47. width={mapWidth}
  48. height={mapHeight}
  49. attribution="Pigeon Map"
  50. attributionPrefix={false}
  51. animation={true}
  52. provider={this.providers["osm"]}
  53. dprs={[1, 2]}
  54. onClick={(event) => this.addMarker(event)}
  55. >
  56. {mapConfig.path.map((pos, idx) =>
  57. <Marker anchor={pos} key={idx}/>
  58. )}
  59. <Overlay anchor={[latitude, longitude]} offset={[120, 79]}>
  60. {/*<img src='pigeon.png' width={240} height={158} alt='pigeon' />*/}
  61. </Overlay>
  62. </Map>
  63. </div>
  64. <div className="mapInfo">
  65. <Button onClick={this.zoomIn}>+</Button>
  66. <Button onClick={this.zoomOut}>-</Button>
  67. <Button onClick={this.removeMarkers}>Clear path</Button>
  68. <Button onClick={this.props.getDistance}>Calculate Distance</Button>
  69. <p>
  70. Distanz: {Math.round(mapConfig.distance*100)/100} km
  71. </p>
  72. </div>
  73. </div>
  74. );
  75. }
  76. }