Gathering detailed insights and metrics for react-native-bmap-geolocation
Gathering detailed insights and metrics for react-native-bmap-geolocation
Gathering detailed insights and metrics for react-native-bmap-geolocation
Gathering detailed insights and metrics for react-native-bmap-geolocation
npm install react-native-bmap-geolocation
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
18
React Native geolocation service for iOS and Android using Baidu SDK on Android.
This library is a complete clone of react-native-amap-geolocation with the Android implementation replaced by Baidu Location SDK for enhanced accuracy in China.
1npm install react-native-bmap-geolocation 2# or 3yarn add react-native-bmap-geolocation
For iOS, you need to add location permissions to your Info.plist
:
1<key>NSLocationWhenInUseUsageDescription</key> 2<string>This app needs access to location when open.</string> 3<key>NSLocationAlwaysAndWhenInUseUsageDescription</key> 4<string>This app needs access to location when in the background.</string>
Run pod install:
1cd ios && pod install
android/app/src/main/AndroidManifest.xml
:1<application> 2 <meta-data 3 android:name="com.baidu.lbsapi.API_KEY" 4 android:value="YOUR_BAIDU_API_KEY" /> 5</application>
android/app/src/main/AndroidManifest.xml
:1<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 2<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 3<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 4<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 5<uses-permission android:name="android.permission.INTERNET" /> 6<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 7<uses-permission android:name="android.permission.READ_PHONE_STATE" />
MainApplication.java
:1import com.rnbmap.RNBmapGeolocationPackage; 2 3@Override 4protected List<ReactPackage> getPackages() { 5 return Arrays.<ReactPackage>asList( 6 new MainReactPackage(), 7 new RNBmapGeolocationPackage() // Add this line 8 ); 9}
The API is identical to react-native-amap-geolocation
:
1import { init } from 'react-native-bmap-geolocation'; 2 3// Initialize with API keys 4await init({ 5 ios: 'YOUR_IOS_API_KEY', // Optional for iOS 6 android: 'YOUR_ANDROID_API_KEY' 7});
1import Geolocation, { 2 setOptions, 3 start, 4 stop, 5 addLocationListener 6} from 'react-native-bmap-geolocation'; 7 8// Set location options 9setOptions({ 10 accuracy: 'HighAccuracy', 11 locationTimeout: 15000, 12 withReGeocode: true, 13 distanceFilter: 10 14}); 15 16// Add location listener 17addLocationListener(location => { 18 console.log('Location update:', location); 19}); 20 21// Start location updates 22start(); 23 24// Stop location updates when done 25stop();
1import { getCurrentPosition } from 'react-native-bmap-geolocation'; 2 3try { 4 const location = await getCurrentPosition(); 5 console.log('Current position:', location); 6} catch (error) { 7 console.error('Location error:', error); 8}
1import { geocode, reGeocode } from 'react-native-bmap-geolocation'; 2 3// Geocoding (address to coordinates) 4try { 5 const results = await geocode({ 6 address: '北京市朝阳区', 7 city: '北京' 8 }); 9 console.log('Geocode results:', results); 10} catch (error) { 11 console.error('Geocoding error:', error); 12} 13 14// Reverse geocoding (coordinates to address) 15try { 16 const result = await reGeocode({ 17 latitude: 39.9042, 18 longitude: 116.4074 19 }); 20 console.log('Reverse geocode result:', result); 21} catch (error) { 22 console.error('Reverse geocoding error:', error); 23}
1import { 2 requestLocationPermission, 3 getLocationPermission, 4 isLocationEnabled 5} from 'react-native-bmap-geolocation'; 6 7// Check if location services are enabled 8const enabled = await isLocationEnabled(); 9 10// Get current permission status 11const status = await getLocationPermission(); 12 13// Request location permission 14const permission = await requestLocationPermission();
init(options: InitOptions): Promise<void>
Initialize the SDK with API keys.
Parameters:
options.ios?: string
- iOS API key (optional)options.android?: string
- Android API key (required for Android)setOptions(options: LocationOptions): void
Set location options.
Parameters:
options.accuracy?: 'BatteryOptimized' | 'DeviceSensors' | 'HighAccuracy'
- Location accuracyoptions.locationTimeout?: number
- Location timeout in millisecondsoptions.withReGeocode?: boolean
- Whether to include reverse geocodingoptions.distanceFilter?: number
- Minimum distance for location updatesoptions.onceLocation?: boolean
- Single location requeststart(): void
Start location updates.
stop(): void
Stop location updates.
getCurrentPosition(): Promise<Location>
Get current position once.
addLocationListener(listener: (location: Location) => void): void
Add location update listener.
removeLocationListener(): void
Remove location listener.
geocode(options: GeocodeOptions): Promise<GeocodeResult[]>
Convert address to coordinates.
reGeocode(options: ReGeocodeOptions): Promise<ReGeocode>
Convert coordinates to address.
Location
1interface Location { 2 timestamp: number; 3 coordinates: { 4 latitude: number; 5 longitude: number; 6 accuracy: number; 7 altitude: number; 8 heading: number; 9 speed: number; 10 }; 11 reGeocode?: ReGeocode; 12 locationType?: number; 13 accuracy?: number; 14}
ReGeocode
1interface ReGeocode { 2 formattedAddress?: string; 3 country?: string; 4 province?: string; 5 city?: string; 6 district?: string; 7 township?: string; 8 // ... more fields 9}
1
- Permission denied2
- Position unavailable3
- TimeoutThis library is a drop-in replacement for react-native-amap-geolocation
. Simply replace the import:
1// Before 2import Geolocation from 'react-native-amap-geolocation'; 3 4// After 5import Geolocation from 'react-native-bmap-geolocation';
All other APIs remain exactly the same!
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
No vulnerabilities found.
No security vulnerabilities found.