Skip to content

Commit 8abcea8

Browse files
authored
fix(KmlRenderer): Recurse over multi-geometry placemarks (#605)
* Recurse over multi-geometry placemarks Add marker icon recursively to point sub-geometries for multi-geometry placemarks. * Check for null geometry
1 parent 1a36ac4 commit 8abcea8

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

library/src/main/java/com/google/maps/android/data/kml/KmlRenderer.java

+70-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.maps.android.collections.PolylineManager;
2020
import com.google.maps.android.data.Feature;
2121
import com.google.maps.android.data.Geometry;
22+
import com.google.maps.android.data.MultiGeometry;
2223
import com.google.maps.android.data.Renderer;
2324

2425
import java.io.IOException;
@@ -30,6 +31,7 @@
3031
import java.util.ArrayList;
3132
import java.util.HashMap;
3233
import java.util.Iterator;
34+
import java.util.List;
3335

3436
/**
3537
* Renders all visible KmlPlacemark and KmlGroundOverlay objects onto the GoogleMap as Marker,
@@ -273,42 +275,92 @@ private void downloadMarkerIcons() {
273275

274276
/**
275277
* Adds the marker icon stored in mMarkerIconCache, to the {@link com.google.android.gms.maps.model.Marker}
278+
* recursing over multi-geometry placemarks to add icons to all point geometries
276279
*
277280
* @param iconUrl icon url of icon to add to markers
278-
* @param placemarks
281+
* @param placemarks map of placemark to features
279282
*/
280283
private void addIconToMarkers(String iconUrl, HashMap<KmlPlacemark, Object> placemarks) {
281284
for (Feature placemark : placemarks.keySet()) {
282285
KmlStyle urlStyle = getStylesRenderer().get(placemark.getId());
283286
KmlStyle inlineStyle = ((KmlPlacemark) placemark).getInlineStyle();
284-
if ("Point".equals(placemark.getGeometry().getGeometryType())) {
285-
boolean isInlineStyleIcon = inlineStyle != null && iconUrl
286-
.equals(inlineStyle.getIconUrl());
287-
boolean isPlacemarkStyleIcon = urlStyle != null && iconUrl
288-
.equals(urlStyle.getIconUrl());
289-
if (isInlineStyleIcon) {
290-
scaleBitmap(inlineStyle, placemarks, (KmlPlacemark) placemark);
291-
} else if (isPlacemarkStyleIcon) {
292-
scaleBitmap(urlStyle, placemarks, (KmlPlacemark) placemark);
293-
}
294-
}
287+
Geometry geometry = placemark.getGeometry();
288+
Object object = placemarks.get(placemark);
289+
addIconToGeometry(iconUrl, urlStyle, inlineStyle, geometry, object);
290+
}
291+
}
292+
293+
/**
294+
* Checks for point geometry and adds icon to marker or
295+
* recurses over multi-geometries to add icon to point sub-geometries
296+
*
297+
* @param iconUrl icon url of icon to add to markers
298+
* @param urlStyle url style for placemark
299+
* @param inlineStyle inline style for placemark
300+
* @param geometry geometry to check
301+
* @param object object related to geometry, marker if point
302+
* or list of sub-objects for multi-geometries
303+
*/
304+
private void addIconToGeometry(String iconUrl, KmlStyle urlStyle, KmlStyle inlineStyle, Geometry geometry, Object object) {
305+
if (geometry == null) return;
306+
if ("Point".equals(geometry.getGeometryType())) {
307+
addIconToMarker(iconUrl, urlStyle, inlineStyle, (Marker) object);
308+
} else if ("MultiGeometry".equals(geometry.getGeometryType())) {
309+
addIconToMultiGeometry(iconUrl, urlStyle, inlineStyle, (MultiGeometry) geometry, (List<Object>) object);
310+
}
311+
}
312+
313+
/**
314+
* Adds icon to point sub-geometries of multi-geometry placemarks
315+
*
316+
* @param iconUrl icon url of icon to add to markers
317+
* @param urlStyle url style for placemark
318+
* @param inlineStyle inline style for placemark
319+
* @param multiGeometry multi-geometry to iterator over sub-geometries of
320+
* @param objects list of sub-objects for sub-geometries
321+
*/
322+
private void addIconToMultiGeometry(String iconUrl, KmlStyle urlStyle, KmlStyle inlineStyle, MultiGeometry multiGeometry, List<Object> objects) {
323+
Iterator<Geometry> geometries = multiGeometry.getGeometryObject().iterator();
324+
Iterator<Object> objItr = objects.iterator();
325+
while (geometries.hasNext() && objItr.hasNext()) {
326+
Geometry geometry = geometries.next();
327+
Object object = objItr.next();
328+
addIconToGeometry(iconUrl, urlStyle, inlineStyle, geometry, object);
329+
}
330+
}
331+
332+
/**
333+
* Add icon to marker for point geometry placemarks
334+
*
335+
* @param iconUrl icon url of icon to add to markers
336+
* @param urlStyle url style for placemark
337+
* @param inlineStyle inline style for placemark
338+
* @param marker marker to add icon to
339+
*/
340+
private void addIconToMarker(String iconUrl, KmlStyle urlStyle, KmlStyle inlineStyle, Marker marker) {
341+
boolean isInlineStyleIcon = inlineStyle != null && iconUrl
342+
.equals(inlineStyle.getIconUrl());
343+
boolean isPlacemarkStyleIcon = urlStyle != null && iconUrl
344+
.equals(urlStyle.getIconUrl());
345+
if (isInlineStyleIcon) {
346+
scaleBitmap(inlineStyle, marker);
347+
} else if (isPlacemarkStyleIcon) {
348+
scaleBitmap(urlStyle, marker);
295349
}
296350
}
297351

298352
/**
299353
* Enlarges or shrinks a bitmap image based on the scale provided
300354
*
301-
* @param style Style to retrieve iconUrl and scale from
302-
* @param placemarks
303-
* @param placemark Placemark object to set the image to
355+
* @param style Style to retrieve iconUrl and scale from
356+
* @param marker Marker to set the image to
304357
*/
305-
private void scaleBitmap(KmlStyle style, HashMap<KmlPlacemark, Object> placemarks,
306-
KmlPlacemark placemark) {
358+
private void scaleBitmap(KmlStyle style, Marker marker) {
307359
double bitmapScale = style.getIconScale();
308360
String bitmapUrl = style.getIconUrl();
309361
Bitmap bitmapImage = getImagesCache().get(bitmapUrl);
310362
BitmapDescriptor scaledBitmap = scaleIcon(bitmapImage, bitmapScale);
311-
((Marker) placemarks.get(placemark)).setIcon(scaledBitmap);
363+
marker.setIcon(scaledBitmap);
312364
}
313365

314366
/**

0 commit comments

Comments
 (0)