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

Master Map & Filter, Javascript’s Most Powerful Array Functions

Master Map & Filter, Javascript’s Most Powerful Array Functions Learn how Array.map and Array.filter work by writing them yourself This article is for those who have written a  for  loop before, but don’t quite understand how  Array.map  or  Array.filter  work. You should also be able to write a basic function. By the end of this, you’ll have a complete understanding of both functions, because you’ll have seen how they’re written. Array.map Array.map  is meant to transform one array into another by performing some operation on each of its values. The original array is left untouched and the function returns a new, transformed array. For example, say we have an array of numbers and we want to  multiply each number by three . We also don’t want to change the original array. To do this without  Array.map , we can use a standard for-loop. for-loop var originalArr = [1, 2, 3, 4, 5]; var newArr = []; for(var i = 0; i < originalArr.length; i+...

Integrate the FaceID or TouchID Authentication in iOS Apps - Swift

FaceID or TouchID Authentication Dear Folks, today I am going to explain the proper way to integrate the Face ID or Touch ID in iOS App. Face ID and Touch ID are secure, familiar authentication methods that people trust. Follow below steps: Step 1: Add NSFaceIDUsageDescription Key in info.plist file <key>NSFaceIDUsageDescription</key> <string>This app requires Face ID permission to authenticate using Face recognition.</string> Step 2: Create a swift file with any name but it would be better to related to its functionality, Like BioMetrixFile.swift Step 3: In BioMetrixFile write below codes and follow instructions. But before proceeding further import LocalAuthentication class in BioMetrixFile. Now, create typealias for success or failure blocks to well manage Authentication Success or Failure. public typealias AuthenticationSuccess = (() -> ()) public typealias AuthenticationFailure = ((AuthenticationError) -> ()) Now written all s...

React Native: How To Open App Settings Page

  React Native: How To Open App Settings Page React Native has a Linking module to manage the interaction with other applications in the device. In this article, I will explain how you can open “Application Setting Page” of the device using. Sample Case Before I explain how to do this, I’ll talk about an example case where we might need this: You need to get location permission within the application from a user using an iOS operating system and you asked for permission. But user did not agree to grant this permission the first time. In this case, you cannot ask your user for location permission the second time. In this case, the only thing to do is to direct the user to the settings page of our application and make it manually allow it. Now that we have explained our example situation, we can move on to how we can redirect our user to the “settings page of our application”. Open App Settings Page iOS If our user is using an iOS operating system, we can handle the whole process wit...