React Native Map Example

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

Run the following command

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.

–save is optional, it is just to update the react-native-maps dependency in your package.json file.

For React Native Version<0.60

Linking of Library

To use react-native-maps library we have to link some dependencies for React Native version<0.60 as RN ).60 brought the auto-linking feature. In order to link the library, you have to run following command

For React Native Version>=0.60

Install Pod File

React Native 0.60+ has auto-linking feature but still we have to install the pod file after the linking of the libraries. So for that run

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 get the Map API key from the Google Developer console, open 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.

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

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

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

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

      
But in the case of IOS, you will see the Apple Maps instead of the Google Map. So by default, IOS open the Apple Map instead of Google Map.
      

Apple maps will also have the same features as google map has 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 “react-native-maps: AirGooglMaps dir must be added to your Xcode project to support GoogleMaps on iOS” error.

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

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

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

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

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

At the top

In the starting of the first function

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 🙂

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

App.js

Now run the iOS App using

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

      

This is how you can integrate Google Map in your React Native Application. If you have any doubt 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. 🙂

18 thoughts on “React Native Map Example”

  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.

  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,
    },
    });

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

  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

  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

    • 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?

Leave a Comment

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