Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
add support for multiple holes
Browse files Browse the repository at this point in the history
  • Loading branch information
Guardiola31337 committed Apr 10, 2017
1 parent b671314 commit d8c1991
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
public abstract class BasePointCollection extends Annotation {

private List<LatLng> points;
private List<LatLng> holePoints;
private float alpha = 1.0f;

protected BasePointCollection() {
super();
points = new ArrayList<>();
holePoints = new ArrayList<>();
}

/**
Expand Down Expand Up @@ -50,36 +48,6 @@ public void addPoint(LatLng point) {
update();
}

/**
* Returns a copy of the hole points.
*
* @return A {@link List} of points.
*/
public List<LatLng> getHolePoints() {
return new ArrayList<>(holePoints);
}

/**
* Sets the points of this polyline hole. This method will take a copy of the points, so further
* mutations to points will have no effect on this polyline hole.
*
* @param holePoints A {@link List} of {@link LatLng} points making up the polyline hole.
*/
public void setHolePoints(List<LatLng> holePoints) {
this.holePoints = new ArrayList<>(holePoints);
update();
}

/**
* Add a point to the polyline hole.
*
* @param holePoint A {@link LatLng} point to be added.
*/
public void addHolePoint(LatLng holePoint) {
holePoints.add(holePoint);
update();
}

/**
* Value between 0 and 1 defining the polyline alpha.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mapbox.mapboxsdk.annotations;


import android.os.Parcel;
import android.os.Parcelable;

import com.mapbox.mapboxsdk.geometry.LatLng;

import java.util.ArrayList;
import java.util.List;

/**
* Encapsulates a {@link List} of {@link LatLng} points defining a hole
*/
public class Hole extends ArrayList<LatLng> implements Parcelable {

public Hole() {
super();
}

/**
* Creates a Hole.
*
* @param holePoints {@link List} list of {@link LatLng} points defining a hole
*/
public Hole(List<LatLng> holePoints) {
super(holePoints);
}

protected Hole(Parcel in) {
in.readTypedList(this, LatLng.CREATOR);
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeTypedList(this);
}

public static final Parcelable.Creator<Hole> CREATOR = new Parcelable.Creator<Hole>() {
public Hole createFromParcel(Parcel in) {
return new Hole(in);
}

public Hole[] newArray(int size) {
return new Hole[size];
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

import com.mapbox.mapboxsdk.maps.MapboxMap;

import java.util.ArrayList;
import java.util.List;

/**
* Polygon is a geometry annotation that's a closed loop of coordinates.
*/
public final class Polygon extends BasePointCollection {

private int fillColor = Color.BLACK; // default fillColor is black
private int strokeColor = Color.BLACK; // default strokeColor is black
private List<Hole> holes;

Polygon() {
super();
holes = new ArrayList<>();
}

/**
Expand All @@ -26,14 +31,23 @@ public int getFillColor() {
}

/**
* Get the color fo the stroke of the polygon.
* Get the color of the stroke of the polygon.
*
* @return The color of the stroke.
*/
public int getStrokeColor() {
return strokeColor;
}

/**
* Returns a copy of the holes.
*
* @return A {@link List} of holes.
*/
public List<Hole> getHoles() {
return new ArrayList<>(holes);
}

/**
* Sets the color of the fill region of the polygon.
*
Expand All @@ -54,6 +68,27 @@ public void setStrokeColor(int color) {
update();
}

/**
* Sets the holes of this polygon. This method will take a copy of the holes, so further
* mutations to holes will have no effect on this polygon.
*
* @param holes A {@link List} of {@link Hole} points making up the holes.
*/
public void setHoles(List<? extends Hole> holes) {
this.holes = new ArrayList<>(holes);
update();
}

/**
* Add a hole to the polygon.
*
* @param hole A {@link Hole} hole to be added.
*/
void addHole(Hole hole) {
holes.add(hole);
update();
}

@Override
void update() {
MapboxMap mapboxMap = getMapboxMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ private PolygonOptions(Parcel in) {
ArrayList<LatLng> pointsList = new ArrayList<>();
in.readList(pointsList, LatLng.class.getClassLoader());
addAll(pointsList);
ArrayList<Hole> holes = new ArrayList<>();
in.readTypedList(holes, Hole.CREATOR);
addAllHoles(holes);
alpha(in.readFloat());
fillColor(in.readInt());
strokeColor(in.readInt());
Expand All @@ -56,6 +59,7 @@ public int describeContents() {
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeList(getPoints());
out.writeTypedList(getHoles());
out.writeFloat(getAlpha());
out.writeInt(getFillColor());
out.writeInt(getStrokeColor());
Expand Down Expand Up @@ -108,6 +112,43 @@ public PolygonOptions addAll(Iterable<LatLng> points) {
return this;
}

/**
* Adds a hole to the outline of the polygon being built.
*
* @param hole {@link Hole} list made up of {@link LatLng} points defining the hole
* @return This {@link PolygonOptions} object with the given hole added to the outline.
*/
public PolygonOptions addHole(Hole hole) {
polygon.addHole(hole);
return this;
}

/**
* Adds holes to the outline of the polygon being built.
*
* @param holes {@link Hole} holes to be added to polygon geometry.
* @return This {@link PolygonOptions} object with the given holes added to the outline.
*/
public PolygonOptions addHole(Hole... holes) {
for (Hole hole : holes) {
addHole(hole);
}
return this;
}

/**
* Adds holes to the outline of the polygon being built.
*
* @param holes {@link Iterable} list made up of {@link Hole} holes defining the hole geometry
* @return This {@link PolygonOptions} object with the given holes added to the outline.
*/
public PolygonOptions addAllHoles(Iterable<Hole> holes) {
for (Hole hole : holes) {
addHole(hole);
}
return this;
}

/**
* Set the alpha value of the polyline.
*
Expand Down Expand Up @@ -177,18 +218,33 @@ public int getStrokeColor() {
return polygon.getStrokeColor();
}

/**
* Gets the points set for this {@link PolygonOptions} object.
*
* @return The list made up of {@link LatLng} points defining the polygon.
*/
public List<LatLng> getPoints() {
// the getter gives us a copy, which is the safe thing to do...
return polygon.getPoints();
}

/**
* Gets the holes set for this {@link PolygonOptions} object.
*
* @return The list made up of {@link List} of {@link LatLng} points defining the holes.
*/
public List<Hole> getHoles() {
return polygon.getHoles();
}


/**
* Compares this {@link PolygonOptions} object with another {@link PolygonOptions} and
* determines if their color, alpha, stroke color, and vertices match.
*
* @param o Another {@link PolygonOptions} to compare with this object.
* @return True if color, alpha, stroke color, and vertices match this {@link PolygonOptions}
* object. Else, false.
* @return True if color, alpha, stroke color, vertices and holes match this {@link PolygonOptions}
* {@link PolygonOptions} object. Else, false.
*/
@Override
public boolean equals(Object o) {
Expand All @@ -210,7 +266,10 @@ public boolean equals(Object o) {
if (getStrokeColor() != polygon.getStrokeColor()) {
return false;
}
return !(getPoints() != null ? !getPoints().equals(polygon.getPoints()) : polygon.getPoints() != null);
if (getPoints() != null ? !getPoints().equals(polygon.getPoints()) : polygon.getPoints() != null) {
return false;
}
return !(getHoles() != null ? !getHoles().equals(polygon.getHoles()) : polygon.getHoles() != null);
}

/**
Expand All @@ -228,6 +287,7 @@ public int hashCode() {
result = 31 * result + getFillColor();
result = 31 * result + getStrokeColor();
result = 31 * result + (getPoints() != null ? getPoints().hashCode() : 0);
result = 31 * result + (getHoles() != null ? getHoles().hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.Menu;
import android.view.MenuItem;

import com.mapbox.mapboxsdk.annotations.Hole;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
Expand All @@ -18,6 +19,7 @@
import com.mapbox.mapboxsdk.testapp.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.BLUE_COLOR;
Expand All @@ -26,8 +28,8 @@
import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.NO_ALPHA;
import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.PARTIAL_ALPHA;
import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.RED_COLOR;
import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.STAR_SHAPE_HOLES;
import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.STAR_SHAPE_POINTS;
import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.TRIANGLE_HOLE_SHAPE_POINTS;

/**
* Test activity to showcase the Polygon annotation API & programmatically creating a MapView.
Expand All @@ -45,6 +47,7 @@ public class PolygonActivity extends AppCompatActivity implements OnMapReadyCall
private boolean visible = true;
private boolean color = true;
private boolean allPoints;
private boolean holes;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -136,12 +139,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_id_points:
allPoints = !allPoints;
polygon.setPoints(allPoints ? STAR_SHAPE_POINTS : BROKEN_SHAPE_POINTS);
polygon.setHolePoints(TRIANGLE_HOLE_SHAPE_POINTS);
return true;
case R.id.action_id_color:
color = !color;
polygon.setFillColor(color ? BLUE_COLOR : RED_COLOR);
return true;
case R.id.action_id_holes:
holes = !holes;
polygon.setHoles(holes ? STAR_SHAPE_HOLES : Collections.<Hole>emptyList());
return true;
default:
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -182,12 +188,25 @@ static final class Config {
static final List<LatLng> BROKEN_SHAPE_POINTS =
STAR_SHAPE_POINTS.subList(0, STAR_SHAPE_POINTS.size() - 3);

static final List<LatLng> TRIANGLE_HOLE_SHAPE_POINTS = new ArrayList<LatLng>() {
static final List<? extends Hole> STAR_SHAPE_HOLES = new ArrayList<Hole>() {
{
add(new LatLng(45.521743, -122.669091));
add(new LatLng(45.530483, -122.676833));
add(new LatLng(45.520483, -122.676833));
add(new LatLng(45.521743, -122.669091));
add(new Hole(new ArrayList<LatLng>() {
{
add(new LatLng(45.521743, -122.669091));
add(new LatLng(45.530483, -122.676833));
add(new LatLng(45.520483, -122.676833));
add(new LatLng(45.521743, -122.669091));
}
}));
add(new Hole(new ArrayList<LatLng>() {
{
add(new LatLng(45.529743, -122.662791));
add(new LatLng(45.525543, -122.662791));
add(new LatLng(45.525543, -122.660));
add(new LatLng(45.527743, -122.660));
add(new LatLng(45.529743, -122.662791));
}
}));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
android:id="@+id/action_id_color"
android:title="@string/action_color_polygon"
mapbox:showAsAction="never" />
<item
android:id="@+id/action_id_holes"
android:title="@string/action_holes_polygon"
mapbox:showAsAction="never" />
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<string name="action_alpha_polygon">Change alpha</string>
<string name="action_points_polygon">Change points</string>
<string name="action_color_polygon">Change color</string>
<string name="action_holes_polygon">Change holes</string>
<string name="action_width_polyline">Change width</string>

<!--Menu-->
Expand Down
Loading

0 comments on commit d8c1991

Please sign in to comment.