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

Reloading UITableView while Animating Scroll in iOS 11

Reloading UITableView while Animating Scroll Calling  reloadData  on  UITableView  may not be the most efficient way to update your cells, but sometimes it’s easier to ensure the data you are storing is in sync with what your  UITableView  is showing. In iOS 10  reloadData  could be called at any time and it would not affect the scrolling UI of  UITableView . However, in iOS 11 calling  reloadData  while your  UITableView  is animating scrolling causes the  UITableView  to stop its scroll animation and not complete. We noticed this is only true for scroll animations triggered via one of the  UITableView  methods (such as  scrollToRow(at:at:animated:) ) and not for scroll animations caused by user interaction. This can be an issue when server responses trigger a  reloadData  call since they can happen at any moment, possibly when scroll animation is occurring. Example of s...

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...

Xcode & Instruments: Measuring Launch time, CPU Usage, Memory Leaks, Energy Impact and Frame Rate

When you’re developing applications for modern mobile devices, it’s vital that you consider the performance footprint that it has on older devices and in less than ideal network conditions. Fortunately Apple provides several powerful tools that enable Engineers to measure, investigate and understand the different performance characteristics of an application running on an iOS device. Recently I spent some time with these tools working to better understand the performance characteristics of an eCommerce application and finding ways that we can optimise the experience for our users. We realised that applications that are increasingly performance intensive, consume excessive amounts of memory, drain battery life and feel uncomfortably slow are less likely to retain users. With the release of iOS 12.0 it’s easier than ever for users to find applications that are consuming the most of their device’s finite amount of resources. Users can now make informed decisions abou...