3

I'm working on a project that involves KML creation using Java. Currently, I'm fooling with the sample Java code from the KML example at Micromata Labs JAK Example. I tried to "extend" the code by adding multiple coordinates and getting two markers, but I could not get it to work. Can you please tell me how I can add multiple coordinates and put markers on them, and also, draw a line between the markers. Thank you for your help!

PS: I need to do this via the program. I saw sample code of them using DOM and XML, but not pure Java/JAK as such. Please guide me.

I got as far as this (updated):

kml.createAndSetDocument().withName("MyMarkers")
.createAndAddPlacemark().withName("London, UK").withOpen(Boolean.TRUE)  
.createAndSetPoint().addToCoordinates(-0.126236, 51.500152);    

kml.createAndSetDocument().withName("MyMarkers")  
.createAndAddPlacemark().withName("Somewhere near London,UK").withOpen(Boolean.TRUE)
.createAndSetPoint().addToCoordinates(-0.129800,52.70‌​0152);

But I know I'm going wrong somewhere. Please point me in the right direction.

Here is the resulting KML output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
<Document>
    <name>MyMarkers</name>
    <Placemark>
        <name>Somewhere near London, UK</name>
        <open>1</open>
        <Point>
            <coordinates>-0.1298,52.700152</coordinates>
        </Point>
    </Placemark>
</Document>
</kml>

I can't seem to access the Document again to add more placemarks. How do I do it?

CodingInCircles
  • 2,565
  • 11
  • 59
  • 84

3 Answers3

6

Basically, you need to do:

Document document = kml.createAndSetDocument().withName("MyMarkers");

document.createAndAddPlacemark().withName("London, UK").withOpen(Boolean.TRUE)  
    .createAndSetPoint().addToCoordinates(-0.126236, 51.500152);    

document.createAndAddPlacemark().withName("Somewhere near London,UK").withOpen(Boolean.TRUE)
    .createAndSetPoint().addToCoordinates(-0.129800,52.70‌​0152);

Previously, you were creating a new document and setting it (as the only document!) in the kml object. Therefore, only the last entry was shown.

Markus
  • 613
  • 1
  • 7
  • 20
3

To put more than one Placemark in a KML file you need a Folder or a Document

A basic <kml> element contains 0 or 1 Feature

A Feature is an abstract element that can be a Placemark.

A Container extends Feature and can be a Document or a Folder

To make a long story short, if you want multiple Placemarks, you need to include them in a Document or a Folder

<kml>
  <Document>
    <Placemark>
    </Placemark>
    ...
    <Placemark>
    </Placemark>
  </Document>
</kml>
geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Right, but that is pure KML. I want to create the structure of the KML in Java. And as you can see, I tried doing that. I added a new Document, and created a Placemark. How do I add the second one in the code? – CodingInCircles Oct 03 '12 at 04:08
  • you need to add the second placemark to the document element, so the resulting kml has the structure as specified above, currently you are adding it to the kml element, not the document element. – geocodezip Oct 03 '12 at 05:09
  • I see. I will try it out and let you know if and when I go wrong with it.. Thanks! :) – CodingInCircles Oct 03 '12 at 06:01
  • Please update your question with your latest version, it is very difficult to read code posted in comments. TO me it looks like you are now creating two documents, both with the name "MyMarkers". Posting the resulting kml would be helpful as well. – geocodezip Oct 03 '12 at 07:47
0

The documentation is very bad.

        final Kml kml = new Kml();
        Document document = kml.createAndSetDocument();
        listForms = formDAO.getAll();
        for (Form list : listForms){
            document.createAndAddPlacemark()
            .withName(String.valueOf(list.getId()))
            .withDescription(list.toStringKML())
            .createAndSetPoint().addToCoordinates(-20.3978398, -43.5146653);
        }
        kml.setFeature(document);
        kml.marshal(new File("test.kml"));
Guilherme
  • 89
  • 4
  • 9