Integrating Google maps with React native
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 init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli
command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the — version argument:
react-native init ProjectName --version X.XX.Xreact-native init ProjectName --version react-native@next
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
.
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,
},
});
Output Screenshots
Android
After running the map example on Android you will see the google map as below
Here you go, implement it and learn new things.