1.7.8 • Published 3 years ago

react-native-map-cluster v1.7.8

Weekly downloads
17
License
MIT
Repository
github
Last release
3 years ago

React Native Map Cluster

Create clustered map views in React Native with current user location, custom marker rendering, custom cluster rendering and custom callout rendering.

Install

npm i --save react-native-map-cluster

Usage

NOTES:

  • the prop key of the markers rendered through renderMarker should not be left up to React. Instead, we strongly suggest to use an id in order the have unique keys while still taking advantage of React's recycling
  • ClusteredMapView supports usual React children. Those children won't be affected by clustering, i.e. the behavior for those children is exactly the same as wrapping them around an AirBnB's react-native-maps instance
  • Use onMarkerPress event on MapView instead of using onPress directly on Markers whenever possibile, in particular if you have a lot of pins and clusters. Within onMarkerPress you have access to the marker identifier through the event.nativeEvent attribute, hence you should be able to do everything you would do within an onPress function of a Marker
import React, { Component } from 'react'
import { Marker, Callout } from 'react-native-maps'
import ClusteredMapView from 'react-native-map-cluster'

const INIT_REGION = {
  latitude: 41.8962667,
  longitude: 11.3340056,
  latitudeDelta: 12,
  longitudeDelta: 12
}

export default class MyClusteredMapView extends Component {

  renderCluster = (cluster, onPress) => {
    const pointCount = cluster.pointCount,
          coordinate = cluster.coordinate,
          clusterId = cluster.clusterId

    const clusteringEngine = this.map.getClusteringEngine(),
          clusteredPoints = clusteringEngine.getLeaves(clusterId, 100)

    return (
      <Marker coordinate={coordinate} onPress={onPress}>
        <View style={styles.myClusterStyle}>
          <Text style={styles.myClusterTextStyle}>
            {pointCount}
          </Text>
        </View>
        {
          /*
            Eventually use <Callout /> to
            show clustered point thumbs, i.e.:
            <Callout>
              <ScrollView>
                {
                  clusteredPoints.map(p => (
                    <Image source={p.image}>
                  ))
                }
              </ScrollView>
            </Callout>
           */
        }
      </Marker>
    )
  }

  renderMarker = (data) => <Marker key={data.id || Math.random()} coordinate={data.location} />

  render() {
    return (
      <ClusteredMapView
        style={{flex: 1}}
        data={this.state.data}
        initialRegion={INIT_REGION}
        ref={(r) => { this.map = r }}
        renderMarker={this.renderMarker}
        renderCluster={this.renderCluster} />
    )
  }
}

Props

NameTypeRequiredDefaultNote
showsUserLocationBoolfalseundefinedShow current location on map.
permissionsAndroidObjectfalseundefinedNeeded when showsUserLocation is set to true. See example here.
radiusNumberfalsewindow width * 4,5%SuperCluster radius.
extentNumberfalse512SuperCluster extent.
minZoomNumberfalse1SuperCluster minZoom.
maxZoomNumberfalse16SuperCluster maxZoom.
widthNumberfalsewindow widthmap's width.
heightNumberfalsewindow heightmap's height.
dataArray trueundefinedObjects must have an attribute location representing a GeoPoint, i.e. { latitude: x, longitude: y }.
onExplodeFunctionfalseundefinedTODO
onImplodeFunctionfalseundefinedTODO
onClusterPress(clusterId, ?children)FunctionfalseAdd (or completey override) behaviours to the clusterPress handler. children is passed when default behavior is preserved (see preserveClusterPressBehavior prop).
preserveClusterPressBehaviorBoolfalsetrueWhether onClusterPress prop should completely override module's behavior rather than integrate it.
clusterPressMaxChildrenFunctionfalse100Max number of cluster leaves returned as second parameter of onClusterPress.
edgePaddingObjectfalse{ top: 10, left: 10, bottom: 10, right: 10 }Edge padding for react-native-maps's fitToCoordinates method, called in onClusterPress for fitting to pressed cluster children.
renderMarkerFunctiontrueundefinedMust return a react-native-maps' Marker component.
renderClusterFunctiontrueundefinedRender the cluster.
animateClustersBoolfalsetrueAnimate imploding/exploding of clusters' markers and clusters size change. Works only on iOS.
layoutAnimationConfLayoutAnimationConfigfalseLayoutAnimation.Presets.springCustom Layout animation configuration object for clusters animation during implode / explode Works only on iOS.
clusteringEnabledBoolfalsetrueDynamically set whether to pass through clustering functions or immediately render markers as a normal mapview.
accessorString|Funcfalse"location"Accessor for item coordinate values. Could be a string (field name of an item object with latitude and longitude values) or a function (that describes how to access to coordinate data).

Methods

NameParamsDescriptionNote
getMapRefnoneGetter for underlying react-native-maps instanceOfficial Doc
getClusteringEnginenoneGetter for underlying SuperCluster instanceOfficial Doc

Credit

This repository is a fork of react-native-maps-super-cluster with many more features and updated implementations.