转载请注明出处:http://blog.csdn.net/hnyzwtf/article/details/51956714
请先阅读:

  • Java(Android)解析KML文件
  • Java生成kml文件

这里就直接给出代码了

package com.soil.soilsampling.ui.parsekml;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Log;import com.soil.soilsampling.R;import com.soil.soilsampling.base.BaseApplication;import com.soil.soilsampling.model.CoordinateAlterSample;import com.soil.soilsampling.support.utils.ToastUtil;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.XMLWriter;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;/** * Created by GIS on 2016/5/19 0019. *  */public class WriteKml {    private String TAG = "WriteKml";    /*    * 传入两个参数,一是kml的名称,第二个是坐标点的list    * */    public void createKml(String kmlName, List alterSamples) throws Exception    {        Element root = DocumentHelper.createElement("kml");  //根节点是kml        Document document = DocumentHelper.createDocument(root);        //给根节点kml添加属性        root.addAttribute("xmlns", "http://www.opengis.net/kml/2.2")                .addAttribute("xmlns:gx", "http://www.google.com/kml/ext/2.2")                .addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")                .addAttribute("xsi:schemaLocation",                        "http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd");        //给根节点kml添加子节点  Document        Element documentElement = root.addElement("Document");        documentElement.addElement("name").addText(kmlName); //添加name节点        documentElement.addElement("Snippet").addText(""); //Snippet节点        Element folderElement = documentElement.addElement("Folder");//Folder节点        folderElement.addAttribute("id", "FeatureLayer0");        //给Folder节点添加子节点        folderElement.addElement("name").addText(kmlName);        folderElement.addElement("Snippet").addText("");        //循环添加每一个Placemark节点,有几个坐标点就有几个Placemark节点        for (int i = 0; i < alterSamples.size(); i++) {            Element placeMarkElement = folderElement.addElement("Placemark");            placeMarkElement.addAttribute("id", alterSamples.get(i).getName());            placeMarkElement.addElement("name").addText(alterSamples.get(i).getName());            placeMarkElement.addElement("Snippet").addText("");            placeMarkElement.addElement("description").addCDATA(getCdataContent(alterSamples.get(i).getName(),                    alterSamples.get(i).getName(), String.valueOf(alterSamples.get(i).getX()), String.valueOf(alterSamples.get(i).getY()),                    alterSamples.get(i).getCostValue()));            placeMarkElement.addElement("styleUrl").addText("#IconStyle00");            Element pointElement = placeMarkElement.addElement("Point");            pointElement.addElement("altitudeMode").addText("clampToGround");            //添加每一个坐标点的经纬度坐标            //pointElement.addElement("coordinates").addText("119.39986000,31.13396700000143,0");            pointElement.addElement("coordinates").addText(String.valueOf(alterSamples.get(i).getX()) + "," +                    String.valueOf(alterSamples.get(i).getY()) + "," + "0");        }        Element styleElement = documentElement.addElement("Style");//Style节点        styleElement.addAttribute("id", "IconStyle00");        // IconStyle        Element iconStyleElement = styleElement.addElement("IconStyle");        Element iconElement = iconStyleElement.addElement("Icon");        iconElement.addElement("href").addText("layer0_symbol.png");        iconStyleElement.addElement("scale").addText("0.250000");        // LabelStyle        Element labelStyleElement = styleElement.addElement("LabelStyle");        labelStyleElement.addElement("color").addText("00000000");        labelStyleElement.addElement("scale").addText("0.000000");        // PolyStyle        Element polyStyleElement = styleElement.addElement("PolyStyle");        polyStyleElement.addElement("color").addText("ff000000");        polyStyleElement.addElement("outline").addText("0");        //将生成的kml写出本地        OutputFormat format = OutputFormat.createPrettyPrint();        format.setEncoding("utf-8");//设置编码格式        //将doc.kml写入到/data/data//files目录        FileOutputStream outputStream = BaseApplication.getContext().openFileOutput("doc.kml", Context.MODE_PRIVATE);        XMLWriter xmlWriter = new XMLWriter(outputStream,format);        xmlWriter.write(document);        xmlWriter.close();        //开始对文件进行压缩,一个kml文件其实是一个压缩文件,里面包含一个kml文件和一个png图标        String docKmlPath = BaseApplication.getContext().getFilesDir().getAbsolutePath() + "//doc.kml";        zipWriteKml(docKmlPath, kmlName);        ToastUtil.show(BaseApplication.getContext(), "导出kml成功");    }    /*    * 将生成的kml文件和drawable下的某个png图标进行压缩,生成最终的kml文件,并保存在/data/data//files目录    * */    public void zipWriteKml(String docKmlPath, String kmlName) throws IOException    {        // 最终生成的kml文件        FileOutputStream fileOutput = BaseApplication.getContext().openFileOutput(kmlName + ".kmz", Context.MODE_PRIVATE);        OutputStream os = new BufferedOutputStream( fileOutput);        ZipOutputStream zos = new ZipOutputStream(os);        byte[] buf = new byte[8192];        int len;        //压缩data/data/package name/files目录下的doc.kml        File file = new File(docKmlPath);        if ( !file.isFile() )            Log.d(TAG, "doc.kml is nonexist");        ZipEntry ze = new ZipEntry( file.getName() );        zos.putNextEntry( ze );        BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) );        while ( ( len = bis.read( buf ) ) > 0 ) {            zos.write( buf, 0, len );        }        zos.closeEntry();        // 压缩drawable目录下的图片        Resources r = BaseApplication.getContext().getResources();        Bitmap bitmap = BitmapFactory.decodeResource(r, R.drawable.layer0_symbol);        ByteArrayOutputStream baos = new ByteArrayOutputStream();        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);        InputStream input = new ByteArrayInputStream(baos.toByteArray());        int temp = 0;        ZipEntry entry2 = new ZipEntry("layer0_symbol.png");        zos.putNextEntry(entry2);        while ((temp = input.read()) != -1)        {            zos.write(temp);        }        input.close();        zos.closeEntry();       /* for (int i=0;i < files.length;i++) {            File file = new File(files[i]);            if ( !file.isFile() )                continue;            ZipEntry ze = new ZipEntry( file.getName() );            zos.putNextEntry( ze );            BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) );            while ( ( len = bis.read( buf ) ) > 0 ) {                zos.write( buf, 0, len );                Log.d(TAG, "we are zipping "+ file.getName());            }            zos.closeEntry();        }*/        zos.closeEntry();        zos.close();    }    /*    * 生成kml的html备注,在description节点下    * */    public String getCdataContent(String id, String placeMarkName, String x, String y, String costValue)    {        StringBuffer buffer = new StringBuffer();        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("");        buffer.append("
").append(placeMarkName).append("
"); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append("
ID").append(id).append("
name").append(placeMarkName).append("
X").append(x).append("
Y").append(y).append("
CostValue").append(costValue).append("
"
); buffer.append("
"
); buffer.append(""); buffer.append(""); String cDataContent = buffer.toString(); return cDataContent; }}

CoordinateAlterSample类如下

/* * 服务器返回的样点,每一个样点包括一个name,x,y,costValue * */public class CoordinateAlterSample implements Serializable {    private double x;    private double y;    private String name;    private String costValue;    public String getCostValue() {        return costValue;    }    public void setCostValue(String costValue) {        this.costValue = costValue;    }    public double getX() {        return x;    }    public void setX(double x) {        this.x = x;    }    public double getY() {        return y;    }    public void setY(double y) {        this.y = y;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

更多相关文章

  1. android中读写sd卡文件
  2. Android 支持的文件类型
  3. Android下载文本文件和mp3文件
  4. 使用Android系统自带的icon图标
  5. android project 文件夹
  6. Android 多国语言文件夹
  7. android shape用法(xml文件)

随机推荐

  1. Python处理字符串
  2. 在管理页面中编辑M2M的两面
  3. 5月28日 python学习总结 CSS学习(二)
  4. 用python阐释工作量证明(proof of work)
  5. 008 Python基本语法元素小结
  6. 在python 3.3列表中查找最小值
  7. 用 Python requests库 爬取网页数据
  8. 用Python和Pygame写游戏-从入门到精通(20)
  9. 是否可以使用argparse来捕获任意一组可选
  10. python题目——认识*与**,判断函数输出