Skip to content

Commit 5cdb14f

Browse files
paodbjavier-godoy
authored andcommitted
feat: update API to allow setting properties to icons
Close #113
1 parent 6fa0978 commit 5cdb14f

File tree

13 files changed

+568
-53
lines changed

13 files changed

+568
-53
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public int getZoom() {
139139
* @param position Coordinates of the marker on the map.
140140
* @param draggable Set true to enable dragging of the marker.
141141
* @param iconUrl The url of the icon of the marker.
142+
*
142143
* @return GoogleMapMarker object created with the given settings.
143144
*/
144145
public GoogleMapMarker addMarker(
@@ -147,6 +148,23 @@ public GoogleMapMarker addMarker(
147148
addMarker(gmm);
148149
return gmm;
149150
}
151+
152+
/**
153+
* Adds a new marker to the map.
154+
*
155+
* @param caption Caption of the marker shown when the marker is hovered.
156+
* @param position Coordinates of the marker on the map.
157+
* @param draggable Set true to enable dragging of the marker.
158+
* @param icon the icon image of the marker.
159+
*
160+
* @return GoogleMapMarker object created with the given settings.
161+
*/
162+
public GoogleMapMarker addMarker(
163+
String caption, LatLon position, boolean draggable, GoogleMapIcon icon) {
164+
GoogleMapMarker gmm = new GoogleMapMarker(caption, position, draggable, icon);
165+
addMarker(gmm);
166+
return gmm;
167+
}
150168

151169
/**
152170
* Creates a polygon with the given points and adds the new polygon to the map.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*-
2+
* #%L
3+
* Google Maps Addon
4+
* %%
5+
* Copyright (C) 2020 - 2024 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+
21+
package com.flowingcode.vaadin.addons.googlemaps;
22+
23+
import elemental.json.JsonObject;
24+
25+
/**
26+
* Interface representing a google map icon ({@link MarkerIcon} or {@link Symbol}).
27+
*/
28+
public interface GoogleMapIcon {
29+
30+
JsonObject getJson();
31+
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,26 @@ public GoogleMapMarker(String caption, LatLon position, boolean draggable) {
7676
* @param caption The caption to use.
7777
* @param position The position of the marker
7878
* @param draggable Can marker be dragged?
79+
* @param iconUrl the icon url as a string
7980
*/
8081
public GoogleMapMarker(String caption, LatLon position, boolean draggable, String iconUrl) {
8182
this(caption, position, draggable);
8283
this.setIconUrl(iconUrl);
8384
}
84-
85+
86+
/**
87+
* Instantiates a new GoogleMapMarker
88+
*
89+
* @param caption The caption to use.
90+
* @param position The position of the marker
91+
* @param draggable Can marker be dragged?
92+
* @param icon the icon image for the marker
93+
*/
94+
public GoogleMapMarker(String caption, LatLon position, boolean draggable, GoogleMapIcon icon) {
95+
this(caption, position, draggable);
96+
this.setIcon(icon);
97+
}
98+
8599
public void addInfoWindow(String htmlContent) {
86100
this.getElement().setProperty("innerHTML", htmlContent);
87101
}
@@ -172,6 +186,15 @@ public String getIconUrl() {
172186
public void setIconUrl(String iconUrl) {
173187
this.getElement().setProperty("icon", iconUrl);
174188
}
189+
190+
/**
191+
* Sets the icon image of the marker
192+
*
193+
* @param icon the icon image of the marker
194+
*/
195+
public void setIcon(GoogleMapIcon icon) {
196+
this.getElement().setPropertyJson("icon", icon.getJson());
197+
}
175198

176199
/**
177200
* Checks if marker animation is enabled.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,29 @@ public Registration addClickListener(
181181
return addListener(GoogleMapPolyClickEvent.class, listener);
182182
}
183183

184+
/**
185+
* @deprecated, use {@link #setIcons(IconSequence...)} instead.
186+
*/
187+
@Deprecated
184188
public void setIcons(Icon... icons) {
185189
JsonArray jsonArray = Json.createArray();
186190
for (int i = 0; i < icons.length; i++) {
187191
jsonArray.set(i, icons[i].getJson());
188192
}
189193
this.getElement().setPropertyJson("icons", jsonArray);
190194
}
195+
196+
/**
197+
* Set icons to the polygon/polyline.
198+
*
199+
* @param icons the icons to set
200+
*/
201+
public void setIcons(IconSequence... icons) {
202+
JsonArray jsonArray = Json.createArray();
203+
for (int i = 0; i < icons.length; i++) {
204+
jsonArray.set(i, icons[i].getJson());
205+
}
206+
this.getElement().setPropertyJson("icons", jsonArray);
207+
}
208+
191209
}

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

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,22 @@
2424
import elemental.json.JsonObject;
2525
import java.util.Optional;
2626

27-
public class Icon {
27+
/**
28+
* @deprecated, use {@link IconSequence} instead.
29+
*/
30+
@Deprecated
31+
public class Icon extends Symbol {
2832

29-
private String path;
30-
private String strokeColor;
31-
private String fillColor;
32-
private int fillOpacity;
3333
private int repeat;
3434

3535
public Icon(String path, String strokeColor, String fillColor, int fillOpacity, int repeat) {
36-
super();
37-
this.path = path;
38-
this.strokeColor = strokeColor;
39-
this.fillColor = fillColor;
40-
this.fillOpacity = fillOpacity;
36+
super(path);
37+
this.setStrokeColor(strokeColor);
38+
this.setFillColor(fillColor);
39+
this.setFillOpacity(Double.valueOf(fillOpacity));
4140
this.repeat = repeat;
4241
}
4342

44-
public String getPath() {
45-
return path;
46-
}
47-
48-
public void setPath(String path) {
49-
this.path = path;
50-
}
51-
52-
public String getStrokeColor() {
53-
return strokeColor;
54-
}
55-
56-
public void setStrokeColor(String strokeColor) {
57-
this.strokeColor = strokeColor;
58-
}
59-
60-
public String getFillColor() {
61-
return fillColor;
62-
}
63-
64-
public void setFillColor(String fillColor) {
65-
this.fillColor = fillColor;
66-
}
67-
68-
public int getFillOpacity() {
69-
return fillOpacity;
70-
}
71-
72-
public void setFillOpacity(int fillOpacity) {
73-
this.fillOpacity = fillOpacity;
74-
}
75-
7643
public int getRepeat() {
7744
return repeat;
7845
}
@@ -81,16 +48,12 @@ public void setRepeat(int repeat) {
8148
this.repeat = repeat;
8249
}
8350

84-
protected JsonObject getJson() {
85-
JsonObject options = Json.createObject();
86-
Optional.ofNullable(getPath()).ifPresent(value -> options.put("path", value));
87-
Optional.ofNullable(getStrokeColor()).ifPresent(value -> options.put("strokeColor", value));
88-
Optional.ofNullable(getFillColor()).ifPresent(value -> options.put("fillColor", value));
89-
Optional.ofNullable(getFillOpacity()).ifPresent(value -> options.put("fillOpacity", value));
90-
51+
@Override
52+
public JsonObject getJson() {
9153
JsonObject js = Json.createObject();
92-
js.put("icon", options);
54+
JsonObject iconJs = super.getJson();
55+
js.put("icon", iconJs);
9356
Optional.ofNullable(getRepeat()).ifPresent(v -> js.put("repeat", v + "px"));
94-
return js;
57+
return js;
9558
}
9659
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*-
2+
* #%L
3+
* Google Maps Addon
4+
* %%
5+
* Copyright (C) 2020 - 2024 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+
21+
package com.flowingcode.vaadin.addons.googlemaps;
22+
23+
import elemental.json.Json;
24+
import elemental.json.JsonObject;
25+
import java.util.Optional;
26+
import lombok.Getter;
27+
import lombok.Setter;
28+
29+
/**
30+
* Describes how icons are to be rendered on a line (for polygons and polylines).
31+
*/
32+
@Getter
33+
@Setter
34+
public class IconSequence {
35+
36+
/**
37+
* The icon to render on the line.
38+
*/
39+
private Symbol symbol;
40+
41+
/**
42+
* The distance between consecutive icons on the line. This distance may be expressed as a
43+
* percentage or in pixels. To disable repeating of the icon, specify '0'.
44+
*/
45+
private String repeat;
46+
47+
/**
48+
* If true, each icon in the sequence has the same fixed rotation regardless of the angle of the
49+
* edge on which it lies. If false, case each icon in the sequence is rotated to align with its
50+
* edge. Default value: false.
51+
*/
52+
private boolean fixedRotation;
53+
54+
/**
55+
* The distance from the start of the line at which an icon is to be rendered. This distance may
56+
* be expressed as a percentage or in pixels.
57+
*/
58+
private String offset;
59+
60+
61+
public IconSequence(Symbol symbol) {
62+
this.symbol = symbol;
63+
}
64+
65+
public IconSequence(Symbol symbol, String repeat) {
66+
this(symbol);
67+
this.repeat = repeat;
68+
}
69+
70+
protected JsonObject getJson() {
71+
JsonObject js = Json.createObject();
72+
JsonObject symbolJs = symbol.getJson();
73+
js.put("icon", symbolJs);
74+
Optional.ofNullable(getRepeat()).ifPresent(value -> js.put("repeat", value));
75+
Optional.ofNullable(isFixedRotation()).ifPresent(value -> js.put("fixedRotation", value));
76+
Optional.ofNullable(getOffset()).ifPresent(value -> js.put("offset", value));
77+
return js;
78+
}
79+
80+
}

0 commit comments

Comments
 (0)