-
Notifications
You must be signed in to change notification settings - Fork 3
/
MapScreen.js
244 lines (223 loc) · 6.74 KB
/
MapScreen.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
Platform,
} from "react-native";
import loadGoogleMapsAPI from "./webMapComponent"; // Import the function
import fetchRouteData from "../api/GetCoords";
let MapViewMob, MarkerMob, MapViewDirectionsMob;
if (Platform.OS === "android" || Platform.OS === "ios") {
MapViewMob = require("react-native-maps").default;
MarkerMob = require("react-native-maps").Marker;
MapViewDirectionsMob = require("react-native-maps-directions").default;
}
let MapView;
if (Platform.OS === "web") {
MapView = require("@preflower/react-native-web-maps").default;
}
// Create the debounce function responsible for zoom actions
const debounce = (func, delay) => {
let timer;
return function (...args) {
try {
clearTimeout(timer);
timer = setTimeout(() => {
if (typeof func == "function") {
func(...args);
}
}, delay);
} catch (error) {
console.log("Error in debounce", error);
}
};
};
export default class MapScreen extends Component {
constructor(props) {
super(props);
this.debouncedOnRegionChange = debounce(this.onRegionChange, 10);
this.state = {
coords: [],
googleMapsLoaded: false,
origin: { latitude: 33.843663, longitude: -117.945171 },
destination: { latitude: 33.8252956, longitude: -117.8307728 },
region: {
latitude: 33.8252956,
longitude: -117.8307728,
latitudeDelta: 0.05, // Adjust this value for zoom level
longitudeDelta: 0.045, // Adjust this value for zoom level
},
waypoint: { latitude: 33.8589565, longitude: -117.9589782 },
};
}
async componentDidMount() {
if (Platform.OS === "web") {
loadGoogleMapsAPI(() => {
this.setState({ googleMapsLoaded: true });
});
}
try {
const newcoords = await fetchRouteData(
this.state.origin,
this.state.waypoint,
this.state.destination
);
this.setState({ coords: newcoords });
} catch (error) {
console.error("Error fetching COORDS", error);
}
}
// Create a debounced version of onRegionChange
debouncedOnRegionChange = debounce((newRegion) => {
// Check if the new region has valid latitude and longitude
if (
!isNaN(newRegion.latitude) &&
!isNaN(newRegion.longitude) &&
isFinite(newRegion.latitude) &&
isFinite(newRegion.longitude)
) {
// Update the state only if the new region has valid coordinates
this.setState({ region: newRegion });
}
}, 10);
onRegionChangeComplete = (region) => {
console.log("Region changed:", region);
};
onPress = (event) => {
console.log("Map pressed:", event.nativeEvent.coordinate);
};
onDoublePress = (event) => {
console.log("Map double pressed:", event.nativeEvent.coordinate);
};
onPanDrag = () => {
console.log("Map panned or dragged");
};
render() {
const { origin, destination, googleMapsLoaded, coords } = this.state;
return (
<View style={styles.container}>
{googleMapsLoaded && Platform.OS === "web" ? (
<View style={styles.container}>
<MapView
style={styles.map}
initialRegion={this.state.region}
onRegionChange={(new_region) => {
this.debouncedOnRegionChange(new_region);
}}
onRegionChangeComplete={this.onRegionChangeComplete}
onPress={this.onPress}
onDoublePress={this.onDoublePress}
onPanDrag={this.onPanDrag}
zoomEnabled={true}
zoomControlEnabled={true}
mapType="terrain"
showsPointsOfInterest={false}
>
<MapView.Marker coordinate={origin} title="Origin">
<View style={styles.markerContainer}></View>
</MapView.Marker>
<MapView.Marker coordinate={destination} title="Destination">
<View style={styles.markerContainer}></View>
</MapView.Marker>
{coords && (
<MapView.Polyline
coordinates={coords.map((coord) => ({
latitude: coord[0],
longitude: coord[1],
}))}
strokeWidth={8}
strokeColor="royalblue"
tappable={true}
onClick={() => {
this.onPolylineClicked();
}}
/>
)}
</MapView>
</View>
) : Platform.OS === "android" || Platform.OS === "ios" ? (
<View style={styles.container}>
<MapViewMob
style={styles.map}
onRegionChange={(new_region) => {
this.debouncedOnRegionChange(new_region);
}}
>
<MarkerMob coordinate={origin} title="Origin">
<View style={styles.markerContainer}></View>
</MarkerMob>
<MarkerMob coordinate={destination} title="Destination">
<View style={styles.markerContainer}></View>
</MarkerMob>
{markers &&
markers.map((marker, index) => (
<MarkerMob
key={index}
coordinate={marker.latlng}
title={marker.title}
description={marker.description}
/>
))}
{plot.draw && (
<MapViewDirectionsMob
origin={origin}
destination={destination}
waypoint={plot.waypoint}
strokeColor={showIcon ? "red" : "royalblue"}
tappable={true}
onPress={() => {
this.onPolylineClicked();
}}
apikey={apiKey}
strokeWidth={14}
/>
)}
</MapViewMob>
</View>
) : (
<Text>LOADING....</Text>
)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
},
map: {
...StyleSheet.absoluteFillObject,
borderColor: "white",
borderWidth: 8,
borderTopWidth: 4,
borderBottomWidth: 4,
},
markerContainer: {
width: 40,
height: 40,
},
markerImage: {
flex: 1,
width: undefined,
height: undefined,
},
rgnText: {
fontSize: 12,
color: "#666666",
},
rgnView: { flexDirection: "row", alignItems: "flex-end" },
button: {
backgroundColor: "orange",
padding: 8,
margin: 5,
borderRadius: 5,
alignItems: "center",
height: 35,
},
buttonText: {
color: "white",
textAlign: "center",
fontWeight: "bold",
},
});