Pergunta

I'm currently using JAK (Java API for KML) to interact with Google Earth and a customized KML file. I'm able to get/set the name, description, coordinates of a placemark using things like Placemark p.getName(), or point.getCoordinates(); into a list, etc. But what I'm having trouble with is getting the url of the image used for the icon. So for example, if my kml file has this placemark in it (contained by a Document, and then the overall KML tag):

  <Placemark>
    <name>Isla de Roatan</name>
    <description>
       Cruise Stop        
    </description>
    <Style>
        <IconStyle>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/airports.png</href>
            </Icon>
        </IconStyle>
    </Style>
    <Point>
      <coordinates>-86.53,16.337461,0</coordinates>
    </Point>
  </Placemark>

How can I grab that png url to say, put in a separate String object? I've seen within Style the .getIconStyle, and within IconStyle, the .getIcon, and within Icon, the .getHttpQuery, but nothing to link looking into the Style from the Placemark/Feature, except for .getStyleSelector and .getStyleUrl. Can you do it with one of those or a Style Map? I'm not sure if I grasp completely what each of these does. Additionally in reverse, what can be done to set this URL? Thanks for any help!

Foi útil?

Solução

Feature.getStyleSelector() returns a List<StyleSelector>. Style is a subclass of StyleSelector, so your Style should be in this list (along with any other Styles and StyleMaps defined for the Feature).

Setting style (and icon URL):

Placemark placemark = ...;

Style myStyle = new Style().withId("my_style");
myStyle.withIconStyle(new IconStyle().withIcon(new Icon().withHref("http://someurl")));

placemark.addToStyleSelector(myStyle);

Getting style (and icon URL):

for (StyleSelector styleSelector : placemark.getStyleSelector())
{
    if (styleSelector.getId() == "my_style")
    {
        String href = ((Style)styleSelector).getIconStyle().getIcon().getHref();
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top