First, download the library from npm:
npm install react-native-maps --save
Then you must install the native dependencies. You can use rnpm (now part of react-native core) to
add native dependencies automatically:
$ react-native link
To install using Cocoapods, simply insert the following line into your Podfile and run pod install
pod 'react-native-maps', :path => '../node_modules/react-native-maps'
- Open your project in XCode, right click on
Librariesand clickAdd Files to "Your Project Name"Look undernode_modules/react-native-maps/iosand addAIRMaps.xcodeproj. - Add
libAIRMaps.ato `Build Phases -> Link Binary With Libraries. - Click on
AIRMaps.xcodeprojinLibrariesand go theBuild Settingstab. Double click the text to the right ofHeader Search Pathsand verify that it has$(SRCROOT)/../../react-native/Reactas well as$(SRCROOT)/../../react-native/Libraries/Image- if they aren't, then add them. This is so XCode is able to find the headers that theAIRMapssource files are referring to by pointing to the header files installed within thereact-nativenode_modulesdirectory. - Whenever you want to use it within React code now you can:
var MapView = require('react-native-maps');
In your android/app/build.gradle add:
...
dependencies {
...
compile 'com.airbnb.android:react-native-maps:0.8.0'
}In your android/settings.gradle add:
...
include ':react-native-maps'
project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-maps/android')
include ':react-native-maps'
project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-maps/android/lib')In your application object, add:
For React Native v0.29.0 or above:
import com.airbnb.android.react.maps.MapsPackage;
public class MyApplication extends Application implements ReactApplication {
private final ReactNativeHost reactNativeHost = new ReactNativeHost(this) {
@Override protected List<ReactPackage> getPackages() {
return Arrays.asList(
new MainReactPackage(),
new MapsPackage());
}
};
@Override public ReactNativeHost getReactNativeHost() {
return reactNativeHost;
}
}For older versions of React Native:
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new MapsPackage()) // <---- and This!
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "MyApp", null);
setContentView(mReactRootView);
}Specify your Google Maps API Key:
To develop is recommended a Browser Key without refer restriction. Go to https://console.developers.google.com/apis/credentials to check your credentials. Add your Browser API key to your manifest file:
<application>
<!-- You will only need to add this meta-data tag, but make sure it's a child of application -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="{{Your Google maps API Key Here}}"/>
</application>If that doesn't work try using an Android Key without refer restriction:
<application>
<!-- You will only need to add this meta-data tag, but make sure it's a child of application -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="{{@string/ANDROID_GOOGLE_MAPS_API_KEY}}"/>
</application>Ensure that you have Google Play Services installed:
- For Genymotion you can follow these instructions.
- For a physical device you need to search on Google 'Google Play Services'. There will be a link that takes you to the play store and from there you will see a button to update it (do not search within the Play Store).
Troubleshooting
If you have a blank map issue, (#118, #176) try the following lines :
On iOS :
You have to link dependencies with rnpm and re-run the build :
rnpm linkreact-native run-ios
On Android :
- Set this Stylesheet in your map component
...
const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
height: 400,
width: 400,
justifyContent: 'flex-end',
alignItems: 'center',
},
map: {
...StyleSheet.absoluteFillObject,
},
});
module.exports = class MyApp extends React.Component {
render() {
const { region } = this.props;
console.log(region);
return (
<View style ={styles.container}>
<MapView
style={styles.map}
region={{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
}}
>
</MapView>
</View>
);
}
}
-
If not installed yet, you have to install the following packages :
- Extras / Google Play services
- Extras / Google Repository
- Android 6.0 (API 23) / Google APIs Intel x86 Atom System Image Rev. 13
-
Generate your SHA1 key :
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android -
Go to Google API Console and select your project, or create one.
InOverview -> Google Maps API -> Google Maps Android API-> Check if it's enabled
Create a new key by clicking onCreate credentials -> API Key -> Android Key, enter the name of the API key and your SHA1 key, generated before, and create it. -
Clean the cache :
watchman watch-del-all
npm cache clean -
When starting emulator, make sure you have enabled
Wipe user data. -
Run
react-native run-android
At this step it should work, but if not, go to your Google API Console and create a Browser key instead of a Android key