From 5b245f85ddba7791fc4581d4e2c48e5932278eba Mon Sep 17 00:00:00 2001 From: Lee Jeo Date: Fri, 19 Jan 2018 21:43:13 +0900 Subject: [PATCH] Android polygon holes support (#1781) --- docs/polygon.md | 1 + .../android/react/maps/AirMapPolygon.java | 40 +++++++++++++++++++ .../react/maps/AirMapPolygonManager.java | 5 +++ 3 files changed, 46 insertions(+) diff --git a/docs/polygon.md b/docs/polygon.md index f13f507559..fb5b25beb9 100644 --- a/docs/polygon.md +++ b/docs/polygon.md @@ -5,6 +5,7 @@ | Prop | Type | Default | Note | |---|---|---|---| | `coordinates` | `Array` | (Required) | An array of coordinates to describe the polygon +| `holes` | `Array>` | | A 2d array of coordinates to describe holes of the polygon where each hole has at least 3 points. | `strokeWidth` | `Number` | `1` | The stroke width to use for the path. | `strokeColor` | `String` | `#000`, `rgba(r,g,b,0.5)` | The stroke color to use for the path. | `fillColor` | `String` | `#000`, `rgba(r,g,b,0.5)` | The fill color to use for the path. diff --git a/lib/android/src/main/java/com/airbnb/android/react/maps/AirMapPolygon.java b/lib/android/src/main/java/com/airbnb/android/react/maps/AirMapPolygon.java index 41257b32e6..5f2729378f 100644 --- a/lib/android/src/main/java/com/airbnb/android/react/maps/AirMapPolygon.java +++ b/lib/android/src/main/java/com/airbnb/android/react/maps/AirMapPolygon.java @@ -18,6 +18,7 @@ public class AirMapPolygon extends AirMapFeature { private Polygon polygon; private List coordinates; + private List> holes; private int strokeColor; private int fillColor; private float strokeWidth; @@ -41,6 +42,38 @@ public void setCoordinates(ReadableArray coordinates) { } } + public void setHoles(ReadableArray holes) { + if (holes == null) { return; } + + this.holes = new ArrayList<>(holes.size()); + + for (int i = 0; i < holes.size(); i++) { + ReadableArray hole = holes.getArray(i); + + if (hole.size() < 3) { continue; } + + List coordinates = new ArrayList<>(); + for (int j = 0; j < hole.size(); j++) { + ReadableMap coordinate = hole.getMap(j); + coordinates.add(new LatLng( + coordinate.getDouble("latitude"), + coordinate.getDouble("longitude"))); + } + + // If hole is triangle + if (coordinates.size() == 3) { + coordinates.add(coordinates.get(0)); + } + + this.holes.add(coordinates); + } + + if (polygon != null) { + polygon.setHoles(this.holes); + } + } + + public void setFillColor(int color) { this.fillColor = color; if (polygon != null) { @@ -91,6 +124,13 @@ private PolygonOptions createPolygonOptions() { options.strokeWidth(strokeWidth); options.geodesic(geodesic); options.zIndex(zIndex); + + if (this.holes != null) { + for (int i = 0; i < holes.size(); i++) { + options.addHole(holes.get(i)); + } + } + return options; } diff --git a/lib/android/src/main/java/com/airbnb/android/react/maps/AirMapPolygonManager.java b/lib/android/src/main/java/com/airbnb/android/react/maps/AirMapPolygonManager.java index 6f16057585..0f0378026d 100644 --- a/lib/android/src/main/java/com/airbnb/android/react/maps/AirMapPolygonManager.java +++ b/lib/android/src/main/java/com/airbnb/android/react/maps/AirMapPolygonManager.java @@ -47,6 +47,11 @@ public void setCoordinate(AirMapPolygon view, ReadableArray coordinates) { view.setCoordinates(coordinates); } + @ReactProp(name = "holes") + public void setHoles(AirMapPolygon view, ReadableArray holes) { + view.setHoles(holes); + } + @ReactProp(name = "strokeWidth", defaultFloat = 1f) public void setStrokeWidth(AirMapPolygon view, float widthInPoints) { float widthInScreenPx = metrics.density * widthInPoints; // done for parity with iOS