Skip to content

Commit a1b341c

Browse files
committed
feat: add support to add polylines
Close #102
1 parent d2eb54c commit a1b341c

File tree

6 files changed

+303
-161
lines changed

6 files changed

+303
-161
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ public void addPolygon(GoogleMapPolygon polygon) {
160160
public void removePolygon(GoogleMapPolygon polygon) {
161161
this.getElement().removeChild(polygon.getElement());
162162
}
163+
164+
public GoogleMapPolyline addPolyline(List<GoogleMapPoint> points) {
165+
GoogleMapPolyline polyline = new GoogleMapPolyline(points);
166+
this.getElement().appendChild(polyline.getElement());
167+
return polyline;
168+
}
169+
170+
public void addPolyline(GoogleMapPolyline polyline) {
171+
this.getElement().appendChild(polyline.getElement());
172+
}
173+
174+
@SuppressWarnings("squid:S3242")
175+
public void removePolyline(GoogleMapPolyline polyline) {
176+
this.getElement().removeChild(polyline.getElement());
177+
}
163178

164179
/**
165180
* Adds a marker to the map.
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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 com.vaadin.flow.component.ClickEvent;
24+
import com.vaadin.flow.component.Component;
25+
import com.vaadin.flow.component.ComponentEventListener;
26+
import com.vaadin.flow.component.DomEvent;
27+
import com.vaadin.flow.component.EventData;
28+
import com.vaadin.flow.component.Tag;
29+
import com.vaadin.flow.component.dependency.JsModule;
30+
import com.vaadin.flow.component.dependency.NpmPackage;
31+
import com.vaadin.flow.shared.Registration;
32+
import elemental.json.Json;
33+
import elemental.json.JsonArray;
34+
import elemental.json.JsonObject;
35+
import elemental.json.JsonValue;
36+
import java.util.List;
37+
38+
@SuppressWarnings("serial")
39+
@Tag("google-map-poly")
40+
@JsModule("@flowingcode/google-map/google-map-poly.js")
41+
@JsModule("@flowingcode/google-map/google-map-point.js")
42+
@NpmPackage(value = "@flowingcode/google-map", version = "3.6.1")
43+
@NpmPackage(value = "@googlemaps/markerclusterer", version = "2.0.8")
44+
public abstract class GoogleMapPoly extends Component {
45+
46+
private static final double DEFAULT_FILL_OPACITY = 0.5d;
47+
private static final String DEFAULT_FILL_COLOR = "blue";
48+
49+
public enum StrokePosition {
50+
CENTER,
51+
INSIDE,
52+
OUTSIDE
53+
}
54+
55+
public GoogleMapPoly(List<GoogleMapPoint> points) {
56+
getElement().removeProperty("draggable");
57+
setFillColor(DEFAULT_FILL_COLOR);
58+
setFillOpacity(DEFAULT_FILL_OPACITY);
59+
setPoints(points);
60+
}
61+
62+
public void setFillOpacity(double opacity) {
63+
this.getElement().setProperty("fillOpacity", opacity);
64+
}
65+
66+
public double getFillOpacity() {
67+
return this.getElement().getProperty("fillOpacity", 1d);
68+
}
69+
70+
public void setStrokeOpacity(double opacity) {
71+
this.getElement().setProperty("strokeOpacity", opacity);
72+
}
73+
74+
public double getStrokeOpacity() {
75+
return this.getElement().getProperty("strokeOpacity", 1d);
76+
}
77+
78+
public void setStrokePosition(StrokePosition position) {
79+
this.getElement().setProperty("strokePosition", position.name().toLowerCase());
80+
}
81+
82+
public StrokePosition getStrokePosition() {
83+
return StrokePosition.valueOf(this.getElement().getProperty("strokePosition").toUpperCase());
84+
}
85+
86+
public void setStrokeWeight(double weight) {
87+
this.getElement().setProperty("strokeWeight", weight);
88+
}
89+
90+
public double getStrokeWeight() {
91+
return this.getElement().getProperty("strokeWeight", 1d);
92+
}
93+
94+
public void setZIndex(double zindex) {
95+
this.getElement().setProperty("zIndex", zindex);
96+
}
97+
98+
public double getZIndex() {
99+
return this.getElement().getProperty("zIndex", 1d);
100+
}
101+
102+
public void setFillColor(String string) {
103+
this.getElement().setProperty("fillColor", string);
104+
}
105+
106+
public String getFillColor() {
107+
return this.getElement().getProperty("fillColor");
108+
}
109+
110+
public void setStrokeColor(String string) {
111+
this.getElement().setProperty("strokeColor", string);
112+
}
113+
114+
public String getStrokeColor() {
115+
return this.getElement().getProperty("strokeColor");
116+
}
117+
118+
public void setClosed(boolean b) {
119+
this.getElement().setProperty("closed", b);
120+
}
121+
122+
public boolean isClosed() {
123+
return this.getElement().getProperty("closed", false);
124+
}
125+
126+
public void setGeodesic(boolean b) {
127+
this.getElement().setProperty("geodesic", b);
128+
}
129+
130+
public boolean isGeodesic() {
131+
return this.getElement().getProperty("geodesic", false);
132+
}
133+
134+
public void setPoints(Iterable<GoogleMapPoint> points) {
135+
points.forEach(point -> this.getElement().appendChild(point.getElement()));
136+
}
137+
138+
public GoogleMapPoint addPoint(LatLon position) {
139+
GoogleMapPoint point = new GoogleMapPoint(position.getLat(), position.getLon());
140+
this.getElement().appendChild(point.getElement());
141+
return point;
142+
}
143+
144+
public void addPoint(GoogleMapPoint point) {
145+
this.getElement().appendChild(point.getElement());
146+
}
147+
148+
@SuppressWarnings("squid:S3242")
149+
public void removePoint(GoogleMapPoint point) {
150+
this.getElement().removeChild(point.getElement());
151+
}
152+
153+
@DomEvent("google-map-poly-click")
154+
public static class GoogleMapPolyClickEvent extends ClickEvent<GoogleMapPolygon> {
155+
156+
private final double lat;
157+
private final double lon;
158+
159+
public GoogleMapPolyClickEvent(
160+
GoogleMapPoly source,
161+
boolean fromClient,
162+
@EventData(value = "event.detail.latLng") JsonValue latLng) {
163+
super(source);
164+
this.lat = ((JsonObject) latLng).getNumber("lat");
165+
this.lon = ((JsonObject) latLng).getNumber("lng");
166+
}
167+
168+
public double getLatitude() {
169+
return this.lat;
170+
}
171+
172+
public double getLongitude() {
173+
return this.lon;
174+
}
175+
}
176+
177+
public Registration addClickListener(
178+
ComponentEventListener<GoogleMapPolyClickEvent> listener) {
179+
this.getElement().setProperty("clickable", true);
180+
this.getElement().setProperty("clickEvents", true);
181+
return addListener(GoogleMapPolyClickEvent.class, listener);
182+
}
183+
184+
public void setIcons(Icon... icons) {
185+
JsonArray jsonArray = Json.createArray();
186+
for (int i = 0; i < icons.length; i++) {
187+
jsonArray.set(i, icons[i].getJson());
188+
}
189+
this.getElement().setPropertyJson("icons", jsonArray);
190+
}
191+
}

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

Lines changed: 2 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -20,174 +20,15 @@
2020

2121
package com.flowingcode.vaadin.addons.googlemaps;
2222

23-
import com.vaadin.flow.component.ClickEvent;
24-
import com.vaadin.flow.component.Component;
25-
import com.vaadin.flow.component.ComponentEventListener;
26-
import com.vaadin.flow.component.DomEvent;
27-
import com.vaadin.flow.component.EventData;
28-
import com.vaadin.flow.component.Tag;
29-
import com.vaadin.flow.component.dependency.JsModule;
30-
import com.vaadin.flow.component.dependency.NpmPackage;
31-
import com.vaadin.flow.shared.Registration;
32-
import elemental.json.Json;
33-
import elemental.json.JsonArray;
34-
import elemental.json.JsonObject;
35-
import elemental.json.JsonValue;
3623
import java.util.List;
3724

3825
/** A class representing a polygon overlay of Google Maps. */
3926
@SuppressWarnings("serial")
40-
@Tag("google-map-poly")
41-
@JsModule("@flowingcode/google-map/google-map-poly.js")
42-
@JsModule("@flowingcode/google-map/google-map-point.js")
43-
@NpmPackage(value = "@flowingcode/google-map", version = "3.6.1")
44-
@NpmPackage(value = "@googlemaps/markerclusterer", version = "2.0.8")
45-
public class GoogleMapPolygon extends Component {
46-
47-
private static final double DEFAULT_FILL_OPACITY = 0.5d;
48-
private static final String DEFAULT_FILL_COLOR = "blue";
49-
50-
public enum StrokePosition {
51-
CENTER,
52-
INSIDE,
53-
OUTSIDE
54-
}
27+
public class GoogleMapPolygon extends GoogleMapPoly {
5528

5629
public GoogleMapPolygon(List<GoogleMapPoint> points) {
57-
getElement().removeProperty("draggable");
30+
super(points);
5831
setClosed(true);
59-
setFillColor(DEFAULT_FILL_COLOR);
60-
setFillOpacity(DEFAULT_FILL_OPACITY);
61-
setPoints(points);
62-
}
63-
64-
public void setFillOpacity(double opacity) {
65-
this.getElement().setProperty("fillOpacity", opacity);
66-
}
67-
68-
public double getFillOpacity() {
69-
return this.getElement().getProperty("fillOpacity", 1d);
70-
}
71-
72-
public void setStrokeOpacity(double opacity) {
73-
this.getElement().setProperty("strokeOpacity", opacity);
74-
}
75-
76-
public double getStrokeOpacity() {
77-
return this.getElement().getProperty("strokeOpacity", 1d);
78-
}
79-
80-
public void setStrokePosition(StrokePosition position) {
81-
this.getElement().setProperty("strokePosition", position.name().toLowerCase());
82-
}
83-
84-
public StrokePosition getStrokePosition() {
85-
return StrokePosition.valueOf(this.getElement().getProperty("strokePosition").toUpperCase());
86-
}
87-
88-
public void setStrokeWeight(double weight) {
89-
this.getElement().setProperty("strokeWeight", weight);
90-
}
91-
92-
public double getStrokeWeight() {
93-
return this.getElement().getProperty("strokeWeight", 1d);
94-
}
95-
96-
public void setZIndex(double zindex) {
97-
this.getElement().setProperty("zIndex", zindex);
98-
}
99-
100-
public double getZIndex() {
101-
return this.getElement().getProperty("zIndex", 1d);
102-
}
103-
104-
public void setFillColor(String string) {
105-
this.getElement().setProperty("fillColor", string);
106-
}
107-
108-
public String getFillColor() {
109-
return this.getElement().getProperty("fillColor");
110-
}
111-
112-
public void setStrokeColor(String string) {
113-
this.getElement().setProperty("strokeColor", string);
114-
}
115-
116-
public String getStrokeColor() {
117-
return this.getElement().getProperty("strokeColor");
118-
}
119-
120-
public void setClosed(boolean b) {
121-
this.getElement().setProperty("closed", b);
122-
}
123-
124-
public boolean isClosed() {
125-
return this.getElement().getProperty("closed", false);
126-
}
127-
128-
public void setGeodesic(boolean b) {
129-
this.getElement().setProperty("geodesic", b);
130-
}
131-
132-
public boolean isGeodesic() {
133-
return this.getElement().getProperty("geodesic", false);
134-
}
135-
136-
public void setPoints(Iterable<GoogleMapPoint> points) {
137-
points.forEach(point -> this.getElement().appendChild(point.getElement()));
138-
}
139-
140-
public GoogleMapPoint addPoint(LatLon position) {
141-
GoogleMapPoint point = new GoogleMapPoint(position.getLat(), position.getLon());
142-
this.getElement().appendChild(point.getElement());
143-
return point;
144-
}
145-
146-
public void addPoint(GoogleMapPoint point) {
147-
this.getElement().appendChild(point.getElement());
148-
}
149-
150-
@SuppressWarnings("squid:S3242")
151-
public void removePoint(GoogleMapPoint point) {
152-
this.getElement().removeChild(point.getElement());
153-
}
154-
155-
@DomEvent("google-map-poly-click")
156-
public static class GoogleMapPolygonClickEvent extends ClickEvent<GoogleMapPolygon> {
157-
158-
private final double lat;
159-
private final double lon;
160-
161-
public GoogleMapPolygonClickEvent(
162-
GoogleMapPolygon source,
163-
boolean fromClient,
164-
@EventData(value = "event.detail.latLng") JsonValue latLng) {
165-
super(source);
166-
this.lat = ((JsonObject) latLng).getNumber("lat");
167-
this.lon = ((JsonObject) latLng).getNumber("lng");
168-
}
169-
170-
public double getLatitude() {
171-
return this.lat;
172-
}
173-
174-
public double getLongitude() {
175-
return this.lon;
176-
}
17732
}
17833

179-
public Registration addClickListener(
180-
ComponentEventListener<GoogleMapPolygonClickEvent> listener) {
181-
this.getElement().setProperty("clickable", true);
182-
this.getElement().setProperty("clickEvents", true);
183-
return addListener(GoogleMapPolygonClickEvent.class, listener);
184-
}
185-
186-
public void setIcons(Icon... icons) {
187-
JsonArray jsonArray = Json.createArray();
188-
for (int i = 0; i < icons.length; i++) {
189-
jsonArray.set(i, icons[i].getJson());
190-
}
191-
this.getElement().setPropertyJson("icons", jsonArray);
192-
}
19334
}

0 commit comments

Comments
 (0)