我使用下面的程序创建了一个KMZ文件,在我的maven项目中,我在项目文件夹下创建了一个名为files
的文件夹,在文件文件夹中添加了一个名为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");
发布于 2020-06-10 19:44:32
Google (GEP)首先查找KMZ文件中作为相对URI引用的文件(例如,file /grn-pupin.png),如果可用的话使用它。
如果在KMZ文件中找不到图像引用作为条目,则GEP将使用相同的路径在本地文件系统中查找相对于KMZ文件的那些文件,因此如果kmz文件out.kmz位于C:/path/data/
中,那么它将查找相对于C:/path/data/files/grn-pushpin.png
中该文件夹的图像。
要验证KMZ中的引用文件是否正在加载,请将out.kmz文件移动到另一个文件夹(例如桌面),并在Google中打开它。
https://stackoverflow.com/questions/62275429
复制相似问题