Skip to content

Commit dba3b0d

Browse files
committed
Merge pull request #205 from barbeau/simplifyInputChange
Fix #204 - Don't change reference for last point in PolyUtil.simplify()
2 parents ca39b45 + c7f201c commit dba3b0d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

library/src/com/google/maps/android/PolyUtil.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,13 @@ public static List<LatLng> simplify(List<LatLng> poly, double tolerance) {
313313
}
314314

315315
boolean closedPolygon = isClosedPolygon(poly);
316+
LatLng lastPoint = null;
316317

317318
// Check if the provided poly is a closed polygon
318319
if (closedPolygon) {
319320
// Add a small offset to the last point for Douglas-Peucker on polygons (see #201)
320321
final double OFFSET = 0.00000000001;
321-
LatLng lastPoint = poly.get(poly.size() - 1);
322+
lastPoint = poly.get(poly.size() - 1);
322323
// LatLng.latitude and .longitude are immutable, so replace the last point
323324
poly.remove(poly.size() - 1);
324325
poly.add(new LatLng(lastPoint.latitude + OFFSET, lastPoint.longitude + OFFSET));
@@ -359,9 +360,9 @@ public static List<LatLng> simplify(List<LatLng> poly, double tolerance) {
359360
}
360361

361362
if (closedPolygon) {
362-
// Replace last point of input (w/ offset) with a copy of first to re-close the polygon
363+
// Replace last point w/ offset with the original last point to re-close the polygon
363364
poly.remove(poly.size() - 1);
364-
poly.add(new LatLng(poly.get(0).latitude, poly.get(0).longitude));
365+
poly.add(lastPoint);
365366
}
366367

367368
// Generate the simplified line

library/tests/src/com/google/maps/android/PolyUtilTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,13 @@ private List<LatLng> copyList(List<LatLng> original) {
368368
* @param beforeInput a copy of the list before it is passed into PolyUtil.simplify()
369369
*/
370370
private void assertInputUnchanged(List<LatLng> afterInput, List<LatLng> beforeInput) {
371+
// Check values
371372
assertEquals(beforeInput, afterInput);
373+
374+
// Check references
375+
for (int i = 0; i < beforeInput.size(); i++) {
376+
assertTrue(afterInput.get(i) == beforeInput.get(i));
377+
}
372378
}
373379

374380
public void testIsClosedPolygon() {

0 commit comments

Comments
 (0)