react-native init MyMap
cd MyMap
yarn add react-native-maps
Step 02: Setup iOS project (Make sure you have Cocoapods installed on your Mac)
cd ios
pod init
Open Podfile
in your vscode (atom, phpstorm etc...)
# Podfile content
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
target 'MyMap' do
# React Native
rn_path = '../node_modules/react-native'
rn_maps_path = '../node_modules/react-native-maps'
pod 'yoga', :path => "#{rn_path}/ReactCommon/yoga"
pod 'React', path: "#{rn_path}", :subspecs => [
'Core',
'CxxBridge',
'RCTActionSheet',
'RCTAnimation',
'RCTGeolocation',
'RCTImage',
'RCTLinkingIOS',
'RCTNetwork',
'RCTSettings',
'RCTText',
'RCTVibration',
'RCTWebSocket',
'DevSupport'
]
pod 'DoubleConversion', :podspec => "#{rn_path}/third-party-podspecs/DoubleConversion.podspec"
pod 'glog', :podspec => "#{rn_path}/third-party-podspecs/glog.podspec"
pod 'Folly', :podspec => "#{rn_path}/third-party-podspecs/Folly.podspec"
# Required by Google Map, ignore if you use Apple Map
pod 'GoogleMaps', '2.5.0'
pod 'Google-Maps-iOS-Utils', '2.1.0'
pod 'GoogleUtilities', '5.3.7'
pod 'GoogleAppMeasurement', '5.4'
pod 'react-native-maps', path: rn_maps_path
pod 'react-native-google-maps', path: rn_maps_path # <~~ if you need GoogleMaps support on iOS
end
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == "react-native-google-maps"
target.build_configurations.each do |config|
config.build_settings['CLANG_ENABLE_MODULES'] = 'No'
end
end
if target.name == "React"
target.remove_from_project
end
end
end
// If your pod repo too old, do update before install: pod repo update
pod install
open MyMap.xcworkspace
open .
Go to Google console, choose or create new project, go to library then enable 2 libraries:
- Google Map SDK for iOS
- Google Map Android API
Then create new Credentials
key
...
#import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
@import GoogleMaps; # INSERT THIS LINE
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation;
[GMSServices provideAPIKey:@"YOUR_KEY"]; # INSERT THIS LINE
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
...
You're done setting up iOS project, you can now react-native run-ios
ext {
buildToolsVersion = "27.0.3"
minSdkVersion = 19
compileSdkVersion = 27
targetSdkVersion = 27
supportLibVersion = "27.1.1"
googleFirebaseVersion = "16.0.1"
googlePlayServicesVersion = "15.0.1"
}
subprojects {
afterEvaluate {project ->
if (project.hasProperty("android")) {
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
}
}
}
}
include ':react-native-maps'
project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-maps/lib/android')
// upgrade sdk version
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
targetSdkVersion 23 // Upgrade target to 23 (You will need use this to check android permission correctly)
...
}
...
}
// update dependencies
dependencies {
...
// Paste these line
implementation(project(':react-native-maps')){
exclude group: 'com.google.android.gms', module: 'play-services-base'
exclude group: 'com.google.android.gms', module: 'play-services-maps'
}
implementation "com.google.android.gms:play-services-location:${rootProject.ext.googlePlayServicesVersion}" // disable if you don't want to use enableHighAccuracy: true in geolocation
implementation "com.google.android.gms:play-services-base:${rootProject.ext.googlePlayServicesVersion}"
implementation "com.google.android.gms:play-services-maps:${rootProject.ext.googlePlayServicesVersion}"
}
<manifest
...
// 2 Permission bellow are not required, just use when you have to use geolocation API
// If you need FINE location, insert this one
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
// Or insert this line if you don't need FINE location
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application>
<uses-library android:name="org.apache.http.legacy" android:required="false" /> <!-- insert this line to fix android target 28+ -->
<!-- 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>
</manifets?
import com.airbnb.android.react.maps.MapsPackage; // INSERT THIS LINE
// getPackages
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
...,
new MapsPackage() // INSERT THIS LINE
);
}
YOU ARE NOW READY TO FOLLOW USAGE GUIDE.
##########################################################################
import React from 'react';
import { View, Text } from 'react-native';
import MapView from 'react-native-maps';
export default class MyMap extends React.Component {
state = {
latitude: 20.9948891,
longitude: 105.799677,
latitudeDelta: 0.002,
longitudeDelta: 0.002
}
render() {
return (
<View style={styles.container}>
<Text style={styles.text}>Welcome to react-native-maps</Text>
<MapView style={styles.map} initialRegion={this.state}>
<MapView.Marker coordinate={this.state} />
</MapView>
</View>
);
}
}
const styles = {
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff'
},
text: {
fontSize: 30,
fontWeight: '700',
color: '#59656C',
marginBottom: 10,
},
map: {
width: null,
height: 300,
flex: 1
}
};
MIT License
Copyright (c) 2017 anhtuank7c
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.