I am using Java API for KML, JAK, to construct KML files. I would like to be able to delete a feature using its ID, but I have not found a good example of how to do so. Ideally, the code would be "myFolder.deleteFeatureById(theID);", but that is not the case. Is there any better method than the following?
List<Feature> features = myFolder.getFeature();
for(int i=features.size()-1; i>=0; i--)
{
if(features.get(i).getId() == "myId")
{
features.remove(i);
break;
}
}
In Java you need to compare strings using String.equals() method not the logical ==
operator.
==
checks if two things are EXACTLY the same thing, not if they have the same content so some string comparisons can be equal (same string) but test differently with ==
.
The following should work.
List<Feature> features = myFolder.getFeature();
for(int i=features.size()-1; i >= 0; i--)
{
if("myId".equals(features.get(i).getId()))
{
features.remove(i);
break;
}
}
Here example code using JAK API that creates two placemarks in a folder then removes one by its id.
final Kml kml = new Kml();
final Folder folder = new Folder();
kml.setFeature(folder);
folder.setName("Folder.kml");
folder.setOpen(true);
final Placemark placemark1 = new Placemark().withId("1")
.withName("Folder object 1 (Placemark)");
folder.getFeature().add(placemark1);
final Placemark placemark2 = new Placemark().withId("2")
.withName("Folder object 2 (Placemark)");
folder.getFeature().add(placemark2);
List<Feature> features = folder.getFeature();
System.out.println(features); // dumps two features
for(int i=features.size()-1; i >= 0; i--)
{
Feature f = features.get(i);
if("1".equals(f.getId()))
{
// this removes feature with id = "1"
features.remove(i);
break;
}
}
System.out.println(features); // folder now only has one item
Related details on this issue:
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.