-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathrenderer.ts
168 lines (154 loc) · 5.13 KB
/
renderer.ts
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
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Cluster } from "./cluster";
import { Marker, MarkerUtils } from "./marker-utils";
/**
* Provides statistics on all clusters in the current render cycle for use in {@link Renderer.render}.
*/
export class ClusterStats {
public readonly markers: { sum: number };
public readonly clusters: {
count: number;
markers: {
mean: number;
sum: number;
min: number;
max: number;
};
};
constructor(markers: Marker[], clusters: Cluster[]) {
this.markers = { sum: markers.length };
const clusterMarkerCounts = clusters.map((a) => a.count);
const clusterMarkerSum = clusterMarkerCounts.reduce((a, b) => a + b, 0);
this.clusters = {
count: clusters.length,
markers: {
mean: clusterMarkerSum / clusters.length,
sum: clusterMarkerSum,
min: Math.min(...clusterMarkerCounts),
max: Math.max(...clusterMarkerCounts),
},
};
}
}
export interface Renderer {
/**
* Turn a {@link Cluster} into a `Marker`.
*
* Below is a simple example to create a marker with the number of markers in the cluster as a label.
*
* ```typescript
* return new google.maps.Marker({
* position,
* label: String(markers.length),
* });
* ```
*/
render(cluster: Cluster, stats: ClusterStats, map: google.maps.Map): Marker;
}
export class DefaultRenderer implements Renderer {
/**
* The default render function for the library used by {@link MarkerClusterer}.
*
* Currently set to use the following:
*
* ```typescript
* // change color if this cluster has more markers than the mean cluster
* const color =
* count > Math.max(10, stats.clusters.markers.mean)
* ? this.overMeanColor // "#ff0000"
* : this.underMeanColor; // "#ff0000"
*
* // create svg url with fill color
* const svg = window.btoa(`
* <svg fill="${color}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 240">
* <circle cx="120" cy="120" opacity=".6" r="70" />
* <circle cx="120" cy="120" opacity=".3" r="90" />
* <circle cx="120" cy="120" opacity=".2" r="110" />
* <circle cx="120" cy="120" opacity=".1" r="130" />
* </svg>`);
*
* // create marker using svg icon
* return new google.maps.Marker({
* position,
* icon: {
* url: `data:image/svg+xml;base64,${svg}`,
* scaledSize: new google.maps.Size(45, 45),
* },
* label: {
* text: String(count),
* color: "rgba(255,255,255,0.9)",
* fontSize: "12px",
* },
* // adjust zIndex to be above other markers
* zIndex: 1000 + count,
* });
* ```
*/
underMeanColor: string
overMeanColor: string
constructor(underMeanColor = "#0000ff", overMeanColor = "#ff0000") {
super()
this.underMeanColor = underMeanColor
this.overMeanColor = overMeanColor
}
public render(
{ count, position }: Cluster,
stats: ClusterStats,
map: google.maps.Map
): Marker {
// change color if this cluster has more markers than the mean cluster
const color =
count > Math.max(10, stats.clusters.markers.mean) ? this.overMeanColor : this.underMeanColor;
// create svg literal with fill color
const svg = `<svg fill="${color}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 240" width="50" height="50">
<circle cx="120" cy="120" opacity=".6" r="70" />
<circle cx="120" cy="120" opacity=".3" r="90" />
<circle cx="120" cy="120" opacity=".2" r="110" />
<text x="50%" y="50%" style="fill:#fff" text-anchor="middle" font-size="50" dominant-baseline="middle" font-family="roboto,arial,sans-serif">${count}</text>
</svg>`;
const title = `Cluster of ${count} markers`,
// adjust zIndex to be above other markers
zIndex: number = Number(google.maps.Marker.MAX_ZINDEX) + count;
if (MarkerUtils.isAdvancedMarkerAvailable(map)) {
// create cluster SVG element
const parser = new DOMParser();
const svgEl = parser.parseFromString(
svg,
"image/svg+xml"
).documentElement;
svgEl.setAttribute("transform", "translate(0 25)");
const clusterOptions: google.maps.marker.AdvancedMarkerElementOptions = {
map,
position,
zIndex,
title,
content: svgEl,
};
return new google.maps.marker.AdvancedMarkerElement(clusterOptions);
}
const clusterOptions: google.maps.MarkerOptions = {
position,
zIndex,
title,
icon: {
url: `data:image/svg+xml;base64,${btoa(svg)}`,
anchor: new google.maps.Point(25, 25),
},
};
return new google.maps.Marker(clusterOptions);
}
}