# react-native-maps-use-cluster

> A hook for managing Marker cluster for react-native-maps.

Latest version **1.0.4** (published 2019-09-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-native-maps-use-cluster
pnpm add react-native-maps-use-cluster
yarn add react-native-maps-use-cluster
bun add react-native-maps-use-cluster
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2019-09-05 |
| First published | 2019-09-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Loïc Mahieu |
| Maintainers | loicmahieu |
| Keywords | react-native, react, native, maps, react-native-maps, android, ios, cluster, marker |

## Links

- npm: https://www.npmjs.com/package/react-native-maps-use-cluster
- Repository: https://github.com/loicmahieu/react-native-maps-use-cluster
- Issues: https://github.com/loicmahieu/react-native-maps-use-cluster/issues
- npm.io page: https://npm.io/package/react-native-maps-use-cluster

## Dependencies (2)

- [supercluster](https://npm.io/package/supercluster.md) ^6.0.2
- [@mapbox/geo-viewport](https://npm.io/package/@mapbox/geo-viewport.md) ^0.4.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2019-09-05
- 1.0.3 — 2019-09-05
- 1.0.2 — 2019-09-05

## README

# react-native-maps-use-cluster

[![npm version](https://badge.fury.io/js/react-native-maps-use-cluster.svg)](https://badge.fury.io/js/react-native-maps-use-cluster)

A simple hook for clustering markers for `react-native-maps`.

Completely inspired by [react-native-maps-super-cluster](https://github.com/novalabio/react-native-maps-super-cluster), most of credit goes back to their maintainers and contributors.

<p align="center">
<img src="https://github.com/LoicMahieu/react-native-maps-use-cluster/blob/master/doc/example.gif?raw=true" height="550" />
</p>

## Installation

```bash
yarn add react-native-maps-use-cluster
```

## Usage

Here is the quick how-to example:

```tsx
import React, { useRef, useState } from "react";
import { Dimensions } from "react-native";
import { useCluster } from "react-native-maps-use-cluster";

const App = () => {
  const mapViewRef = useRef<MapView>();
  const [region, setRegion] = useState<Region | undefined>();
  const { clusters, getCoordinates } = useCluster<
    { marker: IMarker } & IUseClusterItem
  >({
    items: markerInfo.map(marker => ({
      coordinate: marker,
      marker
    })),
    region,
    width: Dimensions.get("window").width,
    height: Dimensions.get("window").height
  });

  const onClusterPress = cluster => {
    if (mapViewRef.current) {
      const coordinates = getCoordinates(cluster.properties.cluster_id);
      if (coordinates && coordinates.length) {
        mapViewRef.current.fitToCoordinates(coordinates, {
          edgePadding: {
            top: 10,
            bottom: 10,
            right: 10,
            left: 10
          },
          animated: true
        });
      }
    }
  };

  const markers = clusters.map(cluster => {
    if (!cluster.properties) {
      return null;
    }

    if (cluster.properties.point_count === 0) {
      const { marker } = cluster.properties.item;
      return <Marker coordinate={marker} key={marker.id} />;
    } else {
      return (
        <Marker
          key={cluster.properties.cluster_id}
          coordinate={{
            latitude: cluster.geometry.coordinates[1],
            longitude: cluster.geometry.coordinates[0]
          }}
          onPress={() => {
            onClusterPress(cluster);
          }}
          style={{ zIndex: 99 }}
        >
          <View
            style={{
              backgroundColor: "red",
              height: 20,
              width: 20,
              borderRadius: 20,
              alignItems: "center"
            }}
          >
            <Text style={{ color: "white" }}>
              {cluster.properties.point_count}
            </Text>
          </View>
        </Marker>
      );
    }
  });

  return (
    <MapView
      provider={"google"}
      ref={ref => (mapViewRef.current = ref || undefined)}
      style={styles.map}
      onRegionChangeComplete={setRegion}
    >
      {markers}
    </MapView>
  );
};
```

For complete demo, see `examples/expo`.

---
_Source: https://npm.io/package/react-native-maps-use-cluster · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
