|
1 | | -import React, { useRef, useEffect } from 'react' |
2 | | -import { Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native' |
3 | | -import MapView, { Marker } from 'react-native-maps' |
4 | | -import { useDispatch, useSelector } from 'react-redux' |
5 | | -import tailwind from 'tailwind-react-native-classnames' |
6 | | -import { selectDestination, selectOrigin, setTravelTimeInformation } from '../redux/slices/navSlice' |
7 | | -import MapViewDirections from 'react-native-maps-directions' |
8 | | -import { GOOGLE_MAP_APIKEY } from '@env' |
9 | | -import { Icon } from 'react-native-elements' |
10 | | -import Constants from 'expo-constants' |
11 | | -import { useNavigation } from '@react-navigation/native' |
12 | | - |
13 | | -const Map = () => { |
14 | | - const origin = useSelector(selectOrigin) |
15 | | - const destination = useSelector(selectDestination) |
16 | | - const mapRef = useRef(null) |
17 | | - const navigation = useNavigation() |
18 | | - const dispatch = useDispatch() |
19 | | - |
20 | | - useEffect(() => { |
21 | | - if (!origin || !destination) return; |
22 | | - mapRef.current.fitToSuppliedMarkers(['origin', 'destination'], { |
23 | | - edgePadding: { top: 50, right: 50, bottom: 50, left: 50 } |
24 | | - }) |
25 | | - }, [origin, destination]) |
26 | | - |
27 | | - useEffect(() => { |
28 | | - if(!origin || !destination) return; |
29 | | - getTravelTime() |
30 | | - }, [origin, destination, GOOGLE_MAP_APIKEY]) |
31 | | - |
32 | | - const getTravelTime = async () => { |
33 | | - const URL = `https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=${origin.description}&destinations=${destination.description}&key=${GOOGLE_MAP_APIKEY}` |
34 | | - const data = await fetch(URL).then(response => response.json()) |
35 | | - if(data.status !== 'OK') return alert(data.error_message) |
36 | | - dispatch(setTravelTimeInformation(data.rows[0].elements[0])) |
37 | | - } |
38 | | - |
39 | | - return ( |
40 | | - <View style={tailwind`flex-1 relative`}> |
41 | | - <TouchableOpacity |
42 | | - style={[ tailwind`bg-white p-3 rounded-full shadow-lg`,{ top: Constants.statusBarHeight, left: 20, position: 'absolute', zIndex: 100 }]} |
43 | | - onPress={() => navigation.push("Home")} |
44 | | - > |
45 | | - <Icon |
46 | | - type="antdesign" |
47 | | - name="home" |
48 | | - color="black" |
49 | | - size={16} |
50 | | - // style={} |
51 | | - /> |
52 | | - </TouchableOpacity> |
53 | | - |
54 | | - <MapView |
55 | | - ref={mapRef} |
56 | | - style={tailwind`flex-1 relative z-10`} |
57 | | - initialRegion={{ |
58 | | - latitude: origin?.loaction.lat, |
59 | | - longitude: origin?.loaction.lng, |
60 | | - latitudeDelta: 0.005, |
61 | | - longitudeDelta: 0.005, |
62 | | - }} |
63 | | - mapType="mutedStandard" |
64 | | - > |
65 | | - {!!origin && !!destination && ( |
66 | | - <MapViewDirections |
67 | | - // origin={{ |
68 | | - // latitude: origin?.loaction.lat, |
69 | | - // longitude: origin?.loaction.lng, |
70 | | - // }} |
71 | | - // destination={{ |
72 | | - // latitude: destination?.loaction.lat, |
73 | | - // longitude: destination?.loaction.lng, |
74 | | - // }} |
75 | | - origin={origin.description} |
76 | | - destination={destination.description} |
77 | | - lineDashPattern={[0]} |
78 | | - apikey={GOOGLE_MAP_APIKEY} |
79 | | - strokeWidth={3} |
80 | | - strokeColor="black" |
81 | | - onError={error => console.log("Directions error: ", error)} |
82 | | - /> |
83 | | - )} |
84 | | - {origin?.loaction && ( |
85 | | - <Marker |
86 | | - coordinate={{ |
87 | | - latitude: origin?.loaction.lat, |
88 | | - longitude: origin?.loaction.lng, |
89 | | - }} |
90 | | - title="Origin" |
91 | | - description={origin.description} |
92 | | - identifier="origin" |
93 | | - > |
94 | | - <Image source={require('../assets/custom_pin.png')} style={{ height: 45, width: 45 }} /> |
95 | | - </Marker> |
96 | | - )} |
97 | | - {destination?.loaction && ( |
98 | | - <Marker |
99 | | - coordinate={{ |
100 | | - latitude: destination?.loaction.lat, |
101 | | - longitude: destination?.loaction.lng, |
102 | | - }} |
103 | | - title="Destination" |
104 | | - description={destination.description} |
105 | | - identifier="destination" |
106 | | - > |
107 | | - <Image source={require('../assets/custom_pin.png')} style={{ height: 45, width: 45 }} /> |
108 | | - </Marker> |
109 | | - )} |
110 | | - </MapView> |
111 | | - </View> |
112 | | - ) |
113 | | -} |
114 | | - |
115 | | -export default Map |
116 | | - |
117 | | -const styles = StyleSheet.create({}) |
| 1 | +import React, { useRef, useEffect } from 'react' |
| 2 | +import { Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native' |
| 3 | +import MapView, { Marker } from 'react-native-maps' |
| 4 | +import { useDispatch, useSelector } from 'react-redux' |
| 5 | +import tailwind from 'tailwind-react-native-classnames' |
| 6 | +import { selectDestination, selectOrigin, setTravelTimeInformation } from '../redux/slices/navSlice' |
| 7 | +import MapViewDirections from 'react-native-maps-directions' |
| 8 | +import { GOOGLE_MAP_APIKEY } from '@env' |
| 9 | +import { Icon } from 'react-native-elements' |
| 10 | +import Constants from 'expo-constants' |
| 11 | +import { useNavigation } from '@react-navigation/native' |
| 12 | + |
| 13 | +const Map = () => { |
| 14 | + const origin = useSelector(selectOrigin) |
| 15 | + const destination = useSelector(selectDestination) |
| 16 | + const mapRef = useRef(null) |
| 17 | + const navigation = useNavigation() |
| 18 | + const dispatch = useDispatch() |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + if (!origin || !destination) return; |
| 22 | + mapRef.current.fitToSuppliedMarkers(['origin', 'destination'], { |
| 23 | + edgePadding: { top: 50, right: 50, bottom: 50, left: 50 } |
| 24 | + }) |
| 25 | + }, [origin, destination]) |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + if(!origin || !destination) return; |
| 29 | + getTravelTime() |
| 30 | + }, [origin, destination, GOOGLE_MAP_APIKEY]) |
| 31 | + |
| 32 | + const getTravelTime = async () => { |
| 33 | + const URL = `https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=${origin.description}&destinations=${destination.description}&key=${GOOGLE_MAP_APIKEY}` |
| 34 | + const data = await fetch(URL).then(response => response.json()) |
| 35 | + if(data.status !== 'OK') return alert(data.error_message) |
| 36 | + dispatch(setTravelTimeInformation(data.rows[0].elements[0])) |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <View style={tailwind`flex-1 relative`}> |
| 41 | + <TouchableOpacity |
| 42 | + style={[ tailwind`bg-white p-3 rounded-full shadow-lg`,{ top: Constants.statusBarHeight, left: 20, position: 'absolute', zIndex: 100 }]} |
| 43 | + onPress={() => navigation.push("Home")} |
| 44 | + > |
| 45 | + <Icon |
| 46 | + type="antdesign" |
| 47 | + name="home" |
| 48 | + color="black" |
| 49 | + size={16} |
| 50 | + // style={} |
| 51 | + /> |
| 52 | + </TouchableOpacity> |
| 53 | + |
| 54 | + <MapView |
| 55 | + ref={mapRef} |
| 56 | + style={tailwind`flex-1 relative z-10`} |
| 57 | + initialRegion={{ |
| 58 | + latitude: origin?.loaction.lat, |
| 59 | + longitude: origin?.loaction.lng, |
| 60 | + latitudeDelta: 0.005, |
| 61 | + longitudeDelta: 0.005, |
| 62 | + }} |
| 63 | + mapType="mutedStandard" |
| 64 | + > |
| 65 | + {!!origin && !!destination && ( |
| 66 | + <MapViewDirections |
| 67 | + // origin={{ |
| 68 | + // latitude: origin?.loaction.lat, |
| 69 | + // longitude: origin?.loaction.lng, |
| 70 | + // }} |
| 71 | + // destination={{ |
| 72 | + // latitude: destination?.loaction.lat, |
| 73 | + // longitude: destination?.loaction.lng, |
| 74 | + // }} |
| 75 | + origin={origin.description} |
| 76 | + destination={destination.description} |
| 77 | + lineDashPattern={[0]} |
| 78 | + apikey={GOOGLE_MAP_APIKEY} |
| 79 | + strokeWidth={3} |
| 80 | + strokeColor="black" |
| 81 | + onError={error => console.log("Directions error: ", error)} |
| 82 | + /> |
| 83 | + )} |
| 84 | + {origin?.loaction && ( |
| 85 | + <Marker |
| 86 | + coordinate={{ |
| 87 | + latitude: origin?.loaction.lat, |
| 88 | + longitude: origin?.loaction.lng, |
| 89 | + }} |
| 90 | + title="Origin" |
| 91 | + description={origin.description} |
| 92 | + identifier="origin" |
| 93 | + > |
| 94 | + <Image source={require('../assets/custom_pin.png')} style={{ height: 45, width: 45 }} /> |
| 95 | + </Marker> |
| 96 | + )} |
| 97 | + {destination?.loaction && ( |
| 98 | + <Marker |
| 99 | + coordinate={{ |
| 100 | + latitude: destination?.loaction.lat, |
| 101 | + longitude: destination?.loaction.lng, |
| 102 | + }} |
| 103 | + title="Destination" |
| 104 | + description={destination.description} |
| 105 | + identifier="destination" |
| 106 | + > |
| 107 | + <Image source={require('../assets/custom_pin.png')} style={{ height: 45, width: 45 }} /> |
| 108 | + </Marker> |
| 109 | + )} |
| 110 | + </MapView> |
| 111 | + </View> |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +export default Map |
| 116 | + |
| 117 | +const styles = StyleSheet.create({}) |
0 commit comments