forked from novalabio/react-native-maps-super-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClusteredMapView.js
205 lines (175 loc) · 6.19 KB
/
ClusteredMapView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use-strict'
// base libs
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'
import {
Platform,
Dimensions,
LayoutAnimation
} from 'react-native'
// map-related libs
import MapView from 'react-native-maps'
import SuperCluster from 'supercluster'
import GeoViewport from '@mapbox/geo-viewport'
// components / views
import ClusterMarker from './ClusterMarker'
// libs / utils
import {
regionToBoundingBox,
itemToGeoJSONFeature
} from './util'
export default class ClusteredMapView extends PureComponent {
constructor(props) {
super(props)
this.state = {
data: [], // helds renderable clusters and markers
region: {}, // helds current map region
}
this.mapRef = this.mapRef.bind(this)
this.onClusterPress = this.onClusterPress.bind(this)
this.onRegionChangeComplete = this.onRegionChangeComplete.bind(this)
}
componentWillMount() {
this.dimensions = [this.props.width, this.props.height]
this.isAndroid = Platform.OS === 'android'
this.setState({ region: this.props.region || this.props.initialRegion })
}
componentDidMount() {
this.clusterize(this.props.data)
}
componentWillReceiveProps(nextProps) {
if (this.props.data !== nextProps.data)
this.clusterize(nextProps.data)
}
componentWillUpdate(nextProps, nextState) {
!this.isAndroid && this.props.animateClusters
&& this.clustersChanged(nextState)
&& LayoutAnimation.configureNext(LayoutAnimation.Presets.spring)
}
mapRef = (ref) => {
this.mapview = ref
}
getMapRef = () => this.mapview
getClusteringEngine = () => this.index
clusterize = (dataset) => {
this.index = SuperCluster({ // eslint-disable-line new-cap
extent: this.props.extent,
minZoom: this.props.minZoom,
maxZoom: this.props.maxZoom,
radius: this.props.radius || (this.dimensions[0] * .045), // 4.5% of screen width
})
// get formatted GeoPoints for cluster
const rawData = dataset.map(itemToGeoJSONFeature)
// load geopoints into SuperCluster
this.index.load(rawData)
const data = this.getClusters(this.state.region)
this.setState({ data })
}
clustersChanged = (nextState) => this.state.data.length !== nextState.data.length
onRegionChangeComplete = (region) => {
if (region.longitudeDelta <= 80) {
const data = this.getClusters(region)
this.setState({ region, data })
}
this.props.onRegionChangeComplete && this.props.onRegionChangeComplete(region)
}
getClusters = (region) => {
const bbox = regionToBoundingBox(region),
viewport = (region.longitudeDelta) >= 40 ? { zoom: this.props.minZoom } : GeoViewport.viewport(bbox, this.dimensions)
return this.index.getClusters(bbox, viewport.zoom)
}
onClusterPress = (cluster) => {
// cluster press behavior might be extremely custom.
if (!this.props.preserveClusterPressBehavior) {
this.props.onClusterPress && this.props.onClusterPress(cluster.properties.cluster_id)
return
}
// //////////////////////////////////////////////////////////////////////////////////
// NEW IMPLEMENTATION (with fitToCoordinates)
// //////////////////////////////////////////////////////////////////////////////////
// get cluster children
const children = this.index.getLeaves(cluster.properties.cluster_id, this.props.clusterPressMaxChildren),
markers = children.map(c => c.properties.item)
// fit right around them, considering edge padding
this.mapview.fitToCoordinates(markers.map(m => m.location), { edgePadding: this.props.edgePadding })
this.props.onClusterPress && this.props.onClusterPress(cluster.properties.cluster_id, markers)
}
render() {
return (
<MapView
{ ...this.props}
ref={this.mapRef}
onRegionChangeComplete={this.onRegionChangeComplete}>
{
this.props.clusteringEnabled && this.state.data.map((d) => {
if (d.properties.point_count === 0)
return this.props.renderMarker(d.properties.item)
return (
<ClusterMarker
{...d}
onPress={this.onClusterPress}
textStyle={this.props.textStyle}
scaleUpRatio={this.props.scaleUpRatio}
renderCluster={this.props.renderCluster}
key={`cluster-${d.properties.cluster_id}`}
containerStyle={this.props.containerStyle}
clusterInitialFontSize={this.props.clusterInitialFontSize}
clusterInitialDimension={this.props.clusterInitialDimension} />
)
})
}
{
!this.props.clusteringEnabled && this.props.data.map(d => this.props.renderMarker(d))
}
{this.props.children}
</MapView>
)
}
}
ClusteredMapView.defaultProps = {
minZoom: 1,
maxZoom: 20,
extent: 512,
textStyle: {},
containerStyle: {},
animateClusters: true,
clusteringEnabled: true,
clusterInitialFontSize: 12,
clusterInitialDimension: 30,
clusterPressMaxChildren: 100,
preserveClusterPressBehavior: true,
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
edgePadding: { top: 10, left: 10, right: 10, bottom: 10 }
}
ClusteredMapView.propTypes = {
...MapView.propTypes,
// number
radius: PropTypes.number,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
extent: PropTypes.number.isRequired,
minZoom: PropTypes.number.isRequired,
maxZoom: PropTypes.number.isRequired,
clusterInitialFontSize: PropTypes.number.isRequired,
clusterPressMaxChildren: PropTypes.number.isRequired,
clusterInitialDimension: PropTypes.number.isRequired,
// array
data: PropTypes.array.isRequired,
// func
onExplode: PropTypes.func,
onImplode: PropTypes.func,
scaleUpRatio: PropTypes.func,
renderCluster: PropTypes.func,
onClusterPress: PropTypes.func,
renderMarker: PropTypes.func.isRequired,
// bool
animateClusters: PropTypes.bool.isRequired,
clusteringEnabled: PropTypes.bool.isRequired,
preserveClusterPressBehavior: PropTypes.bool.isRequired,
// object
textStyle: PropTypes.object,
edgePadding: PropTypes.object.isRequired,
containerStyle: PropTypes.object,
// string
}