React Native Map
React Native Map Example is to integrate the Google Map into your React Native Application. To integrate the Map in our example we will use a very good library called react-native-maps
. Which provides MapView component which is very easy to use. So let’s get started.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependency
To use MapView
we need to install react-native-maps
package. To install this
Open the terminal and jump into your project
cd ProjectName
Run the following command
npm install react-native-maps --save
This command will copy all the dependencies into your node_module directory, You can find the directory in node_module
the directory named react-native-maps
.
CocoaPods Installation
After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods. So to install pods use
cd ios && pod install && cd ..
This is how we have installed the dependency. Now to open the google map in the app we have to get the API key for from Google Developer Console and to do that please follow the below steps.
Generate the API Key to Integration of Google map
To get the Map API key from the Google Developer console
– Open the Developer console and log in from your Google account.
– If you have already created any project you can use that or you can create a new one like this
– After creating the project you can see all of your created project a drop-down in the top left corner. Now select the project from the dropdown and goto to the credential section.
– You can find the create credential option there.
– Hit on the create credential button and it will create an API key to the user in your Map Application
– Before we add this API key in our project you have to enable the API which you want to use in your project. For that, you can search for the Map and you will find the Map related API. We will enable Map SDK for Android because we are making this example for the Android. So click on Map SDK for Android and you will found the enable button to enable the Map API for Android.
– Now after enabling the API key, we have to add this API key to our project’s AndroidManifest.xml file. Just copy the following lines and paste it into your manifest file with your generated API key.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="Replace this with your API key"/>
Code to Integrate Google map in React Native for Android and iOS
Now Open App.js in any code editor and replace the code with the following code
Please note that we have used customMapStyle={mapStyle}
which is for the custom styling of the map in which you just have to provide the mapStyle JSON which will help you to customize your map. To get the custom style JSON please visit https://mapstyle.withgoogle.com/.
App.js
// Integration of Google map in React Native using react-native-maps
// https://aboutreact.com/react-native-map-example/
// Import React
import React from 'react';
// Import required components
import {SafeAreaView, StyleSheet, View} from 'react-native';
// Import Map and Marker
import MapView, {Marker} from 'react-native-maps';
const App = () => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<MapView
style={styles.mapStyle}
initialRegion={{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
customMapStyle={mapStyle}>
<Marker
draggable
coordinate={{
latitude: 37.78825,
longitude: -122.4324,
}}
onDragEnd={
(e) => alert(JSON.stringify(e.nativeEvent.coordinate))
}
title={'Test Marker'}
description={'This is a description of the marker'}
/>
</MapView>
</View>
</SafeAreaView>
);
};
export default App;
const mapStyle = [
{elementType: 'geometry', stylers: [{color: '#242f3e'}]},
{elementType: 'labels.text.fill', stylers: [{color: '#746855'}]},
{elementType: 'labels.text.stroke', stylers: [{color: '#242f3e'}]},
{
featureType: 'administrative.locality',
elementType: 'labels.text.fill',
stylers: [{color: '#d59563'}],
},
{
featureType: 'poi',
elementType: 'labels.text.fill',
stylers: [{color: '#d59563'}],
},
{
featureType: 'poi.park',
elementType: 'geometry',
stylers: [{color: '#263c3f'}],
},
{
featureType: 'poi.park',
elementType: 'labels.text.fill',
stylers: [{color: '#6b9a76'}],
},
{
featureType: 'road',
elementType: 'geometry',
stylers: [{color: '#38414e'}],
},
{
featureType: 'road',
elementType: 'geometry.stroke',
stylers: [{color: '#212a37'}],
},
{
featureType: 'road',
elementType: 'labels.text.fill',
stylers: [{color: '#9ca5b3'}],
},
{
featureType: 'road.highway',
elementType: 'geometry',
stylers: [{color: '#746855'}],
},
{
featureType: 'road.highway',
elementType: 'geometry.stroke',
stylers: [{color: '#1f2835'}],
},
{
featureType: 'road.highway',
elementType: 'labels.text.fill',
stylers: [{color: '#f3d19c'}],
},
{
featureType: 'transit',
elementType: 'geometry',
stylers: [{color: '#2f3948'}],
},
{
featureType: 'transit.station',
elementType: 'labels.text.fill',
stylers: [{color: '#d59563'}],
},
{
featureType: 'water',
elementType: 'geometry',
stylers: [{color: '#17263c'}],
},
{
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [{color: '#515c6d'}],
},
{
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [{color: '#17263c'}],
},
];
const styles = StyleSheet.create({
container: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'flex-end',
},
mapStyle: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
Android
After running the map example on Android you will see the google map as below
iOS
Integration of Google map for iOS in React Native
Apple maps will also have the same features as Google Maps but if you want to keep your cross-platform application align then you have to draw a Google map in the IOS application too.
So to do that you have to add provider={PROVIDER_GOOGLE}
prop in the <MapView />
but if you simply add the PROVIDER_GOOGLE in the MapView and run it, you will face the following error
“react-native-maps: AirGooglMaps dir must be added to your Xcode project to support GoogleMaps on iOS”
To solve the above error we will add AirGoogleMaps dir in the Xcode project as suggested by the error itself.
Adding AirGoogleMaps directory in XCode Project to support GoogleMaps
Please follow the following steps for that
1. First of all, Add the following to your Podfile above the use_native_modules!
function and run pod install in the ios folder
# React Native Maps dependencies
rn_maps_path = '../node_modules/react-native-maps'
pod 'react-native-google-maps', :path => rn_maps_path
pod 'GoogleMaps'
pod 'Google-Maps-iOS-Utils'
2. Now open the terminal, jump into the project and install the pod using
cd iOS && pod install && cd ..
3. Now we have to add an additional flag for that first select your project then please select Build Settings after that search for the preprocessor and click <Multiple Values> in front of Preprocessor Macros. Here you will see a popup please click on add sign and it will give you an option to add some value. Please add the following value into that
HAVE_GOOGLE_MAPS=1
4. Now open the AppDelegate.m file of the mail project and copy the following lines as give in the screenshot.
At the top
#import <GoogleMaps/GoogleMaps.h>
In the starting of the first function
[GMSServices provideAPIKey:@"Your Google API Key"];
Please replace “Your Google API Key” with the API key we have generated from the Google Console.
Now we are ready for the Google Map in the iOS 🙂
Updated Code after adding PROVIDER_GOOGLE
Please replace the code of App.js with the following code for the Google Map in iOS also. Here we are just adding an additional prop provider={PROVIDER_GOOGLE}
in the MapView.
App.js
// Integration of Google map in React Native using react-native-maps
// https://aboutreact.com/react-native-map-example/
// Import React
import React from 'react';
// Import required components
import {SafeAreaView, StyleSheet, View} from 'react-native';
// Import Map and Marker
import MapView, {Marker, PROVIDER_GOOGLE} from 'react-native-maps';
const App = () => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<MapView
style={styles.mapStyle}
provider={PROVIDER_GOOGLE}
initialRegion={{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
customMapStyle={mapStyle}>
<Marker
draggable
coordinate={{
latitude: 37.78825,
longitude: -122.4324,
}}
onDragEnd={
(e) => alert(JSON.stringify(e.nativeEvent.coordinate))
}
title={'Test Marker'}
description={'This is a description of the marker'}
/>
</MapView>
</View>
</SafeAreaView>
);
};
export default App;
const mapStyle = [
{elementType: 'geometry', stylers: [{color: '#242f3e'}]},
{elementType: 'labels.text.fill', stylers: [{color: '#746855'}]},
{elementType: 'labels.text.stroke', stylers: [{color: '#242f3e'}]},
{
featureType: 'administrative.locality',
elementType: 'labels.text.fill',
stylers: [{color: '#d59563'}],
},
{
featureType: 'poi',
elementType: 'labels.text.fill',
stylers: [{color: '#d59563'}],
},
{
featureType: 'poi.park',
elementType: 'geometry',
stylers: [{color: '#263c3f'}],
},
{
featureType: 'poi.park',
elementType: 'labels.text.fill',
stylers: [{color: '#6b9a76'}],
},
{
featureType: 'road',
elementType: 'geometry',
stylers: [{color: '#38414e'}],
},
{
featureType: 'road',
elementType: 'geometry.stroke',
stylers: [{color: '#212a37'}],
},
{
featureType: 'road',
elementType: 'labels.text.fill',
stylers: [{color: '#9ca5b3'}],
},
{
featureType: 'road.highway',
elementType: 'geometry',
stylers: [{color: '#746855'}],
},
{
featureType: 'road.highway',
elementType: 'geometry.stroke',
stylers: [{color: '#1f2835'}],
},
{
featureType: 'road.highway',
elementType: 'labels.text.fill',
stylers: [{color: '#f3d19c'}],
},
{
featureType: 'transit',
elementType: 'geometry',
stylers: [{color: '#2f3948'}],
},
{
featureType: 'transit.station',
elementType: 'labels.text.fill',
stylers: [{color: '#d59563'}],
},
{
featureType: 'water',
elementType: 'geometry',
stylers: [{color: '#17263c'}],
},
{
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [{color: '#515c6d'}],
},
{
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [{color: '#17263c'}],
},
];
const styles = StyleSheet.create({
container: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'flex-end',
},
mapStyle: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
});
Now run the iOS App using
react-native run-ios
If everything is done correctly then you will see the Google Map in iOS app
Output Screenshots after adding PROVIDER_GOOGLE
This is how you can integrate Google Map in your React Native Application. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
Hi, this is wonderful and very easy to understand your article for react native with google map but i have successfully run the android apps but I am getting empty screen. i could see only bottom of google logo only. the map is not loaded fully. please help us. i didn’t encounter any errors.
actually, this is not a styles problem this is API key problem regenerate the API key and may it will work for you because same problem are occure with me
Are you using the same StyleSheet?
const styles = StyleSheet.create({
container: {
position:’absolute’,
top:0,
left:0,
right:0,
bottom:0,
alignItems: ‘center’,
justifyContent: ‘flex-end’,
},
map: {
position:’absolute’,
top:0,
left:0,
right:0,
bottom:0,
},
});
Thank you for your response and I fixed the issue. I was used hybrid Google map key during the development. Now I changed the android native app key. Now working fine.
how to give rotation angle to overlay image on google map?
Hi Bro Please Send idea for current location in this example
For that, you can take the idea from here Current Location Latitude and Longitude Using React Native Geolocation
@karthik
Can you please how to change to Android native API key from hybrid API key. I am doing the same, but without any success.
Very simple and wonderful article. Grate thanks to the Author. Keep up the good work.
how can I show a map background white instead of black?
You can use https://mapstyle.withgoogle.com to make your own map style and update it in mapStyle
Hi, I followed the above steps for android,but I am getting requireNativeComponent:”AIRMAP” was not found in the UI manager. error in android screen,
can you please help me,I am new in react native
Can you please have a look at this thread? https://github.com/react-native-maps/react-native-maps/issues/3099
Thanks for replying,I got the issue resolved.
Bro how I am located at
my current position with the help of button.
Hello i have using react native maps and now i need to show path between two coordinates so i have installed library called react native maps direactions but now it required that purchased google api key to show path on map. what can i do.Is there any alternative for it without using that dependancy.