React Native Geolocation Tutorial With IP Location API


Building mobile apps with geolocation features is now a necessity for businesses. Whether it’s localizing content, personalizing services, or improving security, geolocation plays a key role in modern mobile development.
In this React Native geolocation tutorial, we’ll guide developers, API communities, and small enterprises through building a basic app that detects the user’s location via their IP address. We’ll also show you how to integrate an IP to location API to power this feature.
Why Use IP-Based Geolocation in Apps?
Geolocation can be gathered using GPS or IP-based data. While GPS provides higher precision, using an IP to location API is ideal for:
Detecting user location without permissions
Offering default location settings
Pre-filling location data in forms
Lightweight background usage
For apps where privacy or simplicity is key, IP-based geolocation is a fast and easy option.
Tools You Need
Before getting started, make sure you have the following:
React Native (setup via Expo or React Native CLI)
APILayer’s IP to Location API (free key available
axios for making HTTP requests
A basic understanding of JavaScript and React Native
Step 1: Set Up the React Native App
If you're using Expo (recommended for beginners), start with:
bash
CopyEdit
npm install -g expo-cli
expo init GeolocationApp
cd GeolocationApp
expo start
Choose a blank template to begin.
Step 2: Register for IP to Location API
Head to https://apilayer.com, sign up, and get your free API key for the IP to location API.
This API returns user data such as:
IP address
City, Region, Country
Latitude, Longitude
ISP and timezone (optional)
Step 3: Install Axios and Fetch IP Location
Install axios to make your API call:
bash
CopyEdit
npm install axios
Now update your App.js to call the API:
javascript
CopyEdit
import React, { useEffect, useState } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import axios from 'axios';
export default function App() {
const [location, setLocation] = useState(null);
useEffect(() => {
axios.get('https://api.apilayer.com/ip_to_location', {
headers: {
apikey: 'YOUR_API_KEY'
}
})
.then(res => {
setLocation(res.data);
})
.catch(err => console.log(err));
}, []);
return (
<View style={styles.container}>
{location ? (
<>
<Text>IP: {location.ip}</Text>
<Text>City: {location.city}</Text>
<Text>Region: {location.region_name}</Text>
<Text>Country: {location.country_name}</Text>
<Text>Latitude: {location.latitude}</Text>
<Text>Longitude: {location.longitude}</Text>
</>
) : (
<Text>Loading location...</Text>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1, justifyContent: 'center', alignItems: 'center'
}
});
Replace 'YOUR_API_KEY' with your actual key.
Step 4: (Optional) Show Location on a Map
Add mapping capability for a visual experience:
bash
CopyEdit
expo install react-native-maps
Add the MapView component using the latitude and longitude from the IP API.
javascript
CopyEdit
import MapView, { Marker } from 'react-native-maps';
<MapView
style={{ width: 300, height: 300 }}
initialRegion={{
latitude: parseFloat(location.latitude),
longitude: parseFloat(location.longitude),
latitudeDelta: 0.05,
longitudeDelta: 0.05,
}}
\>
<Marker coordinate={{
latitude: parseFloat(location.latitude),
longitude: parseFloat(location.longitude),
}} />
</MapView>
Benefits for Developers and Businesses
Using an IP to location API inside your React Native app brings several practical advantages:
✅ For Developers:
Fast setup without native code
No permissions required
Lightweight and works instantly
✅ For Small Enterprises:
Personalize user experience based on region
Geo-based analytics for insights
Location-aware content delivery
Real-World Use Cases
This simple implementation can scale into:
Currency converters (auto-set local currency)
Weather forecast apps
Local news or event aggregators
E-commerce apps that auto-set shipping country
Best Practices
Don’t hardcode the API key in production—store it in .env files
Handle API errors properly to avoid crashes
Validate IP data before displaying
Use fallback content if location fails to load
This React Native geolocation tutorial is perfect for developers and small businesses who want to add simple but powerful geolocation features to their mobile apps.
By integrating the IP to location API, you provide a smoother user experience without demanding permissions or draining battery life. From content personalization to intelligent defaults, IP-based geolocation is a game changer.
You now have a working base app that shows the user’s current location. Expand it further by combining with other APIs or turning it into a more robust service.
Subscribe to my newsletter
Read articles from Author Shivani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
