Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Commit

Permalink
Android polygon holes support (react-native-maps#1781)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Jeo authored and bennywrong committed Jan 28, 2018
1 parent c451310 commit 5b245f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/polygon.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| Prop | Type | Default | Note |
|---|---|---|---|
| `coordinates` | `Array<LatLng>` | (Required) | An array of coordinates to describe the polygon
| `holes` | `Array<Array<LatLng>>` | | 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class AirMapPolygon extends AirMapFeature {
private Polygon polygon;

private List<LatLng> coordinates;
private List<List<LatLng>> holes;
private int strokeColor;
private int fillColor;
private float strokeWidth;
Expand All @@ -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<LatLng> 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) {
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b245f8

Please sign in to comment.