# react-native-bmap
**Repository Path**: geekcome/react-native-bmap
## Basic Information
- **Project Name**: react-native-bmap
- **Description**: ReactNative百度地图的封装
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2022-09-29
- **Last Updated**: 2022-09-29
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# react-native-baidumap-sdk [![npm version][version-badge]][npm] [![build status][build-badge]][build]
**注意:该项目现在正在进行重构,接口重新设计且部分重新实现。**
react-native 百度地图组件,支持 Android + iOS。
## 安装
```bash
npm i react-native-baidumap-sdk
```
### 添加 API Key
首先你需要获取百度地图 API Key:
- [Android](http://lbs.amap.com/api/android-sdk/guide/create-project/get-key)
- [iOS](https://lbs.amap.com/api/ios-sdk/guide/create-project/get-key)
然后你需要在显示地图前调用接口设置 API key:
```js
import { BaiduMapSdk } from "react-native-baidumap-sdk";
BaiduMapSdk.init(
Platform.select({
android: "c52c7169e6df23490e3114330098aaac",
ios: "186d3464209b74effa4d8391f441f14d",
})
);
```
## 用法
### 显示地图
```jsx
import { MapView, MapType } from "react-native-baidumap-sdk";
;
```
### 监听地图事件
```jsx
import { MapView } from 'react-native-baidumap-sdk'
render() {
return (
console.log('onLoad')}
onPress={({ nativeEvent }) => console.log(nativeEvent)}
onCameraIdle={({ nativeEvent }) => console.log(nativeEvent)}
/>
)
}
```
### 添加标记
其中 `icon` 支持 [ImageSource](https://reactnative.dev/docs/image#imagesource)。
同时支持 `children` 作为标记图标。
```jsx
import { MapView, Marker } from "react-native-baidumap-sdk";
alert("onPress")}
/>
{new Date().toLocaleString()}
;
```
### 点聚合
Marker 数量过多(尤其是使用自定义 View 的情况下)会导致性能问题,而且显示过于密集,这时候可以用点聚合改善。
```jsx
import { Cluster, MapView, Marker } from "react-native-baidumap-sdk";
const markers = Array(1000)
.fill(0)
.map((_, i) => ({
position: { latitude: 39.5 + Math.random(), longitude: 116 + Math.random() },
properties: { key: `Marker${i}` },
}));
(this.mapView = ref)}
onLoad={() => this.mapView?.moveCamera({ zoom: 8 }, 100)}
onCameraIdle={({ nativeEvent }) => {
this.status = nativeEvent;
this.cluster?.update(nativeEvent);
}}
>
{
this.mapView?.moveCamera(
{
target: position,
zoom: this.status!.cameraPosition.zoom! + 1,
},
200
);
}}
ref={(ref) => (this.cluster = ref)}
points={markers}
renderMarker={(item) => (
)}
/>
```
### 更多示例
参考 [examples](https://github.com/qiuxiang/react-native-baidumap-sdk/tree/master/example/examples)。
#### Android
```bash
npm run android
```
#### iOS
```bash
cd ios && pod install && cd ..
npm run ios
```
## 常见问题
- 尽量使用真实设备进行测试,在模拟器可能存在一些问题(常见的是 Android 模拟器因为缺少 GPU 加速而导致闪退)。
- onLocation 没有返回定位数据通常是因为 key 不正确,或没有申请 PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION 权限
[npm]: https://www.npmjs.com/package/react-native-baidumap-sdk
[version-badge]: https://badge.fury.io/js/react-native-baidumap-sdk.svg
[build-badge]: https://travis-ci.org/qiuxiang/react-native-baidumap-sdk.svg?branch=master
[build]: https://travis-ci.org/qiuxiang/react-native-baidumap-sdk