Leaflet.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React from "react";
  2. import {
  3. Map,
  4. TileLayer,
  5. Marker,
  6. Popup,
  7. ZoomControl,
  8. Polyline
  9. } from "react-leaflet";
  10. import "./css/maps.css";
  11. const LONGITUDE = 14.320570
  12. const LATITUDE = 48.336950
  13. export default class Leaflet extends React.Component{
  14. state = {
  15. latitude: LATITUDE,
  16. longitude: LONGITUDE,
  17. zoom: 15,
  18. path: [[48.295742, 14.286967], [48.328529, 14.322428]],
  19. distance: 0
  20. }
  21. newPathPoint = event => {
  22. const { lat, lng } = event.latlng
  23. console.log("Clicked at", lat, "/", lng)
  24. let pathCopy = this.state.path.slice();
  25. pathCopy.push([lat, lng]);
  26. this.setState({path: pathCopy})
  27. this.calcDistance()
  28. }
  29. updateMarker = (idx, event) => {
  30. let {lat, lng} = event.target.getLatLng();
  31. console.log(lat, lng, idx)
  32. let pathCopy = this.state.path.slice();
  33. pathCopy[idx] = [lat, lng]
  34. this.setState({path: pathCopy})
  35. this.calcDistance()
  36. }
  37. calcDistance = () => {
  38. let dst = 0;
  39. let pathCopy = this.state.path.slice();
  40. for(let i=0; i<pathCopy.length-1; i++) {
  41. /* let currLat = pathCopy[i][0];
  42. let currLng = pathCopy[i][1];
  43. let nextLat = pathCopy[i+1][0];
  44. let nextLng = pathCopy[i+1][1];
  45. let dstLat = (currLat - nextLat) ** 2
  46. let dstLng = (currLng - nextLng) ** 2
  47. dst += Math.sqrt(dstLat + dstLng) * 108*/
  48. var radlat1 = Math.PI * pathCopy[i][0]/180;
  49. var radlat2 = Math.PI * pathCopy[i+1][0]/180;
  50. var theta = pathCopy[i][1]-pathCopy[i+1][1];
  51. var radtheta = Math.PI * theta/180;
  52. var d = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
  53. if (d > 1) {
  54. d = 1;
  55. }
  56. dst += Math.acos(d) * 180/Math.PI * 60 * 1.1515 * 1.609344;
  57. }
  58. this.setState({distance: dst})
  59. }
  60. clearPath = () => {
  61. this.setState({path: [], distance: 0})
  62. }
  63. render() {
  64. const position = [this.state.latitude, this.state.longitude]
  65. return (
  66. <div>
  67. <Map
  68. center={position}
  69. zoom={this.state.zoom}
  70. zoomControl={false}
  71. style={{width: 600, height: 400}}
  72. onClick={this.newPathPoint}
  73. >
  74. <TileLayer
  75. attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  76. url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
  77. />
  78. <ZoomControl position="bottomright" />
  79. <Marker position={position} onClick={this.newPathPoint}>
  80. <Popup>
  81. JKU: Ente
  82. </Popup>
  83. </Marker>
  84. {this.state.path.map((pos, idx) =>
  85. <Marker
  86. position={pos}
  87. draggable
  88. key={idx}
  89. onDragend={(e) => this.updateMarker(idx, e)}
  90. >
  91. <Popup>
  92. {idx}
  93. </Popup>
  94. </Marker>
  95. )}
  96. <Polyline color="grey" positions={this.state.path} />
  97. </Map>
  98. <div>
  99. <button onClick={this.clearPath}>Clear path</button>
  100. <button onClick={this.calcDistance}>Calculate Distance</button>
  101. <p>
  102. Distanz: {Math.round(this.state.distance*100)/100} km
  103. </p>
  104. </div>
  105. </div>
  106. );
  107. }
  108. }