App.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import React, { Component } from 'react'
  2. import './App.css'
  3. import Map from 'pigeon-maps'
  4. import Marker from 'pigeon-marker'
  5. import { geolocated } from "react-geolocated"
  6. import Tables from './Tables';
  7. // please change this if you take some code from here.
  8. // otherwise the demo page will run out of credits and that would be very sad :(
  9. const MAPBOX_ACCESS_TOKEN = 'pk.eyJ1IjoicGlnZW9uLW1hcHMiLCJhIjoiY2l3eW01Y2E2MDA4dzJ6cWh5dG9pYWlwdiJ9.cvdCf-7PymM1Y3xp5j71NQ'
  10. const mapbox = (mapboxId, accessToken) => (x, y, z, dpr) => {
  11. return `https://api.mapbox.com/styles/v1/mapbox/${mapboxId}/tiles/256/${z}/${x}/${y}${dpr >= 2 ? '@2x' : ''}?access_token=${accessToken}`
  12. }
  13. const providers = {
  14. osm: (x, y, z) => {
  15. const s = String.fromCharCode(97 + (x + y + z) % 3)
  16. return `https://${s}.tile.openstreetmap.org/${z}/${x}/${y}.png`
  17. },
  18. wikimedia: (x, y, z, dpr) => {
  19. return `https://maps.wikimedia.org/osm-intl/${z}/${x}/${y}${dpr >= 2 ? '@2x' : ''}.png`
  20. },
  21. stamen: (x, y, z, dpr) => {
  22. return `https://stamen-tiles.a.ssl.fastly.net/terrain/${z}/${x}/${y}${dpr >= 2 ? '@2x' : ''}.jpg`
  23. },
  24. streets: mapbox('streets-v10', MAPBOX_ACCESS_TOKEN),
  25. satellite: mapbox('satellite-streets-v10', MAPBOX_ACCESS_TOKEN),
  26. outdoors: mapbox('outdoors-v10', MAPBOX_ACCESS_TOKEN),
  27. light: mapbox('light-v9', MAPBOX_ACCESS_TOKEN),
  28. dark: mapbox('dark-v9', MAPBOX_ACCESS_TOKEN)
  29. }
  30. const markers = {
  31. Bernie: [[48.1542150, 13.9952130], 13],
  32. Martin: [[48.1642150, 13.9952130], 12],
  33. Simon: [[48.1642160, 13.9852140], 11],
  34. Lukas: [[48.1542134, 14.0052118], 10],
  35. }
  36. const lng2tile = (lon, zoom) => (lon + 180) / 360 * Math.pow(2, zoom)
  37. const lat2tile = (lat, zoom) => (1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * Math.pow(2, zoom)
  38. function isMapBox (provider) {
  39. return provider === 'streets' || provider === 'satellite' || provider === 'outdoors' || provider === 'light' || provider === 'dark'
  40. }
  41. const MapboxAttribution = () => (
  42. <span className='map-attribution'>
  43. <span>© <a href='https://www.mapbox.com/about/maps/'>Mapbox</a></span>{' | '}
  44. <span>© <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a></span>{' | '}
  45. </span>
  46. )
  47. const StamenAttribution = () => (
  48. <span className='map-attribution'>
  49. Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://www.openstreetmap.org/copyright">ODbL</a>.
  50. </span>
  51. )
  52. const WikimediaAttribution = () => (
  53. <span className='map-attribution'>
  54. Map tiles by <a href='https://foundation.wikimedia.org/w/index.php?title=Maps_Terms_of_Use#Where_does_the_map_data_come_from.3F'>Wikimedia</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>
  55. </span>
  56. )
  57. export default class App extends Component {
  58. constructor (props) {
  59. super(props)
  60. this.apiUrl = "http://alpaga.hammerle.me:9876";
  61. this.name = null;
  62. this.state = {
  63. persons: [],
  64. center: [48.1665, 14.0223], //start pos
  65. zoom: 12,
  66. provider: 'osm',
  67. metaWheelZoom: false,
  68. twoFingerDrag: false,
  69. animate: true,
  70. animating: false,
  71. zoomSnap: true,
  72. mouseEvents: true,
  73. touchEvents: true,
  74. minZoom: 1,
  75. maxZoom: 18,
  76. dragAnchor: [48.1665, 14.0223]
  77. };
  78. this.componentDidMount = this.componentDidMount.bind(this)
  79. }
  80. zoomIn = () => {
  81. this.setState({
  82. zoom: Math.min(this.state.zoom + 1, 18)
  83. })
  84. }
  85. zoomOut = () => {
  86. this.setState({
  87. zoom: Math.max(this.state.zoom - 1, 1)
  88. })
  89. }
  90. handleBoundsChange = ({ center, zoom, bounds, initial }) => {
  91. if (initial) {
  92. console.log('Got initial bounds: ', bounds)
  93. }
  94. this.setState({ center, zoom })
  95. }
  96. handleClick = ({ event, latLng, pixel }) => {
  97. console.log('Map clicked!', latLng, pixel)
  98. }
  99. handleMarkerClick = ({ event, payload, anchor }) => {
  100. console.log(`Marker #${payload} clicked at: `, anchor)
  101. }
  102. handleAnimationStart = () => {
  103. this.setState({ animating: true })
  104. }
  105. handleAnimationStop = () => {
  106. this.setState({ animating: false })
  107. }
  108. // is called once when loading the page
  109. componentDidMount() {
  110. this.reloadLocations();
  111. setInterval(this.updateLocation, 4000);
  112. }
  113. reloadLocations = () => {
  114. fetch(this.apiUrl + "/users")
  115. .then(response => response.json())
  116. .then(data =>{
  117. console.log(data["users"]);
  118. this.setState({ persons: data["users"] })
  119. }).catch(error => {
  120. console.log(error);
  121. });
  122. }
  123. receivedPosition = (position) => {
  124. const axios = require('axios');
  125. axios.put(this.apiUrl + "/report-location", {
  126. "longitude": position.coords.longitude,
  127. "latitude": position.coords.latitude,
  128. "timestamp": position.timestamp/1000, // in seconds
  129. "name": this.name
  130. }).then(this.reloadLocations)
  131. .catch(error => {
  132. console.log(error);
  133. });
  134. }
  135. updateLocation = () => {
  136. if (this.name!=null && navigator.geolocation) { //check if geolocation is available
  137. navigator.geolocation.getCurrentPosition(this.receivedPosition);
  138. }
  139. }
  140. render () {
  141. const { center, zoom, provider, animate, metaWheelZoom, twoFingerDrag, zoomSnap, mouseEvents, touchEvents, animating, minZoom, maxZoom } = this.state
  142. return (
  143. <div style={{textAlign: 'center', marginTop: 50, marginLeft: 0}}>
  144. <div style={{maxWidth: 400, margin: '0 auto'}}>
  145. <Map
  146. limitBounds='edge'
  147. center={center}
  148. zoom={zoom}
  149. provider={providers[provider]}
  150. dprs={[1, 2]}
  151. onBoundsChanged={this.handleBoundsChange}
  152. onClick={this.handleClick}
  153. onAnimationStart={this.handleAnimationStart}
  154. onAnimationStop={this.handleAnimationStop}
  155. animate={animate}
  156. metaWheelZoom={metaWheelZoom}
  157. twoFingerDrag={twoFingerDrag}
  158. zoomSnap={zoomSnap}
  159. mouseEvents={mouseEvents}
  160. touchEvents={touchEvents}
  161. minZoom={minZoom}
  162. maxZoom={maxZoom}
  163. attribution={
  164. isMapBox(provider)
  165. ? <MapboxAttribution />
  166. : provider === 'stamen'
  167. ? <StamenAttribution />
  168. : provider === 'wikimedia'
  169. ? <WikimediaAttribution />
  170. : null}
  171. defaultWidth={400}
  172. height={200}
  173. boxClassname="pigeon-filters">
  174. {Object.keys(markers).map(key => (
  175. <Marker key={key} anchor={markers[key][0]} payload={key} onClick={this.handleMarkerClick} />
  176. ))}
  177. {isMapBox(provider) && <span className='mapbox-wordmark' />}
  178. </Map>
  179. </div>
  180. <div>
  181. {' '}
  182. {Math.round(center[0] * 10000) / 10000} ({lat2tile(center[0], zoom)})
  183. {' x '}
  184. {Math.round(center[1] * 10000) / 10000} ({lng2tile(center[1], zoom)})
  185. {' @ '}
  186. {Math.round(zoom * 100) / 100}
  187. {' - '}
  188. {animating ? 'animating' : 'stopped'}
  189. </div>
  190. <div>
  191. <button
  192. onClick={ () => {
  193. this.name = "Bernie";
  194. this.updateLocation();
  195. markers.Bernie=[[this.state.persons[3].latitude,this.state.persons[3].longitude], 13];
  196. }}> Bernie </button>
  197. <button
  198. onClick={ () => {
  199. this.name = "Martin";
  200. this.updateLocation();
  201. markers.Martin=[[this.state.persons[0].latitude,this.state.persons[0].longitude], 12];
  202. }}> Martin </button>
  203. <button
  204. onClick={ () => {
  205. this.name = "Simon";
  206. this.updateLocation();
  207. markers.Simon=[[this.state.persons[1].latitude,this.state.persons[1].longitude], 11];
  208. }}> Simon </button>
  209. <button
  210. onClick={ () => {
  211. this.name = "Lukas";
  212. this.updateLocation();
  213. markers.Lukas=[[this.state.persons[2].latitude,this.state.persons[2].longitude], 10];
  214. }}> Lukas </button>
  215. </div>
  216. <div id="wrapper">
  217. <Tables persons={this.state.persons}/>
  218. </div>
  219. </div>
  220. )
  221. }
  222. }