Skip to content

Commit

Permalink
fix: Correctly parse <hotSpot> elements (#1173)
Browse files Browse the repository at this point in the history
* fix: correctly parse hotSpot elements

* feat: warn users that only "fraction" is supported for hotSpot elements
  • Loading branch information
SpecialMike committed Dec 20, 2023
1 parent 24ce693 commit 643d351
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
9 changes: 9 additions & 0 deletions library/src/main/java/com/google/maps/android/data/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.maps.android.data;

import android.util.Log;

import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.PolylineOptions;
Expand All @@ -30,6 +32,7 @@
* and {@link com.google.maps.android.data.geojson.GeoJsonPolygonStyle GeoJsonPolygonStyle}
*/
public abstract class Style extends Observable {
private static final String LOG_TAG = "Style";

protected MarkerOptions mMarkerOptions;

Expand Down Expand Up @@ -82,9 +85,15 @@ public void setMarkerHotSpot(float x, float y, String xUnits, String yUnits) {
if (xUnits.equals("fraction")) {
xAnchor = x;
}
else {
Log.w(LOG_TAG, "Hotspot xUnits other than \"fraction\" are not supported.");
}
if (yUnits.equals("fraction")) {
yAnchor = y;
}
else {
Log.w(LOG_TAG, "Hotspot yUnits other than \"fraction\" are not supported.");
}

mMarkerOptions.anchor(xAnchor, yAnchor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import static org.xmlpull.v1.XmlPullParser.END_TAG;
import static org.xmlpull.v1.XmlPullParser.START_TAG;

import android.util.Log;

/**
* Parses the styles of a given KML file into a KmlStyle object
*/
/* package */ class KmlStyleParser {
private static final String LOG_TAG = "KmlStyleParser";

private final static String STYLE_TAG = "styleUrl";

Expand Down Expand Up @@ -184,18 +187,24 @@ private static void setIconUrl(XmlPullParser parser, KmlStyle style)
*
* @param style Style object to apply hotspot properties to
*/
private static void setIconHotSpot(XmlPullParser parser, KmlStyle style)
throws XmlPullParserException {
if (parser.isEmptyElementTag()) {
return;
}
private static void setIconHotSpot(XmlPullParser parser, KmlStyle style) {
float xValue, yValue;
String xUnits, yUnits;
xValue = Float.parseFloat(parser.getAttributeValue(null, "x"));
yValue = Float.parseFloat(parser.getAttributeValue(null, "y"));
xUnits = parser.getAttributeValue(null, "xunits");
yUnits = parser.getAttributeValue(null, "yunits");
style.setHotSpot(xValue, yValue, xUnits, yUnits);

try {
xValue = Float.parseFloat(parser.getAttributeValue(null, "x"));
yValue = Float.parseFloat(parser.getAttributeValue(null, "y"));
xUnits = parser.getAttributeValue(null, "xunits");
yUnits = parser.getAttributeValue(null, "yunits");
style.setHotSpot(xValue, yValue, xUnits, yUnits);
}
catch(NullPointerException e) {
Log.w(LOG_TAG,
"Missing 'x' or 'y' attributes in hotSpot for style with id: " + style.getStyleId());
}
catch(NumberFormatException e) {
Log.w(LOG_TAG, "Invalid number in 'x' or 'y' attributes in hotSpot for style with id: " + style.getStyleId());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ public void testEmptyHotSpotStyle() throws Exception {
KmlParser mParser = new KmlParser(parser);
mParser.parseKml();
assertNotNull(mParser.getPlacemarks());
assertEquals(1, mParser.getPlacemarks().size());
assertEquals(2, mParser.getPlacemarks().size());
HashMap<String, KmlStyle> styles = mParser.getStyles();
assertEquals(3, styles.size());
KmlStyle validStyle = styles.get("#validHotspot");
assertNotNull(validStyle);
double hotspotX = validStyle.getMarkerOptions().getAnchorU();
double hotspotY = validStyle.getMarkerOptions().getAnchorV();
assertEquals(0.5234, hotspotX, 0.0001);
assertEquals(0.5062, hotspotY, 0.0001);
}

@Test
Expand Down
31 changes: 28 additions & 3 deletions library/src/test/resources/amu_empty_hotspot.kml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml>
<Style x="20" y="2" xunits="pixels" yunits="pixels" id="shp_centerpoint">
<Style id="emptyHotspot">
<IconStyle>
<scale>0.8</scale>
<Icon>
Expand All @@ -12,17 +12,42 @@
<scale>1</scale>
<color>FFFFFFFF</color>
</LabelStyle>
</Style>
<Style id="validHotspot">
<IconStyle>
<scale>0.8</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pal4/icon57.png</href>
</Icon>
<hotSpot x="0.5234" y="0.5062" xunits="fraction" yunits="fraction"/>
</IconStyle>
<LabelStyle>
<scale>1</scale>
<color>FFFFFFFF</color>
</LabelStyle>
</Style>
<Placemark>
<name>60A</name>
<description>Test</description>
<description>Empty Hotspot</description>
<visibility>1</visibility>
<Point>
<extrude>0</extrude>
<tessellate>1</tessellate>
<altitudeMode>0</altitudeMode>
<coordinates>-111.957935372607,44.0811954480368,0</coordinates>
</Point>
<styleUrl>#shp_centerpoint</styleUrl>
<styleUrl>#emptyHotspot</styleUrl>
</Placemark>
<Placemark>
<name>60B</name>
<description>Valid Hotspot</description>
<visibility>1</visibility>
<Point>
<extrude>0</extrude>
<tessellate>1</tessellate>
<altitudeMode>0</altitudeMode>
<coordinates>-112.057935372607,44.1811954480368,0</coordinates>
</Point>
<styleUrl>#validHotspot</styleUrl>
</Placemark>
</kml>

0 comments on commit 643d351

Please sign in to comment.