Skip to main content

React Native Google Map

Google map is used to locate an address, navigate, and search location in the mobile devices. The Google Maps shows the location (latitude and longitude) using dot Marker. In the react-native, Google Maps is easily integrated using react-native-maps npm library. To use Google Maps in our application, we need to authenticate the Google Maps API.

1. Create the react-native project

Create the react-native project and install the react-native-maps library using the below command

  1. npm install -save react-native-maps  

React Native Google Map

After successful execution of the above code, it installs the react-native-maps library, which can be seen in package.json file.


React Native Google Map

2. Generate Google Maps authentication API key from the Google Developer Console

2.1 To use Google Maps in our application, we need to generate and authenticate the Google Maps API key. Login to https://console.developers.google.com/ with your google mail account and create a new project.

React Native Google Map

2.2 Now, click on API and Services -> Credentials -> Create credentials to create API credentials.


React Native Google Map

2.3 It will pop up your API key with message API key created.


React Native Google Map


2.4 To see an overview of your API key, click Google Maps -> Overview.


React Native Google Map


React Native Google Map
2.5 Now, go to API Library and select Maps SDK for Android to enable the Map API
React Native Google Map



React Native Google Map
3. Open your project_name -> android -> setting.gradle file and add the below code:

  1. include ':react-native-maps'  
  2. project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-maps/lib/android')  
4. Again open your project_name ->android -> build.gradle file and add the below code in dependencies block.

  1. implementation project(':react-native-maps')  
5. Go to project_name -> android ->app->src->main->java->com->project_name-> MainApplication.java and add the below code:

  1. import com.airbnb.android.react.maps.MapsPackage;  
  2. ...  
  3. new MapsPackage()  
MainApplication.java

  1. package com.maps;  
  2. import android.app.Application;  
  3. import com.facebook.react.ReactApplication;  
  4. import com.airbnb.android.react.maps.MapsPackage;  
  5. import com.facebook.react.ReactNativeHost;  
  6. import com.facebook.react.ReactPackage;  
  7. import com.facebook.react.shell.MainReactPackage;  
  8. import com.facebook.soloader.SoLoader;  
  9. import com.airbnb.android.react.maps.MapsPackage;  
  10. import java.util.Arrays;  
  11. import java.util.List;  
  12.   
  13. public class MainApplication extends Application implements ReactApplication {  
  14.   private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {  
  15.     @Override  
  16.     public boolean getUseDeveloperSupport() {  
  17.       return BuildConfig.DEBUG;  
  18.     }  
  19.     @Override  
  20.     protected List<ReactPackage> getPackages() {  
  21.       return Arrays.<ReactPackage>asList(  
  22.           new MainReactPackage(),  
  23.             new MapsPackage()  
  24.       );  
  25.     }  
  26.     @Override  
  27.     protected String getJSMainModuleName() {  
  28.       return "index";  
  29.     }  
  30.   };  
  31.   @Override  
  32.   public ReactNativeHost getReactNativeHost() {  
  33.     return mReactNativeHost;  
  34.   }  
  35.   @Override  
  36.   public void onCreate() {  
  37.     super.onCreate();  
  38.     SoLoader.init(this/* native exopackage */ false);  
  39.   }  
6. In your project_name -> android -> app -> src -> main ->AndroidManifest.xml file add the below code inside application tag.

  1. <meta-data  
  2. android:name="com.google.android.geo.API_KEY"  
  3. android:value="Your_Google_API_Key"/>  
7. Import the MapView and Marker component from react-native-maps library in App.js file.
MapView: It is used to display the MapView component in the project.
Marker: It is used to show the red round mark to pinpoint the exact location in Google Maps.
App.js

  1. import React, { Component } from 'react';  
  2. import { StyleSheet, View } from 'react-native';  
  3. import MapView from 'react-native-maps';  
  4. import { Marker } from 'react-native-maps';  
  5.   
  6. export default class App extends Component {  
  7.   render() {  
  8.     return (  
  9.       <View style={styles.MainContainer}>  
  10.   
  11.         <MapView  
  12.           style={styles.mapStyle}  
  13.           showsUserLocation={false}  
  14.           zoomEnabled={true}  
  15.           zoomControlEnabled={true}  
  16.           initialRegion={{  
  17.             latitude: 28.579660,   
  18.             longitude: 77.321110,  
  19.             latitudeDelta: 0.0922,  
  20.             longitudeDelta: 0.0421,  
  21.           }}>  
  22.   
  23.           <Marker  
  24.             coordinate={{ latitude: 28.579660, longitude: 77.321110 }}  
  25.             title={"JavaTpoint"}  
  26.             description={"Java Training Institute"}  
  27.           />  
  28.         </MapView>  
  29.           
  30.       </View>  
  31.     );  
  32.   }  
  33. }  
  34.   
  35. const styles = StyleSheet.create({  
  36.   MainContainer: {  
  37.     position: 'absolute',  
  38.     top: 0,  
  39.     left: 0,  
  40.     right: 0,  
  41.     bottom: 0,  
  42.     alignItems: 'center',  
  43.     justifyContent: 'flex-end',  
  44.   },  
  45.   mapStyle: {  
  46.     position: 'absolute',  
  47.     top: 0,  
  48.     left: 0,  
  49.     right: 0,  
  50.     bottom: 0,  
  51.   },  
  52. });  

Output:

React Native Google Map React Native Google Map

Comments

Popular Posts

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...