App.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React, { Component } from 'react';
  2. import persons from './persons';
  3. //import ReactDOM from 'react-dom';
  4. import './App.css';
  5. import Tables from './Tables';
  6. class App extends Component {
  7. constructor(props){
  8. super(props);
  9. this.persons = [];
  10. this.state = {
  11. persons
  12. };
  13. };
  14. change = (name) => {
  15. const newPersons = [...this.state.persons];
  16. newPersons.map( person => {
  17. if (name == person.name) {
  18. person.speed = person.speed + Math.floor((Math.random() * 10) + 1)
  19. }
  20. })
  21. this.setState({ persons: newPersons });
  22. }
  23. getLocation = (name) => {
  24. const newPersons = [...this.state.persons];
  25. if (navigator.geolocation) { //check if geolocation is available
  26. navigator.geolocation.getCurrentPosition(function(position){
  27. console.log(position);
  28. newPersons.map( person => {
  29. if (name == person.name) {
  30. person.longitude = position.coords.longitude
  31. person.latitude = position.coords.latitude
  32. }
  33. })
  34. });
  35. }
  36. this.setState({ persons: newPersons });
  37. }
  38. render (){
  39. setTimeout(this.getLocation, 2000)
  40. return (
  41. <div id="wrapper">
  42. <Tables persons={persons}/>
  43. <div id="debug"> </div>
  44. <button
  45. onClick={ () => {
  46. this.getLocation("Bernie");
  47. }}
  48. > Bernie </button>
  49. <button
  50. onClick={ () => {
  51. this.getLocation("Martin");
  52. }}
  53. > Martin </button>
  54. <button
  55. onClick={ () => {
  56. this.getLocation("Simon");
  57. }}
  58. > Simon </button>
  59. <button
  60. onClick={ () => {
  61. this.getLocation("Lukas");
  62. }}
  63. > Lukas </button>
  64. </div>
  65. );
  66. }
  67. }
  68. export default App;