Skip to content

Commit 312ccdc

Browse files
AlexRoigjavier-godoy
authored andcommitted
feat: add marker's label
Add an option to display a marker label.
1 parent dc6a3bd commit 312ccdc

File tree

3 files changed

+135
-3
lines changed

3 files changed

+135
-3
lines changed

src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMapMarker.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ public void setIcon(GoogleMapIcon icon) {
196196
this.getElement().setPropertyJson("icon", icon.getJson());
197197
}
198198

199+
/**
200+
* Sets the label of the marker In order to set the label's position use
201+
* MarkerIcon::setLabelOrigin property.
202+
*
203+
* @param label the new marker's label
204+
*/
205+
public void setLabel(MarkerLabel label) {
206+
this.getElement().setPropertyJson("label", label.getJson());
207+
}
208+
199209
/**
200210
* Checks if marker animation is enabled.
201211
*
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 - 2025 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 elemental.json.Json;
23+
import elemental.json.JsonObject;
24+
import lombok.Getter;
25+
import lombok.NonNull;
26+
import lombok.Setter;
27+
28+
import java.util.Objects;
29+
import java.util.Optional;
30+
31+
/**
32+
* Class representing a marker label.
33+
*
34+
* @see <a href=
35+
* "https://developers.google.com/maps/documentation/javascript/reference/marker#MarkerLabel">Google
36+
* Maps MarkerLabel</a>
37+
*/
38+
@Getter
39+
@Setter
40+
public class MarkerLabel {
41+
/**
42+
* The text to be displayed in the label. Required field.
43+
*/
44+
@NonNull
45+
private String text;
46+
47+
/**
48+
* Optional. The color of the label text. Defaults to: <code>'black'</code>.
49+
*/
50+
private String color;
51+
52+
/**
53+
* Optional. The font family of the label text (equivalent to the CSS font-family property).
54+
*/
55+
private String fontFamily;
56+
57+
/**
58+
* Optional. The font size of the label text (equivalent to the CSS font-size property). Defaults
59+
* to: <code>'14px'</code>.
60+
*/
61+
private String fontSize;
62+
63+
/**
64+
* Optional. The font weight of the label text (equivalent to the CSS font-weight property).
65+
*/
66+
private String fontWeight;
67+
68+
/**
69+
* The className property of the label's element (equivalent to the element's class attribute).
70+
* Multiple space-separated CSS classes can be added. The font color, size, weight, and family can
71+
* only be set via the other properties of <code>MarkerLabel</code>.
72+
*/
73+
private String className;
74+
75+
public MarkerLabel(String text) {
76+
this.text = Objects.requireNonNull(text, "Text cannot be null");
77+
}
78+
79+
public MarkerLabel(String text, String color, String fontSize) {
80+
this.text = Objects.requireNonNull(text, "Text cannot be null");
81+
this.color = color;
82+
this.fontSize = fontSize;
83+
}
84+
85+
protected JsonObject getJson() {
86+
JsonObject js = Json.createObject();
87+
Optional.of(getText()).ifPresent(value -> js.put("text", value));
88+
Optional.ofNullable(getColor()).ifPresent(value -> js.put("color", value));
89+
Optional.ofNullable(getFontFamily()).ifPresent(value -> js.put("fontFamily", value));
90+
Optional.ofNullable(getFontSize()).ifPresent(value -> js.put("fontSize", value));
91+
Optional.ofNullable(getFontWeight()).ifPresent(value -> js.put("fontWeight", value));
92+
Optional.ofNullable(getClassName()).ifPresent(value -> js.put("className", value));
93+
return js;
94+
}
95+
}

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Google Maps Addon
44
* %%
5-
* Copyright (C) 2020 - 2024 Flowing Code
5+
* Copyright (C) 2020 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import com.vaadin.flow.component.notification.Notification;
3434
import com.vaadin.flow.component.orderedlayout.FlexLayout;
3535
import com.vaadin.flow.component.orderedlayout.FlexLayout.FlexWrap;
36+
import com.vaadin.flow.component.textfield.TextField;
3637
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
3738
import com.vaadin.flow.router.PageTitle;
3839
import com.vaadin.flow.router.Route;
@@ -69,7 +70,16 @@ protected void createGoogleMapsDemo(String apiKey) {
6970

7071
Checkbox draggable = new Checkbox("Draggable");
7172
Checkbox withRightClick = new Checkbox("Right Click");
72-
73+
TextField labelText = new TextField();
74+
labelText.setPlaceholder("Label Text"); // hide-source
75+
labelText.setEnabled(false); // hide-source
76+
Checkbox withLabel = new Checkbox("With Label");
77+
// #if vaadin eq 0
78+
withLabel.addValueChangeListener(event -> {
79+
labelText.setEnabled(event.getValue());
80+
});
81+
// #endif
82+
7383
Button addMarker =
7484
new Button(
7585
"Add Marker",
@@ -108,14 +118,31 @@ protected void createGoogleMapsDemo(String apiKey) {
108118
notification.open();
109119
});
110120
}
121+
122+
if(withLabel.getValue()) {
123+
MarkerLabel label = new MarkerLabel(labelText.getValue());
124+
label.setColor("white");
125+
label.setFontWeight("bold");
126+
marker.setLabel(label);
127+
}
128+
111129
gmaps.addMarker(marker);
130+
// #if vaadin eq 0
131+
// Reset form
132+
colorCB.clear();
133+
draggable.setValue(false);
134+
withRightClick.setValue(false);
135+
labelText.clear();
136+
withLabel.setValue(false);
137+
// #endif
112138
});
113139

114140
FlexLayout layout = new FlexLayout();
115141
layout.setFlexWrap(FlexWrap.WRAP); // hide-source
116142
addMarker.addClassName("margin-button"); // hide-source
117143
colorCB.addClassName("margin-button"); // hide-source
118-
layout.add(colorCB, draggable, withRightClick, addMarker);
144+
labelText.addClassName("margin-button"); // hide-source
145+
layout.add(colorCB, draggable, withRightClick, withLabel, labelText, addMarker);
119146
layout.setAlignItems(Alignment.BASELINE); // hide-source
120147
layout.getStyle().set("margin-top", "0"); // hide-source
121148
add(gmaps, layout);

0 commit comments

Comments
 (0)