Add api and demo for expression distance (#339)
* Add api and demo for expression distance * Update test case
This commit is contained in:
@@ -8,6 +8,7 @@ import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.mapbox.geojson.GeoJson;
|
||||
import com.mapbox.geojson.Polygon;
|
||||
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
|
||||
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
|
||||
@@ -1574,6 +1575,42 @@ public class Expression {
|
||||
return new Expression("in", literal(needle), haystack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the shortest distance between two geometries.
|
||||
* The returned value can be consumed as an input into another expression for changing a paint or layout property
|
||||
* or filtering features by distance.
|
||||
*
|
||||
* @param geoJson the target feature geoJso.
|
||||
* Currently supports `Point`, `MultiPoint`, `LineString`, `MultiLineString` geometry types
|
||||
* @param unit the unit for the returned value.
|
||||
* Currently supports `metres`, `kilometers`, `miles`, `nauticalmiles`, "yards","feet","inches"。
|
||||
* @return the distance in the unit that is provided.
|
||||
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-distance">Style specification</a>
|
||||
*/
|
||||
public static Expression distance(@NonNull GeoJson geoJson, String unit) {
|
||||
Map<String, Expression> map = new HashMap<>();
|
||||
map.put("json", literal(geoJson.toJson()));
|
||||
return new Expression("distance", new ExpressionMap(map), literal(unit));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the shortest distance between two geometries.
|
||||
* The returned value can be consumed as an input into another expression for changing a paint or layout property
|
||||
* or filtering features by distance.
|
||||
* <p>
|
||||
* Currently supports `Point`, `MultiPoint`, `LineString`, `MultiLineString` geometry types.
|
||||
*
|
||||
* @param geoJson the target feature geoJson.
|
||||
* Currently supports `Point`, `MultiPoint`, `LineString`, `MultiLineString` geometry types
|
||||
* @return the distance in the unit "meters".
|
||||
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-distance">Style specification</a>
|
||||
*/
|
||||
public static Expression distance(@NonNull GeoJson geoJson) {
|
||||
Map<String, Expression> map = new HashMap<>();
|
||||
map.put("json", literal(geoJson.toJson()));
|
||||
return new Expression("distance", new ExpressionMap(map));
|
||||
}
|
||||
|
||||
public static Expression within(@NonNull Polygon polygon) {
|
||||
Map<String, Expression> map = new HashMap<>();
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.color;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.concat;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.cos;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.cubicBezier;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.distance;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.division;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.downcase;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.e;
|
||||
@@ -487,6 +488,22 @@ public class ExpressionTest {
|
||||
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDistance() throws Exception {
|
||||
Point point = Point.fromLngLat(1, 2);
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("json", point.toJson());
|
||||
Object[] expected = new Object[] {"distance", map};
|
||||
Object[] actual = distance(point).toArray();
|
||||
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
|
||||
|
||||
Object[] expectedWithUnit = new Object[] {"distance", map, "meters"};
|
||||
Object[] actualWithUnit = distance(point, "meters").toArray();
|
||||
assertTrue("expression should match", Arrays.deepEquals(expectedWithUnit, actualWithUnit));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInArray() throws Exception {
|
||||
Object[] expected = new Object[] {"in", "one", new Object[] {"literal", new Object[] {"one", "two"}}};
|
||||
|
||||
Reference in New Issue
Block a user