forked from novalabio/react-native-maps-super-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusterMarker.js
More file actions
135 lines (117 loc) · 3.71 KB
/
ClusterMarker.js
File metadata and controls
135 lines (117 loc) · 3.71 KB
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
"use-strict";
// base libs
import PropTypes from "prop-types";
import React, { Component } from "react";
import { MapView } from "expo";
import { Text, View, StyleSheet } from "react-native";
const Marker = MapView.Marker;
export default class ClusterMarker extends Component {
constructor(props) {
super(props);
this.onPress = this.onPress.bind(this);
}
onPress() {
this.props.onPress(this.props);
}
render() {
const pointCount = this.props.properties.point_count; // eslint-disable-line camelcase
const latitude = this.props.geometry.coordinates[1],
longitude = this.props.geometry.coordinates[0];
if (this.props.renderCluster) {
const cluster = {
pointCount,
coordinate: { latitude, longitude },
clusterId: this.props.properties.cluster_id
};
return this.props.renderCluster(cluster, this.onPress);
}
// keep default marker for backward compatibility, but it'll soon be deprecated
console.warn(
'Deprecation notice: default markers will soon be deprecated. Please, start using "renderCluster" prop.'
); // eslint-disable-line
let scaleUpRatio = this.props.scaleUpRatio
? this.props.scaleUpRatio(pointCount)
: 1 + Math.min(pointCount, 999) / 100;
if (isNaN(scaleUpRatio)) {
console.warn(
"scaleUpRatio must return a Number, falling back to default"
); // eslint-disable-line
scaleUpRatio = 1 + Math.min(pointCount, 999) / 100;
}
let textForCluster = "1";
let width = Math.floor(this.props.clusterInitialDimension * scaleUpRatio),
height = Math.floor(this.props.clusterInitialDimension * scaleUpRatio),
fontSize = Math.floor(this.props.clusterInitialFontSize * scaleUpRatio),
borderRadius = Math.floor(width / 2);
// cluster dimension upper limit
width =
width <= this.props.clusterInitialDimension * 2
? width
: this.props.clusterInitialDimension * 2;
height =
height <= this.props.clusterInitialDimension * 2
? height
: this.props.clusterInitialDimension * 2;
fontSize = fontSize <= 18 ? fontSize : 18;
if (pointCount >= 2 && pointCount <= 10) {
textForCluster = pointCount.toString();
}
if (pointCount > 10 && pointCount <= 25) {
textForCluster = "10+";
}
if (pointCount > 25 && pointCount <= 50) {
textForCluster = "25+";
}
if (pointCount > 50 && pointCount <= 100) {
textForCluster = "50+";
}
if (pointCount > 100) {
textForCluster = "100+";
}
const { containerStyle, textStyle } = this.props;
return (
<Marker coordinate={{ latitude, longitude }} onPress={this.onPress}>
<View
style={[
styles.container,
containerStyle,
{ width, height, borderRadius }
]}
>
<Text style={[styles.counterText, textStyle, { fontSize }]}>
{textForCluster}
</Text>
</View>
</Marker>
);
}
}
ClusterMarker.defaultProps = {
textStyle: {},
containerStyle: {}
};
ClusterMarker.propTypes = {
scaleUpRatio: PropTypes.func,
renderCluster: PropTypes.func,
onPress: PropTypes.func.isRequired,
geometry: PropTypes.object.isRequired,
textStyle: PropTypes.object.isRequired,
properties: PropTypes.object.isRequired,
containerStyle: PropTypes.object.isRequired,
clusterInitialFontSize: PropTypes.number.isRequired,
clusterInitialDimension: PropTypes.number.isRequired
};
const styles = StyleSheet.create({
container: {
borderWidth: 1,
alignItems: "center",
borderColor: "#65bc46",
justifyContent: "center",
backgroundColor: "#fff"
},
counterText: {
fontSize: 16,
color: "#65bc46",
fontWeight: "400"
}
});