React Native
React Native
A React Native Geolocation App allows you to access and display the geographic location of a user's
device. React Native provides built-in support for geolocation through the react-native-geolocation-
service module or the Geolocation API, which is part of the Web APIs.
<key>NSLocationWhenInUseUsageDescription</key>
<string>App requires your location to function.</string>
For Android, add these permissions to AndroidManifest.xml:
Example Code:
javascript
Copy code
import React, { useState, useEffect } from 'react';
import {
SafeAreaView,
Text,
View,
Button,
PermissionsAndroid,
Platform,
} from 'react-native';
import Geolocation from 'react-native-geolocation-service';
import MapView, { Marker } from 'react-native-maps';
useEffect(() => {
(async () => {
const permissionGranted = await requestPermission();
if (permissionGranted) getLocation();
})();
}, []);
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 1 }}>
{location ? (
<MapView
style={{ flex: 1 }}
initialRegion={{
...location,
latitudeDelta: 0.01,
longitudeDelta: 0.01,
}}
>
<Marker coordinate={location} title="You are here" />
</MapView>
):(
<Text>Fetching location...</Text>
)}
</View>
<Button title="Refresh Location" onPress={getLocation} />
</SafeAreaView>
);
};
return (
<View style={styles.container}>
<Button title="Show Alert" onPress={showAlert} />
</View>
);
};
Alert Component:
Button Component:
Styling:
React Native is an open-source framework developed by Facebook that allows developers to build
cross-platform mobile applications using JavaScript and React. With React Native, you can write code
once and deploy it on both iOS and Android platforms, ensuring a native-like user experience. It
combines the best parts of native development with React, a popular JavaScript library for building
user interfaces, enabling developers to use a single codebase to build apps for multiple platforms.