Skip to content

Commit 57310b5

Browse files
committed
Add Marker's Label
Add an option to display a marker label.
1 parent dc6a3bd commit 57310b5

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.flowingcode.vaadin.addons</groupId>
66
<artifactId>google-maps</artifactId>
7-
<version>2.2.4-SNAPSHOT</version>
7+
<version>2.2.5-SNAPSHOT</version>
88
<name>Google Maps Addon</name>
99
<description>Integration of google-map for Vaadin platform</description>
1010

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

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

199+
/**
200+
* Sets the label of the marker
201+
* In order to set the label's position use MarkerIcon::setLabelOrigin property.
202+
* @param label the new marker's label.
203+
*/
204+
public void setLabel(MarkerLabel label) {
205+
this.getElement().setPropertyJson("label", label.getJson());
206+
}
207+
199208
/**
200209
* Checks if marker animation is enabled.
201210
*
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.flowingcode.vaadin.addons.googlemaps;
2+
3+
import elemental.json.Json;
4+
import elemental.json.JsonObject;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
import java.util.Optional;
9+
10+
/**
11+
* Class representing a marker label.
12+
*/
13+
@Getter
14+
@Setter
15+
public class MarkerLabel {
16+
/**
17+
* The text to be displayed in the label.
18+
*/
19+
private String text;
20+
21+
/**
22+
* Optional. The color of the label text.
23+
* Defaults to: <code>'black'</code>
24+
*/
25+
private String color;
26+
27+
/**
28+
* Optional. The font family of the label text (equivalent to the CSS font-family
29+
* property).
30+
*/
31+
private String fontFamily;
32+
33+
/**
34+
* Optional. The font size of the label text (equivalent to the CSS font-size
35+
* property).
36+
* Defaults to: <code>'14px'</code>
37+
*/
38+
private String fontSize;
39+
40+
/**
41+
* Optional. The font weight of the label text (equivalent to the CSS font-weight
42+
* property).
43+
*/
44+
private String fontWeight;
45+
46+
/**
47+
The className property of the label's element (equivalent to the
48+
element's class attribute). Multiple space-separated CSS classes can
49+
be added. The font color, size, weight, and family can only be set via
50+
the other properties of <code>MarkerLabel</code>. CSS classes should not
51+
be used to change the position nor orientation of the label (e.g. using
52+
translations and rotations) if also using <a
53+
href="https://developers.google.com/maps/documentation/javascript/examples/marker-collision-management">marker
54+
collision management</a>. */
55+
private String className;
56+
57+
58+
public MarkerLabel(String text) {
59+
this.text = text;
60+
}
61+
62+
public MarkerLabel(String text, String color, String fontSize) {
63+
this.text = text;
64+
this.color = color;
65+
this.fontSize = fontSize;
66+
}
67+
68+
protected JsonObject getJson() {
69+
JsonObject js = Json.createObject();
70+
Optional.ofNullable(getText()).ifPresent(value -> js.put("text", value));
71+
Optional.ofNullable(getColor()).ifPresent(value -> js.put("color", value));
72+
Optional.ofNullable(getFontFamily()).ifPresent(value -> js.put("fontFamily", value));
73+
Optional.ofNullable(getFontSize()).ifPresent(value -> js.put("fontSize", value));
74+
Optional.ofNullable(getFontWeight()).ifPresent(value -> js.put("fontWeight", value));
75+
Optional.ofNullable(getClassName()).ifPresent(value -> js.put("className", value));
76+
return js;
77+
}
78+
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -45,9 +45,18 @@ protected void createGoogleMapsDemo(String apiKey) {
4545

4646
// marker using a MarkerIcon, url icon image + customization
4747
MarkerIcon markerIcon = new MarkerIcon(Markers.GREEN);
48-
markerIcon.setAnchor(new GoogleMapPoint(15.0, 10.0));
49-
gmaps.addMarker(new GoogleMapMarker("Marker with MarkerIcon defining url and anchor points",
50-
new LatLon(-31.636027, -60.703253), false, markerIcon));
48+
markerIcon.setAnchor(new GoogleMapPoint(0.0, 0.0));
49+
markerIcon.setLabelOrigin(new GoogleMapPoint(0.0, 10.0));
50+
51+
MarkerLabel label = new MarkerLabel("Flowing Code");
52+
label.setColor("blue");
53+
label.setFontSize("16px");
54+
label.setFontWeight("bold");
55+
56+
GoogleMapMarker marker = new GoogleMapMarker("Marker with MarkerIcon defining url and anchor points",
57+
new LatLon(-31.636027, -60.703253), false, markerIcon);
58+
marker.setLabel(label);
59+
gmaps.addMarker(marker);
5160

5261
// marker using a Symbol, svg path for the icon's image + customization
5362
Symbol symbol = new Symbol(
@@ -63,5 +72,5 @@ protected void createGoogleMapsDemo(String apiKey) {
6372

6473
add(gmaps);
6574
}
66-
75+
6776
}

0 commit comments

Comments
 (0)