React Native Maps: How to install and use the map library on iOS and Android [2020]

Maps have become one of the most popular interfaces for many of the applications that we have installed on our phones. Learning to work on maps, represent information appropriately and create a good navigation interface is becoming increasingly important.

In this post we will see how to integrate Google Maps into a React Native app using the react-native-maps library for iOS and Android. To develop an example as realistic as possible, we will recreate an Uber-style interface using a Bottom Sheet.

At the end of the post we will be able to develop an application like this one here.

Project creation

For this project we are going to use Expo to speed up the installation process and make it easy for anyone who wants to download the repository to test the application. If you still do not have expo installed you can follow the official installation guide.

The first thing we will do is create a blank project using the expo cli.

Install react-native-maps library con Google Maps

Once the project is created, the next step is to add the react-native-map library using the command below.

If you are not using expo in your project, you can use this command

The difference between the first command and the second is that using the Expo cli we make sure to use the latest version of the library compatible with Expo.

It is worth mentioning that we can use the react-native-maps library with both Apple Maps and Google Maps. In this tutorial we will focus on using Google Maps as a map provider, but the steps to integrate Apple Maps are very similar.

Get the Google Maps API Key

In order to use Google Maps in our application, it is necessary to enable the iOS and Android SDK in a Google project with an active billing account in the Google Cloud Console and generate an API key to add it to our codebase.

Let's see step by step how to obtain the Google Maps API Key.

1. The first thing we will do is going to Google Cloud Console and create a new project that we will name google-maps-example-reboot.

2. Once we have created our project, we need to enable the Maps SDK for Android and the Maps SDK for iOS within the apis and services library.

Google Cloud APIs Library

Enable Google Maps SDK for iOS

3. Once the sdks are enabled, we need to create an API key. For this we go to the Control Panel → Create Credentials → API Key

Google Cloud Admin Panel

Create API Key on Google Cloud

4. Once the API key is created, it is highly recommended to limit it to the libraries that we want to use and to the applications that will have permission to use it using the application's fingerprint and the identifier bundle.

Now we have the API Key that we need to add to our application. Depending on whether we are using expo or a bare project, the way to do it will change.

Add API Key on Expo

On Expo we simply go to app.json and add this snippet:

Add API Key on Android

If it is a Bare Android project it will be necessary to add the API Key in google_maps_api.xml in the path android/app/src/main/res/values.

Add API Key on iOS

On iOS you need to edit AppDelegate.m file to include the following snippet.

It is important to note that when using location permissions you must tell Apple why you need to access the user's location, otherwise Apple will reject your application when you upload it to the App Store. This can be done in the Info.plist file by editing the NSLocationWhenInUseUsageDescription field explaining clearly and concisely why you need to know the location.

Add and customize a map in React Native

Now that we have integrated the map library, we are going to start by creating a screen with the map visualization and customizing the style with the different options it provides. For this we are going to create a Map.js component like the following.

As we can see, the main component is MapView that has multiple props to customize its behavior. In this case, the most important ones are provider where we indicate that we want to use Google Maps, initialRegion which will be the initial location, mapType where we can define the type of map that is loaded and finally customMapStyle where we will set the custom style of the map we want to use.

If we look at the Google's official documentation we see that we can customize almost all the elements of the map. In this case we seek to make a minimalist interface so we will use the following styles.

Customizing a Google map can be tedious, that's why there are websites like Snazzymaps that gather templates with different styles that we can directly copy their attributes and use as a template.

Add Markers to Google Maps in React Native

The next thing we will do is to add markers to our map. To do this we will create a constant MARKERS_DATA with the following structure.

Once we have our data ready, we can add it to the map by importing the library's Marker component within MapView. To do this we will use an Array.map function with the MARKERS_DATA that we have created.

Voilà! We already have our markers on the map. But it still looks like any standard Google Maps map, so in the next step we're going to give it some personality by customizing the style of the markers.

Customize Google Maps Markers in React Native

The react-native-maps library includes several props to customize the style of the markers, but the best option if you want to create completely customized markers is to use the Marker component as a wrapper and create your own component with the style you want.

Following our minimalist interface we will add some circular markers and we will smoothly animate the size when the marker is selected.

We are going to create the CustomMarker component and a useMarkerAnimation hook to manage the interaction of the animation.

To manage the animations we have added the Reanimated and Redash libraries.

Finally we replace the default Marker from the map screen with our custom marker we have just created.

All right! We already have our custom markers in our map app. But there is still a step left: we need to be able to navigate between the different markers. To do this we will create an interface based on a Bottom Sheet similar to the one found in applications such as Uber or Google Maps. This component will allow us to manage navigation between markers.

Manage map navigation

Let's see how we can navigate the map using both the animateCamera and the animateToRegion function. For this we need to create a map reference to be able to use it and call these functions. In our case we have created a hook to manage this logic.

As we can see in the code above, the functions are quite simple. The animateCamera function receive as parameters: the center with the latitude and longitude, the Zoom and the time that the animation will take. In the case of animateToRegion function, the logic is very similar but instead of using the Type Camera it uses the Type Region.

In our case we have also added a setSelectedMarker to be able to enlarge the marker when the camera uses it as the center.

To use the hook we simply have to add it to our Map component. But before that we will create the component above the map to be able to use the hook functions.

We are going to create a Bottom Sheet component with the list of locations so when you click on one of these, the camera will move to that point and the selected marker will expand. For the component we have used the 'react-native-scroll-bottom-sheet' library that uses Reanimated to manage the component animations.

We will also add a top menu that will allow us to reset the state of our map.

Finally the map component would look like this.

We have managed to build a maps application with a very simple interface that allows us to manage navigation between the different points of interest in a very intuitive way. Much more complex products can be built on top of this, but it is a good starting point if you are developing a map app in React Native in 2020.

The complete project is available on GitHub so that you can download and work on it.