iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🍣

[ReactNative] react-native-maps Basics

に公開
2021/07/26

Introduction

In this article, I will introduce react-native-maps, a library used for displaying maps in ReactNative.
While the installation itself is simple, using Google Maps requires a few extra steps, so I will explain those as well.

What is react-native-maps?

https://github.com/react-native-maps/react-native-maps

This is a map component library for ReactNative.
Personally, I think this is the go-to choice if you want to display maps.

Installation

I will assume the following environment:

  • react-native@0.63.3
  • react-native-maps@0.27.1
yarn add react-native-maps

For iOS, pod install is required.

cd ios
pod install

Since location information is used, you need to add a description to NSLocationWhenInUseUsageDescription in Info.plist.

<key>NSLocationWhenInUseUsageDescription</key>
<string>Location information is acquired for plotting on the map</string>

When using Google Maps

When using Google Maps, configuration on the GCP side is required.
By the way, while integrating Google Maps into an application usually incurs costs, it can be used for free in mobile apps because they use Maps SDK for iOS and Maps SDK for Android.

https://cloud.google.com/maps-platform/pricing?hl=ja

googlemap

Next, I will introduce the work required for iOS and Android.

Android/iOS Common Tasks

Create a GCP project.

iOS

Enable Maps SDK for iOS from the [API Settings].

ios

Follow the on-screen instructions to obtain the API key for Maps SDK for iOS.

Modify AppDelegate.m

Enter the obtained API key into ios/[project]/AppDelegate.m.

#import <GoogleMaps/GoogleMaps.h>  // Add this

// ...omitted

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [GMSServices provideAPIKey:@"【API_KEY】"]; // Add this
}

Next, modify ios/Podfile.

# Add these
pod 'react-native-google-maps', path:'../node_modules/react-native-maps'
pod 'GoogleMaps'
pod 'Google-Maps-iOS-Utils'

Android

Enable Maps SDK for Android in the [API Settings].
Note: The screen is almost the same as iOS, so it is omitted.

Obtain the API key for Maps SDK for Android and modify android/build.gradle.

buildscript {
    dependencies {
        classpath('com.google.android.gms:play-services-base:10.0.1')
        classpath('com.google.android.gms:play-services-maps:10.0.1')
    }
}

Furthermore, enter the API key in android/src/main/AndroidManifest.xml.

<application>
    <meta-data 
      android:name="com.google.android.geo.API_KEY"
      android:value="【API KEY】" />
</application>

How to Use

Use MapView to display the map.
Below is a sample of displaying the map in full screen with fixed coordinates.

App.tsx
import React from 'react';
import {View, StyleSheet} from 'react-native';
import MapView, {PROVIDER_GOOGLE} from 'react-native-maps';

const App: React.FC = () => {
  return (
    <View style={StyleSheet.absoluteFillObject}>
      <MapView
        provider={PROVIDER_GOOGLE}
        style={StyleSheet.absoluteFillObject}
        initialRegion={{
          latitude: 37.78825,
          longitude: -122.4324,
          latitudeDelta: 0.0922,
          longitudeDelta: 0.0421,
        }}
      />
    </View>
  );
};

export default App;

Display

result

Conclusion

In this article, I introduced how to use react-native-maps.
While the goal of this article was simply to display a map, there are many advanced things you can do, such as "plotting the current location" or "placing a marker at a tapped location."

In future articles, I plan to cover these topics as part of an advanced react-native-maps series.

Discussion