Created
July 7, 2021 18:45
-
-
Save draftbitdocs/5b4a350130cf3c5689670c1a1a9aa9f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
import { Dimensions, StyleSheet, Text, View } from "react-native"; | |
import * as Location from "expo-location"; | |
import MapView from "react-native-maps"; | |
export const MyMapComponent = () => { | |
const [location, setLocation] = useState(null); | |
const [errorMsg, setErrorMsg] = useState(null); | |
// Request access for location permissions and store them | |
useEffect(() => { | |
(async () => { | |
let { status } = await Location.requestPermissionsAsync(); | |
if (status !== "granted") { | |
setErrorMsg("Permission to access location was denied"); | |
return; | |
} | |
let location = await Location.getCurrentPositionAsync({}); | |
setLocation(location); | |
})(); | |
}, []); | |
return ( | |
<View style={styles.container}> | |
{location ? ( | |
<MapView | |
// Initialize the map around the user's locations | |
region={{ | |
latitude: location.coords.latitude, | |
longitude: location.coords.longitude, | |
latitudeDelta: 0.5, | |
longitudeDelta: 0.5, | |
}} | |
style={styles.map} | |
> | |
<MapView.Marker | |
// Place a map marker at the user's location | |
coordinate={{ | |
latitude: location.coords.latitude, | |
longitude: location.coords.longitude, | |
}} | |
/> | |
</MapView> | |
) : ( | |
<Text style={styles.text}>{errorMsg || "Gathering location..."}</Text> | |
)} | |
</View> | |
); | |
}; | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
alignItems: "center", | |
}, | |
text: { marginTop: 12 }, | |
map: { | |
width: Dimensions.get("window").width, | |
height: Dimensions.get("window").height, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment