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

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

16 AWS Gotchas

16 AWS Gotchas In January I launched the MVP for my own startup,  Proximistyle , which helps you find what you’re looking for nearby. On advice from friends and industry contacts I chose AWS as my cloud provider. Having never had to set up my own cloud infrastructure before, the learning curve to get from no experience to a stable VPC system I was happy with was significantly steeper than expected, and had its fair share of surprises. #1 Take advantage of the free resources offered AWS offers a free tier for new accounts. If you have recently bought a domain and set up a company you qualify for the free tier for a year. Additionally, if you are a bootstrapped startup you can apply for  the Startup Builders package  and get $1000 in AWS credits. After doing the above, you’re now ready to get started with setting up the AWS infrastructure for your startup. #2 Set up billing budgets and alerting The very first thing you should do after setting up billing, is enabling a budge...

Ultimate Folder Structure For Your React Native Project

  Ultimate Folder Structure For Your React Native Project React native project structure React Native is a flexible framework, giving developers the freedom to choose their code structure. However, this can be a double-edged sword for beginners. Though it offers ease of coding, it can soon become challenging to manage as your project expands. Thus, a structured folder system can be beneficial in many ways like better organization, simplified module management, adhering to coding practices, and giving a professional touch to your project. This write-up discusses a version of a folder arrangement that I employ in my React Native projects. This structure is based on best practices and can be modified to suit the specific needs of your project. Before we get into the project structure let’s give credit to @sanjay who has the original idea of the structure but I modify his version of the code, to make it better. Base library axios  — For network calling. react-navigation ...