Map.js 5.0 KB

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