2015-05-29 31 views
0

我手動編寫了一個試圖在MyMaps中導入某些多邊形的KML文件。這種方式工作得很好:JAK生成的KML不適用於Google我的地圖

<?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> 

我嘗試寫使用JAK生成一個最不可能性相等文件中的Java程序,但它不與地圖

<?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> 

這是程序工作:

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")); 
} 
+1

哪裏了'NS3: '從哪裏來? – geocodezip

+0

Hello @ stefano-r。你有沒有找到解決你的問題?我遇到同樣的問題,但我有ns2前綴。找不到擺脫它的方法。 – Surge

+0

nope,我不得不手動編輯文件。 –

回答

0

我認爲<ns3:在你的kml這使得谷歌地圖的kml無效

嘗試更正文件

+0

我不能或者我不知道如何在JAK結構中做! –

+0

試試這個http://labs.micromata.de/projects/jak/quickstart.html,然後重構你的代碼 – scaisEdge

0

我遇到了同樣的問題。

而不是使用kml.marshal(new FileWriter("D:/prova.kml"));我這樣做的......

 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); 

有了這樣的NameSpaceBeautifier ...

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() { 
    } 
} 

希望這有助於..