Skip to content

Commit 712be9a

Browse files
paodbjavier-godoy
authored andcommitted
feat(demo): add example demo for custom renderer
1 parent d1d5af3 commit 712be9a

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*-
2+
* #%L
3+
* Google Maps Addon
4+
* %%
5+
* Copyright (C) 2020 - 2023 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.googlemaps;
21+
22+
import com.flowingcode.vaadin.addons.demo.DemoSource;
23+
import com.flowingcode.vaadin.addons.googlemaps.GoogleMap.MapType;
24+
import com.vaadin.flow.component.dependency.JsModule;
25+
import com.vaadin.flow.router.PageTitle;
26+
import com.vaadin.flow.router.Route;
27+
28+
@PageTitle("Clustering With Custom Renderer Demo")
29+
@DemoSource
30+
@Route(value = "googlemaps/clustering-custom-renderer", layout = GooglemapsDemoView.class)
31+
@JsModule("./src/clustering-custom-renderer-example.js")
32+
@SuppressWarnings("serial")
33+
public class ClusteringWithCustomRendererDemo extends AbstractGoogleMapsDemo {
34+
35+
@Override
36+
protected void createGoogleMapsDemo(String apiKey) {
37+
GoogleMap gmaps = new GoogleMap(apiKey, null, null);
38+
gmaps.setMapType(MapType.ROADMAP);
39+
gmaps.setSizeFull();
40+
gmaps.setZoom(5);
41+
gmaps.setCenter(new LatLon(-31.636036, -60.7055271));
42+
43+
gmaps.addMarker("Marker 1", new LatLon(-31.646036, -58.7055290), true, Markers.PURPLE);
44+
gmaps.addMarker("Marker 2", new LatLon(-31.562346, -58.6176364), true, Markers.PURPLE);
45+
gmaps.addMarker("Marker 3", new LatLon(-31.531917, -58.8456027), true, Markers.PURPLE);
46+
gmaps.addMarker("Marker 4", new LatLon(-31.651667, -58.9555557), true, Markers.PURPLE);
47+
48+
gmaps.addMarker("Marker 5", new LatLon(-30.997815, -68.60542944), false, Markers.BLUE);
49+
gmaps.addMarker("Marker 6", new LatLon(-31.298693, -68.50630836), false, Markers.BLUE);
50+
gmaps.addMarker("Marker 7", new LatLon(-31.878914, -68.28658178), false, Markers.BLUE);
51+
52+
gmaps.addMarker("Marker 8", new LatLon(-35.138889, -65.95699194), false, Markers.GREEN);
53+
gmaps.addMarker("Marker 9", new LatLon(-35.322683, -65.12203100), false, Markers.GREEN);
54+
gmaps.addMarker("Marker 10", new LatLon(-35.652538, -65.737265381), false, Markers.GREEN);
55+
gmaps.addMarker("Marker 11", new LatLon(-35.512548, -65.565465771), false, Markers.GREEN);
56+
gmaps.addMarker("Marker 12", new LatLon(-35.650638, -65.707366381), false, Markers.YELLOW);
57+
gmaps.addMarker("Marker 13", new LatLon(-35.550638, -65.703360000), false, Markers.YELLOW);
58+
59+
// enable clustering
60+
gmaps.enableMarkersClustering();
61+
62+
/*
63+
* set custom renderer for clustering (see
64+
* /google-maps/src/test/resources/META-INF/resources/frontend/src/clustering-custom-renderer-
65+
* example.js for definition)
66+
*/
67+
gmaps.setClusteringRenderer("customRenderer");
68+
69+
add(gmaps);
70+
}
71+
72+
}

src/test/java/com/flowingcode/vaadin/addons/googlemaps/GooglemapsDemoView.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public GooglemapsDemoView() {
4545
addDemo(MarkerClusteringDemo.class);
4646
addDemo(TiltAndRotationDemo.class);
4747
addDemo(RightClickOnMarkersDemo.class);
48+
addDemo(ClusteringWithCustomRendererDemo.class);
4849
setSizeFull();
4950
}
5051
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*-
2+
* #%L
3+
* Google Maps Addon
4+
* %%
5+
* Copyright (C) 2020 - 2023 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
const markerscolors = new Map([
21+
["green", "#00e64d"],
22+
["yellow", "#fdf569"],
23+
["purple", "#8e67fd"],
24+
["blue", "#6991fd"]
25+
]);
26+
27+
window.customRenderer = {
28+
29+
/*
30+
* Function that contains the custom renderer implementation.
31+
* The function name needs to be "render".
32+
*/
33+
render: function(cluster) {
34+
35+
// get markers count within cluster
36+
const count = cluster.count;
37+
38+
// get color to applied to cluster
39+
const color = this._getClusterColor(cluster);
40+
41+
// create svg url with fill color
42+
const svg = window.btoa(`
43+
<svg fill="${color}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 240">
44+
<circle cx="120" cy="120" opacity=".9" r="70" />
45+
</svg>`);
46+
47+
// get cluster position
48+
const position = cluster.position;
49+
50+
// create marker for cluster
51+
return new google.maps.Marker({
52+
position,
53+
// set icon using defined svg and set size
54+
icon: {
55+
url: `data:image/svg+xml;base64,${svg}`,
56+
scaledSize: new google.maps.Size(75, 75),
57+
},
58+
// set text, color, and font-size for cluster label
59+
label: {
60+
text: String(count),
61+
color: "rgba(255,255,255,0.9)",
62+
fontSize: "14px",
63+
},
64+
// adjust zIndex to be above other markers
65+
zIndex: 1000 + count,
66+
});
67+
},
68+
69+
/*
70+
* Returns the color to be applied to thecluster.
71+
* The color is derived from the color of the markers that are part of the cluster.
72+
* The color with more occurrences within the cluster will be the one to be applied.
73+
*/
74+
_getClusterColor(cluster) {
75+
let markersIconsColors = [];
76+
cluster.markers.forEach(m => {
77+
const markerIcon = m.__data.icon;
78+
const startIdx = markerIcon.lastIndexOf('/');
79+
const endIdx = markerIcon.indexOf('.png');
80+
const markerColor = markerIcon.substring(startIdx + 1, endIdx);
81+
markersIconsColors.push(markerColor);
82+
});
83+
84+
const maxColor = markersIconsColors.reduce((previous, current, _, arr) => {
85+
if (arr.filter((item) => item === previous).length >
86+
arr.filter((item) => item === current).length) {
87+
return previous;
88+
} else {
89+
return current;
90+
}
91+
});
92+
93+
return markerscolors.get(maxColor);
94+
}
95+
}

0 commit comments

Comments
 (0)