-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.tsx
152 lines (136 loc) · 4.04 KB
/
testing.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { useEffect, useState } from 'react';
import { View, Text, TouchableOpacity, TextInput, Modal, Button, StyleSheet, Platform } from 'react-native';
import MapView, { Marker } from 'react-native-maps';
import Geolocation from '@react-native-community/geolocation';
interface Location {
latitude: number;
longitude: number;
}
interface CustomMarker {
id: number;
location: Location;
storeNumber: string;
floorLevel: string;
}
const App: React.FC = () => {
const [currentLocation, setCurrentLocation] = useState<Location | null>(null);
const [markers, setMarkers] = useState<CustomMarker[]>([]);
const [storeNumber, setStoreNumber] = useState<string>('');
const [floorLevel, setFloorLevel] = useState<string>('');
const [isModalVisible, setModalVisible] = useState<boolean>(false);
useEffect(() => {
// Request location permission
Geolocation.requestAuthorization();
// Watch for location changes
const watchId = Geolocation.watchPosition(
position => {
const { latitude, longitude } = position.coords;
console.log(latitude, longitude);
setCurrentLocation({ latitude, longitude });
},
error => console.log('Error getting location:', error),
Platform.OS === 'android'
? {}
: { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
);
return () => {
// Clear the location watch when the component unmounts
Geolocation.clearWatch(watchId);
};
}, [currentLocation]);
const showFormModal = () => setModalVisible(true);
const hideFormModal = () => setModalVisible(false);
const handleSave = () => {
// Generate a unique id for the new marker
const newMarkerId = Date.now();
// Add the new marker to the array with the current location
const newMarker: CustomMarker = {
id: newMarkerId,
location: currentLocation!,
storeNumber,
floorLevel,
};
setMarkers(prevMarkers => [...prevMarkers, newMarker]);
// Clear the form input
setStoreNumber('');
setFloorLevel('');
hideFormModal();
};
return (
<View style={{ flex: 1 }}>
{currentLocation && (
<MapView
style={{ flex: 1 }}
initialRegion={{
latitude: currentLocation.latitude,
longitude: currentLocation.longitude,
latitudeDelta: 0.05,
longitudeDelta: 0.05,
}}>
{markers.map(marker => (
<Marker
key={marker.id}
coordinate={marker.location}
title={marker.storeNumber}
description={`Floor Level: ${marker.floorLevel}`}
/>
))}
</MapView>
)}
<Modal visible={isModalVisible} transparent animationType="slide">
<View style={styles.modalContainer}>
<TextInput
style={styles.input}
placeholder="Store Number"
value={storeNumber}
onChangeText={text => setStoreNumber(text)}
/>
<TextInput
style={styles.input}
placeholder="Floor Level"
value={floorLevel}
onChangeText={text => setFloorLevel(text)}
/>
<Button title="Save" onPress={handleSave} />
<Button title="Cancel" onPress={hideFormModal} />
</View>
</Modal>
<TouchableOpacity style={styles.fab} onPress={showFormModal}>
<Text style={styles.fabText}>+</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
fab: {
position: 'absolute',
backgroundColor: '#007bff',
borderRadius: 50,
width: 50,
height: 50,
alignItems: 'center',
justifyContent: 'center',
right: 16,
bottom: 16,
},
fabText: {
color: 'white',
fontSize: 20,
fontWeight: 'bold',
},
modalContainer: {
backgroundColor: 'white',
padding: 16,
margin: 16,
borderRadius: 8,
elevation: 5,
},
input: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginBottom: 10,
paddingHorizontal: 10,
},
});
export default App;