📜  react native map install npm - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:19:43.545000             🧑  作者: Mango

React Native Map Install

React Native is a popular open-source framework for building native mobile apps using JavaScript and React. One of the most useful features of a mobile app is the ability to display maps and map-related information. In this guide, we will show you how to install and use the React Native Maps library, the most popular library for implementing maps in React Native apps.

Prerequisites

Before you begin, make sure you have the following installed:

  • Node.js
  • npm
  • React Native CLI
Installing React Native Maps

To start using React Native maps, you will need to install the library via npm in your project directory. Open your terminal and navigate to your project directory:

cd my-project

Next, install React Native Maps via npm:

npm install react-native-maps --save

This will download and install the latest version of React Native Maps, and save it as a dependency in your project's package.json file.

Configuring React Native Maps for iOS
Configuring for iOS with Cocoapods

If you're using Cocoapods, add the following line to your Podfile:

pod 'react-native-maps', '~> 0.28.0'

Then run:

pod install
Configuring for iOS without Cocoapods

If you're not using Cocoapods, follow these steps:

1. Link the library

You must link the library to your project by running:

react-native link react-native-maps

2. Configure the app permissions

Add the following to the Info.plist file of your app:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your message to user when the app requests permission for location updates</string>

If you want to show the user their current location on the map, add the following to your project's AppDelegate.m file:

#import <MapKit/MapKit.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [MKMapView class];
    // ...
    return YES;
}
Configuring React Native Maps for Android
Configuring the Google Maps API Key

For Android, you will need to obtain an API key from Google Maps. Follow the instructions in the official documentation to obtain an API key.

Once you have the API key, add it to your app's AndroidManifest.xml file, inside the <application> tag:

<meta-data
  android:name="com.google.android.geo.API_KEY"
  android:value="YOUR_API_KEY" />
Configuring the app permissions

Add the following to your app's AndroidManifest.xml file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Configuring the build.gradle file

Add the following to your app's build.gradle file:

dependencies {
  implementation 'com.google.android.gms:play-services-maps:17.0.1'
}
Using React Native Maps in your app

To use React Native Maps in your app, simply import it into your component and use the MapView component:

import React, { Component } from 'react';
import { View } from 'react-native';
import MapView from 'react-native-maps';

class MyMap extends Component {
  render() {
    return (
      <MapView
        style={{ flex: 1 }}
        initialRegion={{
          latitude: 37.78825,
          longitude: -122.4324,
          latitudeDelta: 0.0922,
          longitudeDelta: 0.0421,
        }}
      />
    );
  }
}

export default MyMap;

In this example, we're importing the MapView component from the react-native-maps library, and using it to display a map with an initial region in San Francisco.

Conclusion

That's it! You have successfully installed and configured React Native Maps for your app. You can now start using the MapView component to display maps in your React Native app. Happy mapping!