Google.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React from "react";
  2. import GoogleMapReact from 'google-map-react';
  3. import "./css/maps.css";
  4. import apiKey from './config/googleApiKey.js';
  5. import {calcDistance} from './helpers/distance';
  6. import {longitude, latitude, mapWidth, mapHeight, markerIcon, testPath} from "./config/variables";
  7. const svgStyle = {
  8. fill: "#343a40",
  9. stroke: "none",
  10. top: "-20",
  11. left: "-8",
  12. position: "absolute"
  13. };
  14. const Marker = ({ text }) =>
  15. <div>
  16. <svg height={20} viewBox="0 0 24 24" style={svgStyle}>
  17. <path d={markerIcon}/>
  18. </svg>
  19. <p>{text}</p>
  20. </div>;
  21. export default class Google extends React.Component{
  22. state = {
  23. latitude: latitude,
  24. longitude: longitude,
  25. zoom: 15,
  26. distance: 0,
  27. path: testPath,
  28. draggable: true
  29. }
  30. onMarkerInteraction = (childKey, childProps, mouse) => {
  31. this.setState({draggable: false});
  32. }
  33. addMarker = (event) => {
  34. const lng = event.lng
  35. const lat = event.lat
  36. console.log("Clicked at", lat, "/", lng)
  37. let pathCopy = this.state.path.slice();
  38. pathCopy.push([lat, lng]);
  39. this.setState({path: pathCopy})
  40. this.getDistance()
  41. }
  42. updateMarker = (childKey, childProps, mouse) => {
  43. const lng = mouse.lng
  44. const lat = mouse.lat
  45. console.log("new position (marker", childKey, "):", lat, lng)
  46. let pathCopy = this.state.path.slice();
  47. pathCopy[childKey] = [lat, lng]
  48. this.setState({path: pathCopy, draggable: true})
  49. this.getDistance()
  50. }
  51. getDistance = () => {
  52. let dst = calcDistance(this.state.path)
  53. this.setState({distance: dst})
  54. }
  55. clearPath = () => {
  56. this.setState({path: [], distance: 0})
  57. }
  58. render() {
  59. const position = [this.state.latitude, this.state.longitude]
  60. return (
  61. <div>
  62. <div className="map" style={{height: mapHeight, width: mapWidth}}>
  63. <GoogleMapReact
  64. bootstrapURLKeys={{ key: apiKey }}
  65. defaultCenter={position}
  66. defaultZoom={this.state.zoom}
  67. yesIWantToUseGoogleMapApiInternals
  68. layerTypes={['TrafficLayer', 'TransitLayer']}
  69. onClick={(e, c) => this.addMarker(e, c)}
  70. draggable={this.state.draggable}
  71. onChildMouseDown={this.onMarkerInteraction}
  72. onChildMouseUp={this.updateMarker}
  73. onChildMouseMove={this.onMarkerInteraction}
  74. onChildClick={() => console.log('child click')}
  75. >
  76. {this.state.path.map((pos, idx) =>
  77. <Marker
  78. lat={pos[0]}
  79. lng={pos[1]}
  80. text={idx}
  81. key={idx}
  82. draggable={true}
  83. onDragEnd={(e) => this.updateMarker(idx, e)}
  84. />
  85. )}
  86. </GoogleMapReact>
  87. </div>
  88. <div className="mapInfo">
  89. <button onClick={this.clearPath}>Clear path</button>
  90. <button onClick={this.getDistance}>Calculate Distance</button>
  91. <p>
  92. Distanz: {Math.round(this.state.distance*100)/100} km
  93. </p>
  94. </div>
  95. </div>
  96. );
  97. }
  98. }