react-native-maps-use-cluster

1.0.4 • Public • Published

react-native-maps-use-cluster

npm version

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

Completely inspired by react-native-maps-super-cluster, most of credit goes back to their maintainers and contributors.

Installation

yarn add react-native-maps-use-cluster

Usage

Here is the quick how-to example:

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<
    { markerIMarker } & 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.

Package Sidebar

Install

npm i react-native-maps-use-cluster

Weekly Downloads

6

Version

1.0.4

License

MIT

Unpacked Size

8.29 kB

Total Files

4

Last publish

Collaborators

  • loicmahieu