Skip to content

Commit

Permalink
feat: add mapview & custom marker with draggable current location
Browse files Browse the repository at this point in the history
  • Loading branch information
geeekgod committed Apr 1, 2023
1 parent 712feca commit 11dd2d4
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 6 deletions.
Binary file added app/assets/requests_marker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/user_marker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion app/context/DataContext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ const DataContextProvider = ({ children }) => {
getNearByRequests,
nearByRequests,
getConfig,
config
config,
setLocation
}}
>
{children}
Expand Down
13 changes: 12 additions & 1 deletion app/navigation/DrawerNavigator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Ionicons } from "@expo/vector-icons";
import CustomDrawerHeader from '../../components/CustomDrawerHeader';
import SavedRequests from '../../screens/SavedRequests';
import NearByRequests from '../../screens/NearByRequests';
import MapRequestsView from '../../screens/MapRequestsView';

const Drawer = createDrawerNavigator();

Expand Down Expand Up @@ -43,6 +44,16 @@ const DrawerNavigator = () => {
}}
component={Home} />

<Drawer.Screen name="MapRequestsView"
options={{
header: (props) => <CustomDrawerHeader {...props} title={'Map View'} />,
drawerLabel: "Map View",
drawerIcon: ({ color }) => (
<Ionicons name="map" size={25} color={color} />
),
}}
component={MapRequestsView} />

<Drawer.Screen name="SavedRequest"
options={{
header: (props) => <CustomDrawerHeader {...props} title={'Saved Requests'} />,
Expand Down Expand Up @@ -76,4 +87,4 @@ const DrawerNavigator = () => {
);
}

export default DrawerNavigator
export default DrawerNavigator
114 changes: 114 additions & 0 deletions app/screens/MapRequestsView/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import React, { useContext, useEffect, useState } from "react";
import { StyleSheet, TouchableOpacity, View } from "react-native";
import { heightScreen, widthScreen } from "../../utils/layout";
import {
Box,
Button,
CloseIcon,
HStack,
Divider,
Icon,
IconButton,
Text,
VStack,
FlatList,
} from "native-base";
import { DataContext } from "../../context/DataContext";
import * as Linking from "expo-linking";
import { AuthContext } from "../../context/AuthContext";
import MapView, { Marker } from "react-native-maps";

const MapRequestsView = () => {
const {
requests,
getRequest,
getSavedRequest,
savedRequests,
nearByRequests,
getLocation,
getNearByRequests,
setLocation,
location
} = useContext(DataContext);
const { accessToken, getUser } = useContext(AuthContext);

const [refreshing, setRefreshing] = useState(false);
const [locationState, setLocationState] = useState({
latitude: 19.2183,
longitude: 72.9781,
latitudeDelta: 0.00922,
longitudeDelta: 0.00421,
})

const loadRequest = async () => {
return new Promise(async () => {
await getLocation()
await getSavedRequest(accessToken);
await getRequest(accessToken);
await getNearByRequests(accessToken);
setRefreshing(false);
});
};

const onRefresh = async () => {
setRefreshing(true);
await loadRequest();
};

const updateCurrentLocation = (e) => {
setLocationState({ ...locationState, latitude: e.nativeEvent.coordinate.latitude, longitude: e.nativeEvent.coordinate.longitude })
}

useEffect(() => {
setLocation({ coords: { latitude: locationState.latitude, longitude: locationState.longitude } })
setTimeout(async () => {
await getNearByRequests(accessToken);
}, 1000)
}, [locationState]);

return (
<View style={styles.container}>
<MapView
initialRegion={locationState}
style={styles.map} >

{locationState.latitude && locationState.longitude && (
<Marker coordinate={locationState}
title={"Your Location"} image={require('../../assets/user_marker.png')}
draggable
onDragEnd={(e) => updateCurrentLocation(e)}
/>)}
{nearByRequests && nearByRequests.map((request, index) => {
return (
<Marker
key={index}
coordinate={{
latitude: request.location[1],
longitude: request.location[0],
}}
title={`Requests from ${request.name} for ${request.bloodGroup}`}
image={require('../../assets/requests_marker.png')}
// description={request.description}
/>
);
})}
</MapView>
</View>
);
};

export default MapRequestsView;

const styles = StyleSheet.create({
container: {
flex: 1,
width: widthScreen,
backgroundColor: "#fff",
alignItems: "center",
position: "relative",
},
map: {
width: '100%',
height: '100%',
},
});
4 changes: 2 additions & 2 deletions metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = (async () => {
},
resolver: {
assetExts: assetExts.filter(ext => ext !== 'svg'),
sourceExts: [...sourceExts, 'svg'],
sourceExts: [...sourceExts, 'svg', 'jsx', 'js'],
},
};
})();
})();
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"web": "expo start --web"
},
"dependencies": {
"@freakycoder/react-native-bounceable": ">= 0.2.2",
"@react-native-async-storage/async-storage": "^1.17.10",
"@react-navigation/drawer": "^6.5.0",
"@react-navigation/native": "^6.0.13",
Expand All @@ -26,11 +27,11 @@
"native-base": "^3.4.16",
"react": "18.0.0",
"react-native": "0.69.6",
"react-native-animated-spinkit": "^1.4.1",
"react-native-dynamic-search-bar": "WrathChaos/react-native-dynamic-search-bar#expo",
"react-native-dynamic-vector-icons": "WrathChaos/react-native-dynamic-vector-icons#expo",
"react-native-animated-spinkit": "^1.4.1",
"@freakycoder/react-native-bounceable": ">= 0.2.2",
"react-native-gesture-handler": "~2.5.0",
"react-native-maps": "^1.4.0",
"react-native-reanimated": "~2.9.1",
"react-native-safe-area-context": "4.3.1",
"react-native-screens": "~3.15.0",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,11 @@
resolved "https://registry.yarnpkg.com/@trysound/sax/-/sax-0.2.0.tgz#cccaab758af56761eb7bf37af6f03f326dd798ad"
integrity sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA==

"@types/geojson@^7946.0.8":
version "7946.0.10"
resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-7946.0.10.tgz#6dfbf5ea17142f7f9a043809f1cd4c448cb68249"
integrity sha512-Nmh0K3iWQJzniTuPRcJn5hxXkfB1T1pgB89SBig5PlJQU5yocazeu4jATJlaA0GYFKWMqDdvYemoSnF2pXgLVA==

"@types/graceful-fs@^4.1.2":
version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
Expand Down Expand Up @@ -6897,6 +6902,13 @@ react-native-keyboard-aware-scroll-view@^0.9.5:
prop-types "^15.6.2"
react-native-iphone-x-helper "^1.0.3"

react-native-maps@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/react-native-maps/-/react-native-maps-1.4.0.tgz#e24133311c032447c35e55152841c4e0e9ddf2a5"
integrity sha512-asP6oVx9uF4E9U22j40q0AcSJ4v3hPBPCg1kPdlbckGXGj+z8dwwdPkQi3hUpl94odlR//v60Sw6PAEYv8zSxw==
dependencies:
"@types/geojson" "^7946.0.8"

react-native-reanimated@~2.9.1:
version "2.9.1"
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-2.9.1.tgz#d9a932e312c13c05b4f919e43ebbf76d996e0bc1"
Expand Down

0 comments on commit 11dd2d4

Please sign in to comment.