Integration of Google map in React Native using react-native-maps

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

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods. So to install pods use

cd ios && pod install && cd ..

This is how we have installed the dependency. Now to open the google map in the app we have to get the API key for from Google Developer Console and to do that please follow the below steps.

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

/*This is an Example of React Native Map*/
import React from 'react';
import { StyleSheet, Text, View , TextInput} from 'react-native';
import MapView, {Marker} from 'react-native-maps';
 
export default class App extends React.Component {
  onRegionChange(region) {
    this.setState({ region });
  }
  render() {
    var 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"}]}];
    return (
      <View style={styles.container}>
        <MapView
          style={styles.map}
          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>
    );
  }
}
 
const styles = StyleSheet.create({
  container: {
    position:'absolute',
    top:0,
    left:0,
    right:0,
    bottom:0,
    alignItems: 'center',
    justifyContent: 'flex-end',
  },
  map: {
    position:'absolute',
    top:0,
    left:0,
    right:0,
    bottom:0,
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots

Android

After running the map example on Android you will see the google map as below

      

iOS

In the case of IOS, you will see Apple Maps instead of the Google Map.
Because by default IOS opens the Apple Map instead of Google Map.
      

Integration of Google map for iOS in React Native

Apple maps will also have the same features as Google Maps but if you want to keep your cross-platform application align then you have to draw a Google map in the IOS application too.

So to do that you have to add provider={PROVIDER_GOOGLE} prop in the <MapView /> but if you simply add the PROVIDER_GOOGLE in the MapView and run it, you will face the following error

“react-native-maps: AirGooglMaps dir must be added to your Xcode project to support GoogleMaps on iOS”

To solve the above error we will add AirGoogleMaps dir in the Xcode project as suggested by the error itself.

Adding AirGoogleMaps directory in XCode Project to support GoogleMaps

Please follow the following steps for that

1. First of all, open the pod file of the iOS project and add the following line as given in the screenshot

pod 'GoogleMaps'

2. Now open the terminal, jump into the project and install the pod using

cd ios
pod install 

3. Now Open the project in the Xcode and find AirGooglrMaps in the node_modules here at node_modules > react-native-maps > lib > ios > AirGooglrMaps.

4. Now drag that folder into your project’s main directory. For example, my project is RNMapExample so I selected AirGooglrMaps and dragged it into the RNMapExample directory in Xcode. After that, it will ask to choose options for adding these files. Please note you must have to select “Copy items if needed” and “Create groups” options.

5. After doing that you can see the additional AirGooglrMaps directory that is what our error said.

6. Now we have to add an additional flag for that first select your project then please select Build Settings after that search for the preprocessor and click <Multiple Values> in front of Preprocessor Macros. Here you will see a popup please click on add sign and it will give you an option to add some value. Please add the following value into that

HAVE_GOOGLE_MAPS=1

7. Now open the AppDelegate.m file of the mail project and copy the following lines as give in the screenshot.

At the top

#import <GoogleMaps/GoogleMaps.h>

In the starting of the first function

[GMSServices provideAPIKey:@"Your Google API Key"];

Please replace “Your Google API Key” with the API key we have generated from the Google Console.

Now we are ready for the Google Map in the iOS 🙂

Updated Code after adding PROVIDER_GOOGLE

Please replace the code of App.js with the following code for the Google Map in iOS also. Here we are just adding an additional prop provider={PROVIDER_GOOGLE} in the MapView.

App.js

/*This is an Example of React Native Map*/
import React from 'react';
import { StyleSheet, Text, View , TextInput} from 'react-native';
import MapView, {Marker, PROVIDER_GOOGLE} from 'react-native-maps';
 
export default class App extends React.Component {
  onRegionChange(region) {
    this.setState({ region });
  }
  render() {
    var 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"}]}];
    return (
      <View style={styles.container}>
        <MapView
          provider={PROVIDER_GOOGLE}
          style={styles.map}
          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>
    );
  }
}
 
const styles = StyleSheet.create({
  container: {
    position:'absolute',
    top:0,
    left:0,
    right:0,
    bottom:0,
    alignItems: 'center',
    justifyContent: 'flex-end',
  },
  map: {
    position:'absolute',
    top:0,
    left:0,
    right:0,
    bottom:0,
  },
});

Now run the iOS App using

react-native run-ios

If everything is done correctly then you will see the Google Map in iOS app

Output Screenshots after adding PROVIDER_GOOGLE

      

This is how you can integrate Google Map in your React Native Application. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂


Related Posts

25 thoughts on “Integration of Google map in React Native using react-native-maps”

  1. Hi, this is wonderful and very easy to understand your article for react native with google map but i have successfully run the android apps but I am getting empty screen. i could see only bottom of google logo only. the map is not loaded fully. please help us. i didn’t encounter any errors.

    Reply
  2. Are you using the same StyleSheet?

    const styles = StyleSheet.create({
    container: {
    position:’absolute’,
    top:0,
    left:0,
    right:0,
    bottom:0,
    alignItems: ‘center’,
    justifyContent: ‘flex-end’,
    },
    map: {
    position:’absolute’,
    top:0,
    left:0,
    right:0,
    bottom:0,
    },
    });

    Reply
    • Thank you for your response and I fixed the issue. I was used hybrid Google map key during the development. Now I changed the android native app key. Now working fine.

      Reply
  3. REACT NATIVE MAPS NPM DOES NOT WORKS IN THE LATEST REACT 0.59.1 VERSION

    PLZ HELP…

    NPM IS INSTALLED
    PACKAGE IS ALSO LINKED

    BUT REACT NATIVE RUN ANDROID GIVES ERROR IN NEW PROJECT

    Reply
  4. i am using react native 0.60 and react native map version 0.25, after installed maps my app is crashed .. i don’t the reason

    Reply
    • With the updated of React native 0.60, you also have to update Android SDK for the AndroidX. Can you please check once? also can you please confirm your device support application version?

      Reply
  5. @karthik
    Can you please how to change to Android native API key from hybrid API key. I am doing the same, but without any success.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.