Skip to content

Commit

Permalink
[annotation] - generic geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Nov 21, 2018
1 parent a38c56d commit 2d76836
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 90 deletions.
63 changes: 8 additions & 55 deletions plugin-annotation/scripts/annotation.java.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import static com.mapbox.mapboxsdk.constants.GeometryConstants.MAX_MERCATOR_LATI
import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE;

@UiThread
public class <%- camelize(type) %> extends Annotation {
public class <%- camelize(type) %> extends Annotation<<%- geometryType(type) %>> {

private final AnnotationManager<?, <%- camelize(type) %>, ?, ?, ?, ?> annotationManager;
<% if (type === "symbol") { -%>
Expand All @@ -44,7 +44,7 @@ public class <%- camelize(type) %> extends Annotation {
* @param jsonObject the features of the annotation
* @param geometry the geometry of the annotation
*/
<%- camelize(type) %>(long id, AnnotationManager<?, <%- camelize(type) %>, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, Geometry geometry) {
<%- camelize(type) %>(long id, AnnotationManager<?, <%- camelize(type) %>, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, <%- geometryType(type) %> geometry) {
super(id, jsonObject, geometry);
this.annotationManager = annotationManager;
}
Expand Down Expand Up @@ -85,8 +85,7 @@ public class <%- camelize(type) %> extends Annotation {
*/
@NonNull
public LatLng getLatLng() {
Point point = (Point) geometry;
return new LatLng(point.latitude(), point.longitude());
return new LatLng(geometry.latitude(), geometry.longitude());
}
<% } else if (type === "line") { -%>
Expand Down Expand Up @@ -164,7 +163,6 @@ public class <%- camelize(type) %> extends Annotation {
return latLngs;
}
<% } -%>
<% if (type === "circle" || type === "symbol") { -%>

/**
* Set the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map
Expand All @@ -174,7 +172,7 @@ public class <%- camelize(type) %> extends Annotation {
*
* @param geometry the geometry of the <%- type %>
*/
public void setGeometry(Point geometry) {
public void setGeometry(<%- geometryType(type) %> geometry) {
this.geometry = geometry;
}

Expand All @@ -183,54 +181,9 @@ public class <%- camelize(type) %> extends Annotation {
*
* @return the geometry of the <%- type %>
*/
public Point getGeometry() {
return ((Point) geometry);
}
<% } else if (type === "line") { -%>
/**
* Set the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map
* <p>
* To update the <%- type %> on the map use {@link <%- camelize(type) %>Manager#update(Annotation)}.
* <p>
*
* @param geometry the geometry of the <%- type %>
*/
public void setGeometry(LineString geometry) {
this.geometry = geometry;
}
/**
* Get the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map
*
* @return geometry the geometry of the <%- type %>
*/
public LineString getGeometry() {
return ((LineString) geometry);
public <%- geometryType(type) %> getGeometry() {
return geometry;
}
<% } else { -%>
/**
* Set the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map
* <p>
* To update the <%- type %> on the map use {@link <%- camelize(type) %>Manager#update(Annotation)}.
* <p>
*
* @param geometry the geometry of the <%- type %>
*/
public void setGeometry(Polygon geometry) {
this.geometry = geometry;
}
/**
* Get the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map
*
* @return the geometry of the <%- type %>
*/
public Polygon getGeometry() {
return ((Polygon) geometry);
}
<% } -%>
<% if (type === "symbol") { -%>
/**
Expand Down Expand Up @@ -390,7 +343,7 @@ public class <%- camelize(type) %> extends Annotation {
@Nullable
Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject,
float touchAreaShiftX, float touchAreaShiftY) {
List<Point> originalPoints = ((LineString) getGeometry()).coordinates();
List<Point> originalPoints = geometry.coordinates();
List<Point> resultingPoints = new ArrayList<>(originalPoints.size());
for (Point jsonPoint : originalPoints) {
PointF pointF = projection.toScreenLocation(new LatLng(jsonPoint.latitude(), jsonPoint.longitude()));
Expand All @@ -412,7 +365,7 @@ public class <%- camelize(type) %> extends Annotation {
@Nullable
Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject,
float touchAreaShiftX, float touchAreaShiftY) {
List<List<Point>> originalPoints = ((Polygon) getGeometry()).coordinates();
List<List<Point>> originalPoints = geometry.coordinates();
if (originalPoints != null) {
List<List<Point>> resultingPoints = new ArrayList<>(originalPoints.size());
for (List<Point> points : originalPoints) {
Expand Down
6 changes: 3 additions & 3 deletions plugin-annotation/scripts/annotation_options.java.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import static com.mapbox.mapboxsdk.plugins.annotation.ConvertUtils.toStringArray
public class <%- camelize(type) %>Options extends Options<<%- camelize(type) %>> {

private boolean isDraggable;
private Geometry geometry;
private <%- geometryType(type) %> geometry;
<% for (const property of properties) { -%>
<% if (supportsPropertyFunction(property)) { -%>
private <%- propertyType(property) %> <%- camelizeWithLeadingLowercase(property.name) %>;
Expand Down Expand Up @@ -82,7 +82,7 @@ public class <%- camelize(type) %>Options extends Options<<%- camelize(type) %>>
* @param geometry the location of the <%- type %>
* @return this
*/
public <%- camelize(type) %>Options withGeometry(Point geometry) {
public <%- camelize(type) %>Options withGeometry(<%- geometryType(type) %> geometry) {
this.geometry = geometry;
return this;
}
Expand Down Expand Up @@ -248,7 +248,7 @@ public class <%- camelize(type) %>Options extends Options<<%- camelize(type) %>>
}
<%- camelize(type) %>Options options = new <%- camelize(type) %>Options();
options.geometry = feature.geometry();
options.geometry = (<%- geometryType(type) %>) feature.geometry();
<% for (const property of properties) { -%>
<% if (supportsPropertyFunction(property)) { -%>
<% if (propertyType(property).endsWith("[]")) { -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
import com.mapbox.geojson.Geometry;
import com.mapbox.mapboxsdk.maps.Projection;

public abstract class Annotation {
public abstract class Annotation<T extends Geometry> {

static final String ID_KEY = "id";
protected JsonObject jsonObject;
protected Geometry geometry;
protected T geometry;
private boolean isDraggable;

Annotation(long id, JsonObject jsonObject, Geometry geometry) {
Annotation(long id, JsonObject jsonObject, T geometry) {
this.jsonObject = jsonObject;
this.jsonObject.addProperty(ID_KEY, id);
this.geometry = geometry;
}

void setGeometry(Geometry geometry) {
void setGeometry(T geometry) {
this.geometry = geometry;
}

Geometry getGeometry() {
T getGeometry() {
if (geometry == null) {
throw new IllegalStateException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE;

@UiThread
public class Circle extends Annotation {
public class Circle extends Annotation<Point> {

private final AnnotationManager<?, Circle, ?, ?, ?, ?> annotationManager;

Expand All @@ -35,7 +35,7 @@ public class Circle extends Annotation {
* @param jsonObject the features of the annotation
* @param geometry the geometry of the annotation
*/
Circle(long id, AnnotationManager<?, Circle, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, Geometry geometry) {
Circle(long id, AnnotationManager<?, Circle, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, Point geometry) {
super(id, jsonObject, geometry);
this.annotationManager = annotationManager;
}
Expand Down Expand Up @@ -84,8 +84,7 @@ public void setLatLng(LatLng latLng) {
*/
@NonNull
public LatLng getLatLng() {
Point point = (Point) geometry;
return new LatLng(point.latitude(), point.longitude());
return new LatLng(geometry.latitude(), geometry.longitude());
}

/**
Expand All @@ -106,7 +105,7 @@ public void setGeometry(Point geometry) {
* @return the geometry of the circle
*/
public Point getGeometry() {
return ((Point) geometry);
return geometry;
}

// Property accessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class CircleOptions extends Options<Circle> {

private boolean isDraggable;
private Geometry geometry;
private Point geometry;
private Float circleRadius;
private String circleColor;
private Float circleBlur;
Expand Down Expand Up @@ -262,7 +262,7 @@ static CircleOptions fromFeature(@NonNull Feature feature) {
}

CircleOptions options = new CircleOptions();
options.geometry = feature.geometry();
options.geometry = (Point) feature.geometry();
if (feature.hasProperty("circle-radius")) {
options.circleRadius = feature.getProperty("circle-radius").getAsFloat();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE;

@UiThread
public class Fill extends Annotation {
public class Fill extends Annotation<Polygon> {

private final AnnotationManager<?, Fill, ?, ?, ?, ?> annotationManager;

Expand All @@ -35,7 +35,7 @@ public class Fill extends Annotation {
* @param jsonObject the features of the annotation
* @param geometry the geometry of the annotation
*/
Fill(long id, AnnotationManager<?, Fill, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, Geometry geometry) {
Fill(long id, AnnotationManager<?, Fill, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, Polygon geometry) {
super(id, jsonObject, geometry);
this.annotationManager = annotationManager;
}
Expand Down Expand Up @@ -116,7 +116,7 @@ public void setGeometry(Polygon geometry) {
* @return the geometry of the fill
*/
public Polygon getGeometry() {
return ((Polygon) geometry);
return geometry;
}

// Property accessors
Expand Down Expand Up @@ -211,7 +211,7 @@ public void setFillPattern(String value) {
@Nullable
Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject,
float touchAreaShiftX, float touchAreaShiftY) {
List<List<Point>> originalPoints = ((Polygon) getGeometry()).coordinates();
List<List<Point>> originalPoints = geometry.coordinates();
if (originalPoints != null) {
List<List<Point>> resultingPoints = new ArrayList<>(originalPoints.size());
for (List<Point> points : originalPoints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class FillOptions extends Options<Fill> {

private boolean isDraggable;
private Geometry geometry;
private Polygon geometry;
private Float fillOpacity;
private String fillColor;
private String fillOutlineColor;
Expand Down Expand Up @@ -200,7 +200,7 @@ static FillOptions fromFeature(@NonNull Feature feature) {
}

FillOptions options = new FillOptions();
options.geometry = feature.geometry();
options.geometry = (Polygon) feature.geometry();
if (feature.hasProperty("fill-opacity")) {
options.fillOpacity = feature.getProperty("fill-opacity").getAsFloat();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE;

@UiThread
public class Line extends Annotation {
public class Line extends Annotation<LineString> {

private final AnnotationManager<?, Line, ?, ?, ?, ?> annotationManager;

Expand All @@ -35,7 +35,7 @@ public class Line extends Annotation {
* @param jsonObject the features of the annotation
* @param geometry the geometry of the annotation
*/
Line(long id, AnnotationManager<?, Line, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, Geometry geometry) {
Line(long id, AnnotationManager<?, Line, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, LineString geometry) {
super(id, jsonObject, geometry);
this.annotationManager = annotationManager;
}
Expand Down Expand Up @@ -114,10 +114,10 @@ public void setGeometry(LineString geometry) {
/**
* Get the Geometry of the line, which represents the location of the line on the map
*
* @return geometry the geometry of the line
* @return the geometry of the line
*/
public LineString getGeometry() {
return ((LineString) geometry);
return geometry;
}

// Property accessors
Expand Down Expand Up @@ -295,7 +295,7 @@ public void setLinePattern(String value) {
@Nullable
Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject,
float touchAreaShiftX, float touchAreaShiftY) {
List<Point> originalPoints = ((LineString) getGeometry()).coordinates();
List<Point> originalPoints = geometry.coordinates();
List<Point> resultingPoints = new ArrayList<>(originalPoints.size());
for (Point jsonPoint : originalPoints) {
PointF pointF = projection.toScreenLocation(new LatLng(jsonPoint.latitude(), jsonPoint.longitude()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class LineOptions extends Options<Line> {

private boolean isDraggable;
private Geometry geometry;
private LineString geometry;
private String lineJoin;
private Float lineOpacity;
private String lineColor;
Expand Down Expand Up @@ -289,7 +289,7 @@ static LineOptions fromFeature(@NonNull Feature feature) {
}

LineOptions options = new LineOptions();
options.geometry = feature.geometry();
options.geometry = (LineString) feature.geometry();
if (feature.hasProperty("line-join")) {
options.lineJoin = feature.getProperty("line-join").getAsString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE;

@UiThread
public class Symbol extends Annotation {
public class Symbol extends Annotation<Point> {

private final AnnotationManager<?, Symbol, ?, ?, ?, ?> annotationManager;

Expand All @@ -37,7 +37,7 @@ public class Symbol extends Annotation {
* @param jsonObject the features of the annotation
* @param geometry the geometry of the annotation
*/
Symbol(long id, AnnotationManager<?, Symbol, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, Geometry geometry) {
Symbol(long id, AnnotationManager<?, Symbol, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, Point geometry) {
super(id, jsonObject, geometry);
this.annotationManager = annotationManager;
}
Expand Down Expand Up @@ -143,8 +143,7 @@ public void setLatLng(LatLng latLng) {
*/
@NonNull
public LatLng getLatLng() {
Point point = (Point) geometry;
return new LatLng(point.latitude(), point.longitude());
return new LatLng(geometry.latitude(), geometry.longitude());
}

/**
Expand All @@ -165,7 +164,7 @@ public void setGeometry(Point geometry) {
* @return the geometry of the symbol
*/
public Point getGeometry() {
return ((Point) geometry);
return geometry;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class SymbolOptions extends Options<Symbol> {

private boolean isDraggable;
private Geometry geometry;
private Point geometry;
private Float iconSize;
private String iconImage;
private Float iconRotate;
Expand Down Expand Up @@ -702,7 +702,7 @@ static SymbolOptions fromFeature(@NonNull Feature feature) {
}

SymbolOptions options = new SymbolOptions();
options.geometry = feature.geometry();
options.geometry = (Point) feature.geometry();
if (feature.hasProperty("icon-size")) {
options.iconSize = feature.getProperty("icon-size").getAsFloat();
}
Expand Down

0 comments on commit 2d76836

Please sign in to comment.