Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import android.view.MenuItem;
import android.widget.Toast;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
Expand All @@ -15,6 +19,7 @@
import com.mapbox.mapboxsdk.plugins.annotation.Fill;
import com.mapbox.mapboxsdk.plugins.annotation.FillManager;
import com.mapbox.mapboxsdk.plugins.annotation.FillOptions;
import com.mapbox.mapboxsdk.plugins.annotation.OnFillClickListener;
import com.mapbox.mapboxsdk.plugins.testapp.R;
import com.mapbox.mapboxsdk.plugins.testapp.Utils;
import com.mapbox.mapboxsdk.utils.ColorUtils;
Expand Down Expand Up @@ -47,11 +52,12 @@ protected void onCreate(Bundle savedInstanceState) {

fillManager = new FillManager(mapView, mapboxMap, style);
fillManager.addClickListener(fill -> Toast.makeText(FillActivity.this,
String.format("Fill clicked %s", fill.getId()),
String.format("Fill clicked %s with title: %s", fill.getId(), getTitleFromFill(fill)),
Toast.LENGTH_SHORT
).show());

fillManager.addLongClickListener(fill -> Toast.makeText(FillActivity.this,
String.format("Fill long clicked %s", fill.getId()),
String.format("Fill long clicked %s with title: %s", fill.getId(), getTitleFromFill(fill)),
Toast.LENGTH_SHORT
).show());

Expand All @@ -66,12 +72,13 @@ protected void onCreate(Bundle savedInstanceState) {

FillOptions fillOptions = new FillOptions()
.withLatLngs(latLngs)
.withData(new JsonPrimitive("Foobar"))
.withFillColor(ColorUtils.colorToRgbaString(Color.RED));
fillManager.create(fillOptions);

// random add fills across the globe
List<FillOptions> fillOptionsList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
for (int i = 0; i < 3; i++) {
int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
fillOptionsList.add(new FillOptions()
.withLatLngs(createRandomLatLngs())
Expand All @@ -88,6 +95,15 @@ protected void onCreate(Bundle savedInstanceState) {
}));
}

private String getTitleFromFill(Fill fill) {
String title = "unknown";
JsonElement customData = fill.getData();
if (!(customData.isJsonNull())) {
title = customData.getAsString();
}
return title;
}

private List<List<LatLng>> createRandomLatLngs() {
List<LatLng> latLngs = new ArrayList<>();
LatLng firstLast = new LatLng((random.nextDouble() * -180.0) + 90.0,
Expand Down
25 changes: 25 additions & 0 deletions plugin-annotation/scripts/annotation_manager_unit_test.junit.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.mapbox.mapboxsdk.plugins.annotation;

import com.google.gson.JsonPrimitive;
import com.mapbox.geojson.*;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
Expand Down Expand Up @@ -526,6 +527,30 @@ public class <%- camelize(type) %>ManagerTest {
assertTrue(<%- type %>Manager.getDragListeners().isEmpty());
}

@Test
public void testCustomData() {
<%- type %>Manager = new <%- camelize(type) %>Manager(mapView, mapboxMap, style, coreElementProvider, null, null, draggableAnnotationController);
<% if (type === "circle" || type === "symbol") { -%>
<%- camelize(type) %>Options options = new <%- camelize(type) %>Options().withLatLng(new LatLng());
<% } else if (type === "line") { -%>
List<LatLng>latLngs = new ArrayList<>();
latLngs.add(new LatLng());
latLngs.add(new LatLng(1,1));
<%- camelize(type) %>Options options = new <%- camelize(type) %>Options().withLatLngs(latLngs);
<% } else { -%>
List<LatLng>innerLatLngs = new ArrayList<>();
innerLatLngs.add(new LatLng());
innerLatLngs.add(new LatLng(1,1));
innerLatLngs.add(new LatLng(-1,-1));
List<List<LatLng>>latLngs = new ArrayList<>();
latLngs.add(innerLatLngs);
<%- camelize(type) %>Options options = new <%- camelize(type) %>Options().withLatLngs(latLngs);
<% } -%>
options.withData(new JsonPrimitive("hello"));
<%- camelize(type) %> <%- type %> = <%- type %>Manager.create(options);
assertEquals(new JsonPrimitive("hello"), <%- type %>.getData());
}

@Test
public void testClearAll() {
<%- type %>Manager = new <%- camelize(type) %>Manager(mapView, mapboxMap, style, coreElementProvider, null, null, draggableAnnotationController);
Expand Down
22 changes: 22 additions & 0 deletions plugin-annotation/scripts/annotation_options.java.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import static com.mapbox.mapboxsdk.plugins.annotation.ConvertUtils.toStringArray
public class <%- camelize(type) %>Options extends Options<<%- camelize(type) %>> {

private boolean isDraggable;
private JsonElement data;
private <%- geometryType(type) %> geometry;
<% for (const property of properties) { -%>
<% if (supportsPropertyFunction(property)) { -%>
Expand Down Expand Up @@ -279,6 +280,26 @@ public class <%- camelize(type) %>Options extends Options<<%- camelize(type) %>>
return this;
}

/**
* Set the arbitrary json data of the annotation.
*
* @param jsonElement the arbitrary json element data
*/
public <%- camelize(type) %>Options withData(@Nullable JsonElement jsonElement) {
this.data = jsonElement;
return this;
}

/**
* Get the arbitrary json data of the annotation.
*
* @return the arbitrary json object data if set, else null
*/
@Nullable
public JsonElement getData() {
return data;
}

@Override
<%- camelize(type) %> build(long id, AnnotationManager<?, <%- camelize(type) %>, ?, ?, ?, ?> annotationManager) {
if (geometry == null) {
Expand All @@ -299,6 +320,7 @@ public class <%- camelize(type) %>Options extends Options<<%- camelize(type) %>>
<% } -%>
<%- camelize(type) %> <%- type %> = new <%- camelize(type) %>(id, annotationManager, jsonObject, geometry);
<%- type %>.setDraggable(isDraggable);
<%- type %>.setData(data);
return <%- type %>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mapbox.android.gestures.MoveDistancesObject;
import com.mapbox.geojson.Geometry;
Expand All @@ -11,6 +12,7 @@
public abstract class Annotation<T extends Geometry> {

static final String ID_KEY = "id";
static final String ID_DATA = "custom_data";
protected JsonObject jsonObject;
protected T geometry;
private boolean isDraggable;
Expand Down Expand Up @@ -75,6 +77,25 @@ public void setDraggable(boolean draggable) {
isDraggable = draggable;
}

/**
* Set the arbitrary json data of the annotation.
*
* @param jsonElement the arbitrary json object data
*/
public void setData(@Nullable JsonElement jsonElement) {
this.jsonObject.add(ID_DATA, jsonElement);
}

/**
* Get the arbitrary json object data of the annotation.
*
* @return the arbitrary json object data if set, else null
*/
@Nullable
public JsonElement getData() {
return jsonObject.get(ID_DATA);
}

@Nullable
abstract Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject,
float touchAreaShiftX, float touchAreaShiftY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class CircleOptions extends Options<Circle> {

private boolean isDraggable;
private JsonElement data;
private Point geometry;
private Float circleRadius;
private String circleColor;
Expand Down Expand Up @@ -275,6 +276,26 @@ public CircleOptions withDraggable(boolean draggable) {
return this;
}

/**
* Set the arbitrary json data of the annotation.
*
* @param jsonElement the arbitrary json element data
*/
public CircleOptions withData(@Nullable JsonElement jsonElement) {
this.data = jsonElement;
return this;
}

/**
* Get the arbitrary json data of the annotation.
*
* @return the arbitrary json object data if set, else null
*/
@Nullable
public JsonElement getData() {
return data;
}

@Override
Circle build(long id, AnnotationManager<?, Circle, ?, ?, ?, ?> annotationManager) {
if (geometry == null) {
Expand All @@ -290,6 +311,7 @@ Circle build(long id, AnnotationManager<?, Circle, ?, ?, ?, ?> annotationManager
jsonObject.addProperty(PROPERTY_CIRCLE_STROKE_OPACITY, circleStrokeOpacity);
Circle circle = new Circle(id, annotationManager, jsonObject, geometry);
circle.setDraggable(isDraggable);
circle.setData(data);
return circle;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class FillOptions extends Options<Fill> {

private boolean isDraggable;
private JsonElement data;
private Polygon geometry;
private Float fillOpacity;
private String fillColor;
Expand Down Expand Up @@ -212,6 +213,26 @@ public FillOptions withDraggable(boolean draggable) {
return this;
}

/**
* Set the arbitrary json data of the annotation.
*
* @param jsonElement the arbitrary json element data
*/
public FillOptions withData(@Nullable JsonElement jsonElement) {
this.data = jsonElement;
return this;
}

/**
* Get the arbitrary json data of the annotation.
*
* @return the arbitrary json object data if set, else null
*/
@Nullable
public JsonElement getData() {
return data;
}

@Override
Fill build(long id, AnnotationManager<?, Fill, ?, ?, ?, ?> annotationManager) {
if (geometry == null) {
Expand All @@ -224,6 +245,7 @@ Fill build(long id, AnnotationManager<?, Fill, ?, ?, ?, ?> annotationManager) {
jsonObject.addProperty(PROPERTY_FILL_PATTERN, fillPattern);
Fill fill = new Fill(id, annotationManager, jsonObject, geometry);
fill.setDraggable(isDraggable);
fill.setData(data);
return fill;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class LineOptions extends Options<Line> {

private boolean isDraggable;
private JsonElement data;
private LineString geometry;
private String lineJoin;
private Float lineOpacity;
Expand Down Expand Up @@ -308,6 +309,26 @@ public LineOptions withDraggable(boolean draggable) {
return this;
}

/**
* Set the arbitrary json data of the annotation.
*
* @param jsonElement the arbitrary json element data
*/
public LineOptions withData(@Nullable JsonElement jsonElement) {
this.data = jsonElement;
return this;
}

/**
* Get the arbitrary json data of the annotation.
*
* @return the arbitrary json object data if set, else null
*/
@Nullable
public JsonElement getData() {
return data;
}

@Override
Line build(long id, AnnotationManager<?, Line, ?, ?, ?, ?> annotationManager) {
if (geometry == null) {
Expand All @@ -324,6 +345,7 @@ Line build(long id, AnnotationManager<?, Line, ?, ?, ?, ?> annotationManager) {
jsonObject.addProperty(PROPERTY_LINE_PATTERN, linePattern);
Line line = new Line(id, annotationManager, jsonObject, geometry);
line.setDraggable(isDraggable);
line.setData(data);
return line;
}

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

private boolean isDraggable;
private JsonElement data;
private Point geometry;
private Float iconSize;
private String iconImage;
Expand Down Expand Up @@ -768,6 +769,26 @@ public SymbolOptions withDraggable(boolean draggable) {
return this;
}

/**
* Set the arbitrary json data of the annotation.
*
* @param jsonElement the arbitrary json element data
*/
public SymbolOptions withData(@Nullable JsonElement jsonElement) {
this.data = jsonElement;
return this;
}

/**
* Get the arbitrary json data of the annotation.
*
* @return the arbitrary json object data if set, else null
*/
@Nullable
public JsonElement getData() {
return data;
}

@Override
Symbol build(long id, AnnotationManager<?, Symbol, ?, ?, ?, ?> annotationManager) {
if (geometry == null) {
Expand Down Expand Up @@ -802,6 +823,7 @@ Symbol build(long id, AnnotationManager<?, Symbol, ?, ?, ?, ?> annotationManager
jsonObject.addProperty(PROPERTY_Z_INDEX, zIndex);
Symbol symbol = new Symbol(id, annotationManager, jsonObject, geometry);
symbol.setDraggable(isDraggable);
symbol.setData(data);
return symbol;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.mapbox.mapboxsdk.plugins.annotation;

import com.google.gson.JsonPrimitive;
import com.mapbox.geojson.*;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
Expand Down Expand Up @@ -379,6 +380,15 @@ public void testDragListener() {
assertTrue(circleManager.getDragListeners().isEmpty());
}

@Test
public void testCustomData() {
circleManager = new CircleManager(mapView, mapboxMap, style, coreElementProvider, null, null, draggableAnnotationController);
CircleOptions options = new CircleOptions().withLatLng(new LatLng());
options.withData(new JsonPrimitive("hello"));
Circle circle = circleManager.create(options);
assertEquals(new JsonPrimitive("hello"), circle.getData());
}

@Test
public void testClearAll() {
circleManager = new CircleManager(mapView, mapboxMap, style, coreElementProvider, null, null, draggableAnnotationController);
Expand Down
Loading