Creating kml-file, multiple placemarks

267 views
Skip to first unread message

Michi Gorkow

unread,
Dec 5, 2012, 6:26:14 PM12/5/12
to javaap...@googlegroups.com
Hi Guys,

I am working on a Java-tool to create a kml-file containing some placemarks.
The placemark-data comes from a csv and it works perfectly.
The only problem is, that the kml-file only contains the last entry of my csv. The other placemarks are overwritten...
Can someone help me?

Thanks!

public void createKML() throws IOException{
        int zeile = 0;
       
        Kml kml = new Kml();
        kml.createAndSetDocument();
        CSVReader reader = new CSVReader(new FileReader("kmlVorlage.csv"), ';');
        String [] nextLine;   
            //extendedData
            while ((nextLine = reader.readNext()) != null) {
                List<Data> list = new ArrayList<Data>();
                ExtendedData extendedData = new ExtendedData();
           
            String name = retkundennummer(String.valueOf(zeile));
            String kundennummer = retname(String.valueOf(zeile));
            String land = retland(String.valueOf(zeile));
            String postleitzahl = retpostleitzahl(String.valueOf(zeile));
            String ort =retort(String.valueOf(zeile));
            String location = retlocation(String.valueOf(zeile));
            String notizen =retnotizen(String.valueOf(zeile));
            String image =retimage(String.valueOf(zeile));
           
            Data dname = new Data("");
            dname.setName("Kundennummer:");
            dname.setValue(name);

            Data dland = new Data("");
            dland.setName("Land:");
            dland.setValue(land);

            Data dort = new Data("");
            dort.setName("Ort:");
            dort.setValue(ort);

            Data dpostleitzahl = new Data("");
            dpostleitzahl.setName("Postleitzahl:");
            dpostleitzahl.setValue(postleitzahl);

            Data dnotizen = new Data("");
            dnotizen.setName("Notizen:");
            dnotizen.setValue(notizen);

            Data dimage = new Data("");
            dimage.setName("Image:");
            dimage.setValue(image);
           
            list.add(dname);
            list.add(dland);
            list.add(dort);
            list.add(dpostleitzahl);
            list.add(dnotizen);
            //list.add(dimage);
            extendedData.withData(list);
            //Ende extendedData
            kml.createAndSetPlacemark()
            .withName(kundennummer)
            .withStyleUrl("#BasicStyle")
            .withDescription(notizen)
            .withExtendedData(extendedData)
            .withAddress(location);
            zeile = zeile +1;
            System.out.println("test");
        }
            kml.marshal(new File("HelloKml.kml"));
    }

Benjamin Cramet

unread,
Dec 8, 2012, 9:29:55 AM12/8/12
to javaap...@googlegroups.com
Try to declare the list of Data and extendedData outside of the loop and to instanciate them in the loop.
Something like this:
      String [] nextLine;   
       //extendedData
       List<Data> list = null;
       ExtendedData extendedData = null;
       Document d = kml.createAndSetDocument().withName("FromCSVFile").withOpen(true);
     
       Placemark p = null;      
       while ((nextLine = reader.readNext()) != null) {
           list = new ArrayList<Data>();
           extendedData = new ExtendedData();
           p = new Placemark();

           [...]
           d.getFeature().add(p);
       }

I do this with a db not a csv.
If this does not work send me the csv file.

Regards,

Benjamin
Reply all
Reply to author
Forward
0 new messages