Map View With Pin Point Accuracy in React Native & Expo Apps

Google Maps For Android & iOS Apps

Google Map in react native & expo apps

Project setup, Package Requirements, and Code

  • Start your expo project
  • Install the required npm package for this project:
npx expo install expo-location
npx expo install react-native-maps
  • First, I am setting the default map region using the useState hook. Later we are going to change it with the user’s current location. The default map region is:
const [mapRegion, setMapRegion] = useState({
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
});
  • Next, add the ‘userLocation’ function, Inside this function, first, we will fetch the user’s current location, then we will replace it with the map region state. Something like this:
const userLocation = async () => {
let {status} = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
}
let location = await Location.getCurrentPositionAsync({enableHighAccuracy: true});
setMapRegion({
latitude: location.coords.latitude,
longitude: location.coords.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
});
console.log(location.coords.latitude, location.coords.longitude);
}
  • Console log the coordinate to check that the function is working fine.
  • At last, pass this userLocation to a useEffect hook. By doing this, the application will remember the state change.
useEffect(() => {
userLocation();
}, []);
  • That’s it. Now, add the UI according to your project requirement. If you need help then you can check this.
  • If you are confused or lost, I would recommend watching a step-by-step video tutorial. Few minutes of setup and code, you will be ready to test the application.

Why do I suggest watching more videos?

I am from India and I love writing code and sharing it with the community. We all know that money is the ultimate motivation for work, and the Medium payment system uses Stripe, which has been in preview mode in India for the past 4–5 years. I waited a year on the Medium platform, but in the end, nothing worked out.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store