Skip to content

Commit

Permalink
feat: update API to allow setting properties to icons
Browse files Browse the repository at this point in the history
Close #113
  • Loading branch information
paodb authored and javier-godoy committed Mar 14, 2024
1 parent 6fa0978 commit 5cdb14f
Show file tree
Hide file tree
Showing 13 changed files with 568 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public int getZoom() {
* @param position Coordinates of the marker on the map.
* @param draggable Set true to enable dragging of the marker.
* @param iconUrl The url of the icon of the marker.
*
* @return GoogleMapMarker object created with the given settings.
*/
public GoogleMapMarker addMarker(
Expand All @@ -147,6 +148,23 @@ public GoogleMapMarker addMarker(
addMarker(gmm);
return gmm;
}

/**
* Adds a new marker to the map.
*
* @param caption Caption of the marker shown when the marker is hovered.
* @param position Coordinates of the marker on the map.
* @param draggable Set true to enable dragging of the marker.
* @param icon the icon image of the marker.
*
* @return GoogleMapMarker object created with the given settings.
*/
public GoogleMapMarker addMarker(
String caption, LatLon position, boolean draggable, GoogleMapIcon icon) {
GoogleMapMarker gmm = new GoogleMapMarker(caption, position, draggable, icon);
addMarker(gmm);
return gmm;
}

/**
* Creates a polygon with the given points and adds the new polygon to the map.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*-
* #%L
* Google Maps Addon
* %%
* Copyright (C) 2020 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

package com.flowingcode.vaadin.addons.googlemaps;

import elemental.json.JsonObject;

/**
* Interface representing a google map icon ({@link MarkerIcon} or {@link Symbol}).
*/
public interface GoogleMapIcon {

JsonObject getJson();
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,26 @@ public GoogleMapMarker(String caption, LatLon position, boolean draggable) {
* @param caption The caption to use.
* @param position The position of the marker
* @param draggable Can marker be dragged?
* @param iconUrl the icon url as a string
*/
public GoogleMapMarker(String caption, LatLon position, boolean draggable, String iconUrl) {
this(caption, position, draggable);
this.setIconUrl(iconUrl);
}


/**
* Instantiates a new GoogleMapMarker
*
* @param caption The caption to use.
* @param position The position of the marker
* @param draggable Can marker be dragged?
* @param icon the icon image for the marker
*/
public GoogleMapMarker(String caption, LatLon position, boolean draggable, GoogleMapIcon icon) {
this(caption, position, draggable);
this.setIcon(icon);
}

public void addInfoWindow(String htmlContent) {
this.getElement().setProperty("innerHTML", htmlContent);
}
Expand Down Expand Up @@ -172,6 +186,15 @@ public String getIconUrl() {
public void setIconUrl(String iconUrl) {
this.getElement().setProperty("icon", iconUrl);
}

/**
* Sets the icon image of the marker
*
* @param icon the icon image of the marker
*/
public void setIcon(GoogleMapIcon icon) {
this.getElement().setPropertyJson("icon", icon.getJson());
}

/**
* Checks if marker animation is enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,29 @@ public Registration addClickListener(
return addListener(GoogleMapPolyClickEvent.class, listener);
}

/**
* @deprecated, use {@link #setIcons(IconSequence...)} instead.
*/
@Deprecated
public void setIcons(Icon... icons) {
JsonArray jsonArray = Json.createArray();
for (int i = 0; i < icons.length; i++) {
jsonArray.set(i, icons[i].getJson());
}
this.getElement().setPropertyJson("icons", jsonArray);
}

/**
* Set icons to the polygon/polyline.
*
* @param icons the icons to set
*/
public void setIcons(IconSequence... icons) {
JsonArray jsonArray = Json.createArray();
for (int i = 0; i < icons.length; i++) {
jsonArray.set(i, icons[i].getJson());
}
this.getElement().setPropertyJson("icons", jsonArray);
}

}
65 changes: 14 additions & 51 deletions src/main/java/com/flowingcode/vaadin/addons/googlemaps/Icon.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,22 @@
import elemental.json.JsonObject;
import java.util.Optional;

public class Icon {
/**
* @deprecated, use {@link IconSequence} instead.
*/
@Deprecated
public class Icon extends Symbol {

private String path;
private String strokeColor;
private String fillColor;
private int fillOpacity;
private int repeat;

public Icon(String path, String strokeColor, String fillColor, int fillOpacity, int repeat) {
super();
this.path = path;
this.strokeColor = strokeColor;
this.fillColor = fillColor;
this.fillOpacity = fillOpacity;
super(path);
this.setStrokeColor(strokeColor);
this.setFillColor(fillColor);
this.setFillOpacity(Double.valueOf(fillOpacity));
this.repeat = repeat;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getStrokeColor() {
return strokeColor;
}

public void setStrokeColor(String strokeColor) {
this.strokeColor = strokeColor;
}

public String getFillColor() {
return fillColor;
}

public void setFillColor(String fillColor) {
this.fillColor = fillColor;
}

public int getFillOpacity() {
return fillOpacity;
}

public void setFillOpacity(int fillOpacity) {
this.fillOpacity = fillOpacity;
}

public int getRepeat() {
return repeat;
}
Expand All @@ -81,16 +48,12 @@ public void setRepeat(int repeat) {
this.repeat = repeat;
}

protected JsonObject getJson() {
JsonObject options = Json.createObject();
Optional.ofNullable(getPath()).ifPresent(value -> options.put("path", value));
Optional.ofNullable(getStrokeColor()).ifPresent(value -> options.put("strokeColor", value));
Optional.ofNullable(getFillColor()).ifPresent(value -> options.put("fillColor", value));
Optional.ofNullable(getFillOpacity()).ifPresent(value -> options.put("fillOpacity", value));

@Override
public JsonObject getJson() {
JsonObject js = Json.createObject();
js.put("icon", options);
JsonObject iconJs = super.getJson();
js.put("icon", iconJs);
Optional.ofNullable(getRepeat()).ifPresent(v -> js.put("repeat", v + "px"));
return js;
return js;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*-
* #%L
* Google Maps Addon
* %%
* Copyright (C) 2020 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

package com.flowingcode.vaadin.addons.googlemaps;

import elemental.json.Json;
import elemental.json.JsonObject;
import java.util.Optional;
import lombok.Getter;
import lombok.Setter;

/**
* Describes how icons are to be rendered on a line (for polygons and polylines).
*/
@Getter
@Setter
public class IconSequence {

/**
* The icon to render on the line.
*/
private Symbol symbol;

/**
* The distance between consecutive icons on the line. This distance may be expressed as a
* percentage or in pixels. To disable repeating of the icon, specify '0'.
*/
private String repeat;

/**
* If true, each icon in the sequence has the same fixed rotation regardless of the angle of the
* edge on which it lies. If false, case each icon in the sequence is rotated to align with its
* edge. Default value: false.
*/
private boolean fixedRotation;

/**
* The distance from the start of the line at which an icon is to be rendered. This distance may
* be expressed as a percentage or in pixels.
*/
private String offset;


public IconSequence(Symbol symbol) {
this.symbol = symbol;
}

public IconSequence(Symbol symbol, String repeat) {
this(symbol);
this.repeat = repeat;
}

protected JsonObject getJson() {
JsonObject js = Json.createObject();
JsonObject symbolJs = symbol.getJson();
js.put("icon", symbolJs);
Optional.ofNullable(getRepeat()).ifPresent(value -> js.put("repeat", value));
Optional.ofNullable(isFixedRotation()).ifPresent(value -> js.put("fixedRotation", value));
Optional.ofNullable(getOffset()).ifPresent(value -> js.put("offset", value));
return js;
}

}
Loading

0 comments on commit 5cdb14f

Please sign in to comment.