Skip to content

Commit 122e96b

Browse files
authored
fix: Make KML/GeoJSON lines and polygons clickable by default (#614)
As discussed in #558, the original intent when combining the KML and GeoJSON renderers was to have GeoJSON and KML lines and polygons clickable by default. However, there was a regression in #454 that unintentionally disabled clicks for KML and GeoJSON polygons when attempting to allow developers to use GeoJSON custom styles to set clickable. I investigated both KML and GeoJSON lines and polygons, and here is the current state of the library: * For KML/GeoJSON polygons - we're currently supporting developer-defined clickable styles for GeoJSON (the change in #454), but that results in a default of not clickable for GeoJSON and KML. And there isn't a way to override the default in KmlLayer. * For KML/GeoJSON lines - we're currently ignoring developer-defined clickable styles and hard-coding the default clickable style to true. This PR solves both problems (default of "not clickable" and supporting developer-defined styles) as follows: * Makes all styles for GeoJSON and KML lines and polygons clickable by default by setting clickable=true in style constructors rather than the renderer. This also allows the developer to set a custom style with clickable=false on GeoJSON lines and polygons (custom programmatic KML styles currently aren't supported by the library). * Make clickable default to true in the Style superclass - this should hopefully avoid a similar issue in any new implementations that extend Style * Normalize the GeoJsonLineStringStyle and GeoJsonPolygonStyle clickable implementations - GeoJsonPolygonStyle was missing code related to the clickable attribute * Add missing <name> attribute to the KML polygon demo file used in the MultiLayerDemoActivity * Add unit tests: * For GeoJSON - test both default and programmatic custom clickable styles for both the style and renderer classes * For KML - test the default style for both the style and renderer classes * Refactor KML and GeoJSON test utility methods used in more than one test class to new Kml/GeoJsonTestUtil classes * Fix GeoJSON polygonStyle.toPolygonOptions().isVisible() test (this previously didn't check PolygonOptions) I've also added TODO statements in the unit tests where we could expand coverage further (i.e., after adding the layers to the map), but that requires additional test instrumentation to have a live GoogleMap object. Fixes #558 Refs: 558
1 parent 8abcea8 commit 122e96b

15 files changed

+750
-570
lines changed

demo/src/main/res/raw/south_london_square_kml.kml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<kml xmlns="http://www.opengis.net/kml/2.2">
33
<Document>
44
<Placemark>
5+
<name>South London Polygon KML</name>
56
<ExtendedData></ExtendedData>
67
<Polygon>
78
<outerBoundaryIs>

library/src/androidTest/java/com/google/maps/android/data/geojson/GeoJsonLineStringStyleTest.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.google.maps.android.data.geojson;
22

3-
import android.graphics.Color;
4-
53
import org.junit.Before;
64
import org.junit.Test;
75

6+
import android.graphics.Color;
7+
88
import java.util.Arrays;
99

10-
import static org.junit.Assert.*;
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertTrue;
1113

1214
public class GeoJsonLineStringStyleTest {
1315
private GeoJsonLineStringStyle lineStringStyle;
@@ -75,6 +77,7 @@ public void testDefaultLineStringStyle() {
7577
assertTrue(lineStringStyle.isVisible());
7678
assertEquals(10.0f, lineStringStyle.getWidth(), 0);
7779
assertEquals(0.0f, lineStringStyle.getZIndex(), 0);
80+
assertTrue(lineStringStyle.isClickable());
7881
}
7982

8083
@Test
@@ -84,5 +87,6 @@ public void testDefaultGetPolylineOptions() {
8487
assertTrue(lineStringStyle.toPolylineOptions().isVisible());
8588
assertEquals(10.0f, lineStringStyle.toPolylineOptions().getWidth(), 0);
8689
assertEquals(0.0f, lineStringStyle.toPolylineOptions().getZIndex(), 0);
90+
assertTrue(lineStringStyle.toPolylineOptions().isClickable());
8791
}
8892
}

library/src/androidTest/java/com/google/maps/android/data/geojson/GeoJsonParserTest.java

+28-492
Large diffs are not rendered by default.

library/src/androidTest/java/com/google/maps/android/data/geojson/GeoJsonPolygonStyleTest.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.google.maps.android.data.geojson;
22

3-
import android.graphics.Color;
4-
53
import org.junit.Before;
64
import org.junit.Test;
75

6+
import android.graphics.Color;
7+
88
import java.util.Arrays;
99

10-
import static org.junit.Assert.*;
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertTrue;
1113

1214
public class GeoJsonPolygonStyleTest {
1315
private GeoJsonPolygonStyle polygonStyle;
@@ -91,6 +93,7 @@ public void testDefaultPolygonStyle() {
9193
assertEquals(10.0f, polygonStyle.getStrokeWidth(), 0);
9294
assertTrue(polygonStyle.isVisible());
9395
assertEquals(0.0f, polygonStyle.getZIndex(), 0);
96+
assertTrue(polygonStyle.isClickable());
9497
}
9598

9699
@Test
@@ -99,7 +102,8 @@ public void testDefaultGetPolygonOptions() {
99102
assertFalse(polygonStyle.toPolygonOptions().isGeodesic());
100103
assertEquals(Color.BLACK, polygonStyle.toPolygonOptions().getStrokeColor());
101104
assertEquals(10.0f, polygonStyle.toPolygonOptions().getStrokeWidth(), 0);
102-
assertTrue(polygonStyle.isVisible());
105+
assertTrue(polygonStyle.toPolygonOptions().isVisible());
103106
assertEquals(0.0f, polygonStyle.toPolygonOptions().getZIndex(), 0);
107+
assertTrue(polygonStyle.toPolygonOptions().isClickable());
104108
}
105109
}

library/src/androidTest/java/com/google/maps/android/data/geojson/GeoJsonRendererTest.java

+44-39
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.google.android.gms.maps.GoogleMap;
44
import com.google.android.gms.maps.model.LatLng;
5+
import com.google.maps.android.data.Feature;
56

6-
import org.json.JSONObject;
77
import org.junit.Before;
88
import org.junit.Test;
99

@@ -13,7 +13,11 @@
1313
import java.util.HashMap;
1414
import java.util.Set;
1515

16-
import static org.junit.Assert.*;
16+
import static com.google.maps.android.data.geojson.GeoJsonTestUtil.createFeatureCollection;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertFalse;
19+
import static org.junit.Assert.assertNull;
20+
import static org.junit.Assert.assertTrue;
1721

1822
public class GeoJsonRendererTest {
1923
private GoogleMap mMap1;
@@ -28,6 +32,10 @@ public void setUp() throws Exception {
2832
GeoJsonParser parser = new GeoJsonParser(createFeatureCollection());
2933
HashMap<GeoJsonFeature, Object> geoJsonFeatures = new HashMap<>();
3034
for (GeoJsonFeature feature : parser.getFeatures()) {
35+
// Set default styles
36+
feature.setPointStyle(new GeoJsonPointStyle());
37+
feature.setLineStringStyle(new GeoJsonLineStringStyle());
38+
feature.setPolygonStyle(new GeoJsonPolygonStyle());
3139
geoJsonFeatures.put(feature, null);
3240
}
3341
geoJsonFeaturesSet = geoJsonFeatures.keySet();
@@ -85,42 +93,39 @@ public void testRemoveFeature() {
8593
assertFalse(mRenderer.getFeatures().contains(mGeoJsonFeature));
8694
}
8795

88-
private JSONObject createFeatureCollection() throws Exception {
89-
return new JSONObject(
90-
"{ \"type\": \"FeatureCollection\",\n"
91-
+ " \"features\": [\n"
92-
+ " { \"type\": \"Feature\",\n"
93-
+ " \"geometry\": {\"type\": \"MultiPoint\", \"coordinates\": [[102.0,"
94-
+ " 0.5], [100, 0.5]]},\n"
95-
+ " \"properties\": {\"title\": \"Test MultiPoint\"}\n"
96-
+ " },\n"
97-
+ " { \"type\": \"Feature\",\n"
98-
+ " \"geometry\": {\n"
99-
+ " \"type\": \"MultiLineString\",\n"
100-
+ " \"coordinates\": [\n"
101-
+ " [[100, 0],[101, 1]], [[102, 2], [103, 3]]\n"
102-
+ " ]\n"
103-
+ " },\n"
104-
+ " \"properties\": {\n"
105-
+ " \"title\": \"Test MultiLineString\"\n"
106-
+ " }\n"
107-
+ " },\n"
108-
+ " { \"type\": \"Feature\",\n"
109-
+ " \"geometry\": {\n"
110-
+ " \"type\": \"MultiPolygon\",\n"
111-
+ " \"coordinates\": [\n"
112-
+ " [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0],"
113-
+ " [102.0, 2.0]]],\n"
114-
+ " [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0,"
115-
+ " 0.0]],\n"
116-
+ " [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2,"
117-
+ " 0.2]]],\n"
118-
+ " ]\n"
119-
+ " },\n"
120-
+ " \"properties\": {\n"
121-
+ " \"title\": \"Test MultiPolygon\"}\n"
122-
+ " }\n"
123-
+ " ]\n"
124-
+ " }");
96+
@Test
97+
public void testDefaultStyleClickable() {
98+
// TODO - we should call mRenderer.addLayerToMap() here for a complete end-to-end test, but
99+
// that requires an instantiated GoogleMap be passed into GeoJsonRenderer()
100+
for (Feature f : mRenderer.getFeatures()) {
101+
assertTrue(((GeoJsonFeature)f).getPolylineOptions().isClickable());
102+
assertTrue(((GeoJsonFeature)f).getPolygonOptions().isClickable());
103+
}
104+
}
105+
106+
@Test
107+
public void testCustomStyleNotClickable() throws Exception {
108+
GeoJsonParser parser = new GeoJsonParser(createFeatureCollection());
109+
HashMap<GeoJsonFeature, Object> geoJsonFeatures = new HashMap<>();
110+
for (GeoJsonFeature feature : parser.getFeatures()) {
111+
// Set default style for points
112+
feature.setPointStyle(new GeoJsonPointStyle());
113+
// Set custom style of not clickable for lines and polygons
114+
GeoJsonLineStringStyle ls = new GeoJsonLineStringStyle();
115+
ls.setClickable(false);
116+
feature.setLineStringStyle(ls);
117+
GeoJsonPolygonStyle ps = new GeoJsonPolygonStyle();
118+
ps.setClickable(false);
119+
feature.setPolygonStyle(ps);
120+
geoJsonFeatures.put(feature, null);
121+
}
122+
123+
GeoJsonRenderer renderer = new GeoJsonRenderer(mMap1, geoJsonFeatures, null, null, null, null);
124+
// TODO - we should call renderer.addLayerToMap() here for a complete end-to-end test, but
125+
// that requires an instantiated GoogleMap be passed into GeoJsonRenderer()
126+
for (Feature f : renderer.getFeatures()) {
127+
assertFalse(((GeoJsonFeature)f).getPolylineOptions().isClickable());
128+
assertFalse(((GeoJsonFeature)f).getPolygonOptions().isClickable());
129+
}
125130
}
126131
}

0 commit comments

Comments
 (0)