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 9513907
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 79 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
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 @@ -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 @@ -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 @@ -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

0 comments on commit 9513907

Please sign in to comment.