Skip to content

Commit 992b797

Browse files
authored
feat: Support KML BalloonStyle template (#606)
1 parent 0ffe279 commit 992b797

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

library/src/main/java/com/google/maps/android/data/Renderer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.google.maps.android.data.kml.KmlPlacemark;
5151
import com.google.maps.android.data.kml.KmlPoint;
5252
import com.google.maps.android.data.kml.KmlStyle;
53+
import com.google.maps.android.data.kml.KmlUtil;
5354

5455
import android.graphics.Bitmap;
5556
import android.os.Bundle;
@@ -1010,7 +1011,7 @@ private void setMarkerInfoWindow(KmlStyle style, Marker marker,
10101011
boolean hasBalloonOptions = style.hasBalloonStyle();
10111012
boolean hasBalloonText = style.getBalloonOptions().containsKey("text");
10121013
if (hasBalloonOptions && hasBalloonText) {
1013-
marker.setTitle(style.getBalloonOptions().get("text"));
1014+
marker.setTitle(KmlUtil.substituteProperties(style.getBalloonOptions().get("text"), placemark));
10141015
createInfoWindow();
10151016
} else if (hasBalloonOptions && hasName) {
10161017
marker.setTitle(placemark.getProperty("name"));

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.google.maps.android.data.Feature;
77
import com.google.maps.android.data.Geometry;
88

9-
import java.util.HashMap;
9+
import java.util.Map;
1010

1111
/**
1212
* Represents a placemark which is either a {@link com.google.maps.android.data.kml.KmlPoint},
@@ -29,7 +29,7 @@ public class KmlPlacemark extends Feature {
2929
* @param properties properties hashmap to store
3030
*/
3131
public KmlPlacemark(Geometry geometry, String style, KmlStyle inlineStyle,
32-
HashMap<String, String> properties) {
32+
Map<String, String> properties) {
3333
super(geometry, style, properties);
3434
mStyle = style;
3535
mInlineStyle = inlineStyle;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.google.maps.android.data.kml;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* Utility methods for KML
8+
*/
9+
public class KmlUtil {
10+
11+
/**
12+
* Substitute property values in BalloonStyle text template
13+
*
14+
* @param template text template
15+
* @param placemark placemark to get property values from
16+
* @return string with property values substituted
17+
*/
18+
public static String substituteProperties(String template, KmlPlacemark placemark) {
19+
StringBuffer sb = new StringBuffer();
20+
Pattern pattern = Pattern.compile("\\$\\[(.+?)]");
21+
Matcher matcher = pattern.matcher(template);
22+
while (matcher.find()) {
23+
String property = matcher.group(1);
24+
String value = placemark.getProperty(property);
25+
if (value != null) {
26+
matcher.appendReplacement(sb, value);
27+
}
28+
}
29+
matcher.appendTail(sb);
30+
return sb.toString();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.google.maps.android.data.kml;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class KmlUtilTest {
11+
12+
@Test
13+
public void testSubstituteProperties() {
14+
Map<String, String> properties = new HashMap<>();
15+
properties.put("name", "Bruce Wayne");
16+
properties.put("description", "Batman");
17+
properties.put("Snippet", "I am the night");
18+
KmlPlacemark placemark = new KmlPlacemark(null, null, null, properties);
19+
20+
String result1 = KmlUtil.substituteProperties("$[name] is my name", placemark);
21+
assertEquals("Bruce Wayne is my name", result1);
22+
23+
String result2 = KmlUtil.substituteProperties("Also known as $[description]", placemark);
24+
assertEquals("Also known as Batman", result2);
25+
26+
String result3 = KmlUtil.substituteProperties("I say \"$[Snippet]\" often", placemark);
27+
assertEquals("I say \"I am the night\" often", result3);
28+
29+
String result4 = KmlUtil.substituteProperties("My address is $[address]", placemark);
30+
assertEquals("When property doesn't exist, placeholder is left in place", "My address is $[address]", result4);
31+
}
32+
}

0 commit comments

Comments
 (0)