简体   繁体   中英

JAK generated KML not work with Google My Maps

I write manually a KML file trying to import some polygons in MyMaps. This way works fine:

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://earth.google.com/kml/2.0">
    <Document>
        <Placemark> 
            <Style> 
                <PolyStyle>  
                    <color>#a00000ff</color>
                    <outline>0</outline>
                </PolyStyle> 
            </Style>
            <Polygon>
                <outerBoundaryIs>
                    <LinearRing>  
                        <coordinates>9.184254,45.443636 9.183379,45.434288 9.224836,45.431499 9.184254,45.443636</coordinates>
                    </LinearRing>
                </outerBoundaryIs>
            </Polygon>
        </Placemark>
    </Document>
</kml>

I try to write a java program using JAK that generate a most possibile equal file, but it doesn't work with Maps

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:kml xmlns:atom="http://www.w3.org/2005/Atom" xmlns:ns3="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
    <ns3:Document>
        <ns3:Placemark>
            <ns3:Style>
                <ns3:PolyStyle>
                    <ns3:color>#EABCFF</ns3:color>
                    <ns3:outline>0</ns3:outline>
                </ns3:PolyStyle>
            </ns3:Style>
            <ns3:Polygon>
                <ns3:innerBoundaryIs>
                    <ns3:LinearRing>
                        <ns3:coordinates>9.184254,45.443636 9.183379,45.434288 9.224836,45.431499 9.184254,45.443636</ns3:coordinates>
                    </ns3:LinearRing>
                </ns3:innerBoundaryIs>
            </ns3:Polygon>
        </ns3:Placemark>
    </ns3:Document>
</ns3:kml>

That's program:

public static void main(String[] args) throws IOException {
    // Style
    PolyStyle polystyle = KmlFactory.createPolyStyle();
    polystyle.setColor("#EABCFF");
    // polystyle.setFill(true);
    polystyle.setOutline(false);
    //
    Kml kml = KmlFactory.createKml();
    Document document = kml.createAndSetDocument();
    Placemark pm = document.createAndAddPlacemark();
    LinearRing linearRing = pm.createAndSetPolygon().createAndAddInnerBoundaryIs().createAndSetLinearRing();
    linearRing.addToCoordinates(9.184254, 45.443636, 0);
    linearRing.addToCoordinates(9.183379, 45.434288, 0);
    linearRing.addToCoordinates(9.224836, 45.431499, 0);
    linearRing.addToCoordinates(9.184254, 45.443636, 0);
    pm.createAndAddStyle().setPolyStyle(polystyle);
    //
    kml.marshal(new FileWriter("D:/prova.kml"));
}

I view <ns3: in your kml this make the kml invalid for google maps

Try to correct the file

I had the same problem.

Instead of using kml.marshal(new FileWriter("D:/prova.kml")); I did this...

        String name = kml.getClass().getSimpleName();
        if ("Kml".equals(name)) {
            name = name.toLowerCase();
        }

        JAXBContext jaxbContext = JAXBContext.newInstance(Kml.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NameSpaceBeautyfier());
        JAXBElement<Kml> jaxbKml = new JAXBElement<>(new QName("http://www.opengis.net/kml/2.2", name), (Class<Kml>) kml.getClass(), kml);
        jaxbMarshaller.marshal(jaxbKml, file);

With a NameSpaceBeautifier like this ...

private static final class NameSpaceBeautyfier extends NamespacePrefixMapper {

    private static final String KML_PREFIX = ""; // DEFAULT NAMESPACE
    private static final String KML_URI= "http://www.opengis.net/kml/2.2";

    @Override
    public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
        if(KML_URI.equals(namespaceUri)) {
            return KML_PREFIX;
        }
        return suggestion;
    }

    @Override
    public String[] getPreDeclaredNamespaceUris() {
        return new String[] { KML_URI };
    }

    private NameSpaceBeautyfier() {
    }
}

Hope this helps..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM