我用下面的程序创建了一个KMZ文件,在我的maven项目中,我创建了一个文件夹,叫做 files
在project文件夹下,我添加了一个叫做 grn-pushpin.png
文件夹中。
在我的程序中,当创建KMZ时,我已经通过了我的图像,如下所示
FileInputStream is = new FileInputStream("files/grn-pushpin.png");
ZipEntry zEnt = new ZipEntry("files/grn-pushpin.png");
在KML中显示点图像时,我给出了像 ps.println("<Icon><href>files/grn-pushpin.png</href></Icon>");
现在它显示的图像,但它似乎是显示从本地文件夹只。
我怎样才能确定图像是来自KMZ文件?
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
import java.io.*;
public class TestKmz {
public static void main(String[] args) throws IOException {
createKMZ();
System.out.println("file out.kmz created");
}
public static void createKMZ() throws IOException {
FileOutputStream fos = new FileOutputStream("out.kmz");
ZipOutputStream zoS = new ZipOutputStream(fos);
ZipEntry ze = new ZipEntry("doc.kml");
zoS.putNextEntry(ze);
PrintStream ps = new PrintStream(zoS);
ps.println("<?xml version='1.0' encoding='UTF-8'?>");
ps.println("<kml xmlns='http://www.opengis.net/kml/2.2'>");
// write out contents of KML file ...
ps.println("<Placemark>");
// add reference to image via inline style
ps.println(" <Style><IconStyle>");
ps.println(" <Icon><href>files/grn-pushpin.png</href></Icon>");
ps.println(" </IconStyle></Style>");
ps.println(" <Point><coordinates>72.877460,19.144808</coordinates></Point> ");
ps.println("</Placemark>");
ps.println("</kml>");
ps.flush();
zoS.closeEntry(); // close KML entry
// now add image file entry to KMZ
FileInputStream is = null;
try {
is = new FileInputStream("files/grn-pushpin.png");
ZipEntry zEnt = new ZipEntry("files/grn-pushpin.png");
zoS.putNextEntry(zEnt);
// copy image input to KMZ output
// write contents to entry within compressed KMZ file
IOUtils.copy(is, zoS);
} finally {
IOUtils.closeQuietly(is);
}
zoS.closeEntry();
zoS.close();
}
}
我已经删除了下面的代码行,我仍然能够看到图像,这意味着它是从文件夹加载,它不是从KMZ文件读取。
is = new FileInputStream("files/grn-pushpin.png");
ZipEntry zEnt = new ZipEntry("files/grn-pushpin.png");
Google Earth Pro (GEP)首先在KMZ文件中寻找相对URI引用的文件(如filesgrn-pushpin.png),如果可用就使用它。
如果图像引用没有找到作为KMZ文件内的条目,那么GEP将寻找这些文件在本地文件系统中相对于KMZ文件使用相同的路径,所以如果kmz文件out.kmz是位于 C:/path/data/
然后,它将在 C:/path/data/files/grn-pushpin.png
.
要验证KMZ内的引用文件是否被加载,请将out.kmz文件移动到不同的文件夹(如桌面),并在Google Earth中打开它。