Map.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React, { useRef, useState } from 'react';
  2. import {Alert, StyleSheet, View} from 'react-native';
  3. import MapboxGL from '@react-native-mapbox-gl/maps';
  4. import { v4 as uuidv4 } from 'uuid';
  5. import 'react-native-get-random-values'
  6. import { useFocusEffect } from '@react-navigation/native';
  7. import Globals from '../globals';
  8. MapboxGL.setAccessToken('pk.eyJ1IjoiY2Rtb3NzIiwiYSI6ImNrbmhuOXJzcDIyd20ycW1pYm8xaGI0aGUifQ.j04Sp636N9Wg4N9j9t2tXw');
  9. const Map = ({navigation, route}) => {
  10. const getApiPlaces = async () => {
  11. const response = await fetch(GLOBALS.API_HOST);
  12. const placeData = await response.json();
  13. setApiPlaces(placeData);
  14. }
  15. useFocusEffect(
  16. React.useCallback(() => {
  17. async function getData() {
  18. await getApiPlaces();
  19. }
  20. getData();
  21. }, [apiPlaces])
  22. );
  23. const [apiPlaces, setApiPlaces] = useState([]);
  24. const [tempPlaces, setTempPlaces] = useState([]);
  25. const [center, setCenter] = useState([-110.67815,50.04107]);
  26. const mapRef = useRef();
  27. const addPoint = (coords) => {
  28. setTempPlaces(prevPlaces => {
  29. return [{
  30. id: uuidv4(),
  31. coords: coords,
  32. desc: "temp point"}, ...prevPlaces]
  33. })
  34. setCenter(coords);
  35. }
  36. const deletePoint = async (id) => {
  37. console.log(id);
  38. for (let index = 0; index < tempPlaces.length; index++) {
  39. if (tempPlaces[index].id == id) {
  40. const placesCopy = tempPlaces.filter(place => place !== tempPlaces[index]);
  41. setTempPlaces(placesCopy);
  42. return;
  43. }
  44. }
  45. await fetch(`${GLOBALS.API_HOST}${id}/`, {
  46. method: 'DELETE'
  47. }).then(response => {
  48. if (response.ok) {
  49. Alert.alert("Place deleted from database.")
  50. }
  51. else {
  52. Alert.alert("Something went wrong when deleting new place", `Response code: ${response.status}`)
  53. }
  54. })
  55. .catch(error => Alert.alert("Something went wrong when talking to the server",error));
  56. getApiPlaces();
  57. }
  58. const promptAddPlace = (coords) => {
  59. Alert.alert(
  60. "New Place",
  61. `What would you like to do with the coordinate at \n\n Longitude: ${coords[0]} \n Latitutde: ${coords[1]}?`,
  62. [
  63. {text: 'Do nothing'},
  64. {text: 'Add temporary point', onPress: () => addPoint(coords)},
  65. {text: 'Add new place', onPress: () => goToNewPlace(coords)},
  66. ]
  67. )
  68. }
  69. const focusPoint = (id) => {
  70. let place;
  71. for (let index = 0; index < tempPlaces.length; index++) {
  72. if (tempPlaces[index].id == id) {
  73. place = tempPlaces[index];
  74. console.log(place);
  75. Alert.alert(
  76. `Temporary Place`,
  77. `Coordinates:\n lat - (${place.coords[1]})\n lat - (${place.coords[1]})`,
  78. [
  79. {text: 'Delete place', onPress:() => deletePoint(id), style: "cancel"},
  80. {text: 'Do nothing'}
  81. ]);
  82. return;
  83. }
  84. }
  85. for (let index = 0; index < apiPlaces.length; index++) {
  86. if (apiPlaces[index].id == id) {
  87. place = apiPlaces[index];
  88. Alert.alert(
  89. `Selected Place`,
  90. `Created by: ${place.name}\nDescription: ${place.desc}\nCoordinates:\n lat - (${place.lat})\n lat - (${place.lng})`,
  91. [
  92. {text: 'Delete place', onPress:() => deletePoint(id), style: "cancel"},
  93. {text: 'Do nothing'}
  94. ]);
  95. return;
  96. }
  97. }
  98. }
  99. const goToNewPlace = (coords) => {
  100. navigation.navigate('New Place', {coords: coords});
  101. }
  102. return(
  103. <View style={styles.container}>
  104. <View style={styles.subContainer}>
  105. <MapboxGL.MapView
  106. ref={mapRef}
  107. style={styles.mapbox}
  108. onPress={p => addPoint(p.geometry.coordinates)}
  109. onLongPress={p => promptAddPlace(p.geometry.coordinates)}>
  110. <MapboxGL.Camera
  111. zoomLevel={9}
  112. centerCoordinate={center}
  113. />
  114. {apiPlaces.length > 0 && apiPlaces.map(place => {
  115. const coords = [parseFloat(place.lng), parseFloat(place.lat)];
  116. return (
  117. <MapboxGL.PointAnnotation
  118. onSelected={e => focusPoint(e.properties.id)}
  119. key={place.id.toString()} id={place.id.toString()} coordinate={coords}/>)
  120. })}
  121. {tempPlaces.length > 0 && tempPlaces.map(place => {
  122. return (
  123. <MapboxGL.PointAnnotation
  124. onSelected={e => focusPoint(e.properties.id)}
  125. key={place.id} id={place.id}
  126. coordinate={place.coords}>
  127. <View style={{height: 10, width: 10, borderRadius: 50, backgroundColor: '#FFF' }}>
  128. </View>
  129. </MapboxGL.PointAnnotation>)
  130. })}
  131. </MapboxGL.MapView>
  132. </View>
  133. </View>
  134. )
  135. }
  136. const styles = StyleSheet.create({
  137. container: {
  138. flex: 1
  139. },
  140. subContainer: {
  141. height: '100%',
  142. width: '100%',
  143. backgroundColor: 'white',
  144. },
  145. mapbox: {
  146. flex: 1,
  147. },
  148. })
  149. export default Map;