KmlParser.Java
使用XMLSerializer创建KML文件,但不将其保存在android内部存储中。我不知道在创建文件还是仅保存在内部存储中是否有问题。任何人请帮助。可以节省我的时间。谢谢。
public class KmlParser {
private FileOutputStream fileos = null;
private XmlSerializer xmlSerializer;
File file;
public void writeKml(Context ctx, ArrayList<Double> coor_la, ArrayList<Double> coor_lo) {
//StringBuilder sb1 = new StringBuilder();
StringBuilder sb = new StringBuilder();
try {
File myDir = ctx.getDir("KML",Context.MODE_PRIVATE);
file = new File(myDir, "LandMap.kml");
//file.createNewFile();
//fileos = new FileOutputStream(file);
fileos = ctx.openFileOutput("LandMap.kml", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Here begins the KML creation
try {
xmlSerializer = XmlPullParserFactory.newInstance().newSerializer();
} catch (XmlPullParserException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
//Toast.makeText(PathGoogleMapActivity.this, "Error en serializer", Toast.LENGTH_SHORT).show();
}
try {
/* This KML file has a really basic structure, if you need
* styles and other fancy stuff, you are always free to add
* those style lines or anything you might need in order to
* make it look goof :) */
xmlSerializer.setOutput(fileos, "UTF-8");
xmlSerializer.startDocument(null, null);
xmlSerializer.startTag(null, "kml");
xmlSerializer.attribute(null, "xmlns", "http://www.opengis.net/kml/2.2");
xmlSerializer.startTag(null, "Document");
xmlSerializer.startTag(null, "name");
xmlSerializer.text("Land");
xmlSerializer.endTag(null, "name");
//Style1
xmlSerializer.startTag(null, "Style");
xmlSerializer.attribute(null, "id", "poly-000000-2000-77-nodesc-normal");
//Line Style
xmlSerializer.startTag(null, "LineStyle");
xmlSerializer.startTag(null, "color");
xmlSerializer.text("ff000000");
xmlSerializer.endTag(null, "color");
xmlSerializer.startTag(null, "width");
xmlSerializer.text("2");
xmlSerializer.endTag(null, "width");
xmlSerializer.endTag(null, "LineStyle");
//LineStyle
//PolyStyle
xmlSerializer.startTag(null, "PolyStyle");
xmlSerializer.startTag(null, "color");
xmlSerializer.text("4d000000");
xmlSerializer.endTag(null, "color");
xmlSerializer.startTag(null, "fill");
xmlSerializer.text("1");
xmlSerializer.endTag(null, "fill");
xmlSerializer.startTag(null, "outline");
xmlSerializer.text("1");
xmlSerializer.endTag(null, "outline");
xmlSerializer.endTag(null, "PolyStyle");
//PolyStlye
//BalloonStyle
xmlSerializer.startTag(null, "BalloonStyle");
xmlSerializer.startTag(null, "text");
xmlSerializer.text("<![CDATA[<h3>$[name]</h3>]]>");
xmlSerializer.endTag(null, "text");
xmlSerializer.endTag(null, "BalloonStyle");
//BalloonStyle
xmlSerializer.endTag(null, "Style");
//Style1
//Style2
xmlSerializer.startTag(null, "Style");
xmlSerializer.attribute(null, "id", "poly-000000-2000-77-nodesc-highlight");
//Line Style
xmlSerializer.startTag(null, "LineStyle");
xmlSerializer.startTag(null, "color");
xmlSerializer.text("ff000000");
xmlSerializer.endTag(null, "color");
xmlSerializer.startTag(null, "width");
xmlSerializer.text("3");
xmlSerializer.endTag(null, "width");
xmlSerializer.endTag(null, "LineStyle");
//LineStyle
//PolyStyle
xmlSerializer.startTag(null, "PolyStyle");
xmlSerializer.startTag(null, "color");
xmlSerializer.text("4d000000");
xmlSerializer.endTag(null, "color");
xmlSerializer.startTag(null, "fill");
xmlSerializer.text("1");
xmlSerializer.endTag(null, "fill");
xmlSerializer.startTag(null, "outline");
xmlSerializer.text("1");
xmlSerializer.endTag(null, "outline");
xmlSerializer.endTag(null, "PolyStyle");
//PolyStlye
//BalloonStyle
xmlSerializer.startTag(null, "BalloonStyle");
xmlSerializer.startTag(null, "text");
xmlSerializer.text("<![CDATA[<h3>$[name]</h3>]]>");
xmlSerializer.endTag(null, "text");
xmlSerializer.endTag(null, "BalloonStyle");
//BalloonStyle
xmlSerializer.endTag(null, "Style");
//Style2
//StyleMAp
xmlSerializer.startTag(null, "StyleMap");
xmlSerializer.attribute(null, "id", "poly-000000-2000-77-nodesc");
xmlSerializer.startTag(null, "Pair");
xmlSerializer.startTag(null, "key");
xmlSerializer.text("normal");
xmlSerializer.endTag(null, "key");
xmlSerializer.startTag(null, "styleUrl");
xmlSerializer.text("#poly-000000-2000-77-nodesc-normal");
xmlSerializer.endTag(null, "styleUrl");
xmlSerializer.endTag(null, "Pair");
xmlSerializer.startTag(null, "Pair");
xmlSerializer.startTag(null, "key");
xmlSerializer.text("highlight");
xmlSerializer.endTag(null, "key");
xmlSerializer.startTag(null, "styleUrl");
xmlSerializer.text("#poly-000000-2000-77-nodesc-highlight");
xmlSerializer.endTag(null, "styleUrl");
xmlSerializer.endTag(null, "Pair");
xmlSerializer.endTag(null, "StyleMap");
//StyleMap
//Placemark
xmlSerializer.startTag(null, "Placemark");
xmlSerializer.startTag(null, "name");
xmlSerializer.text("Land1");
xmlSerializer.endTag(null, "name");
xmlSerializer.startTag(null, "styleUrl");
xmlSerializer.text("#poly-000000-2000-77-nodesc");
xmlSerializer.endTag(null, "styleUrl");
xmlSerializer.startTag(null, "Polygon");
xmlSerializer.startTag(null, "outerBoundaryIs");
xmlSerializer.startTag(null, "LinearRing");
xmlSerializer.startTag(null, "tessellate");
xmlSerializer.text("1");
xmlSerializer.endTag(null, "tessellate");
xmlSerializer.startTag(null, "coordinates");
sb.setLength(0);
/*This is the part where I get the size of the path arraylist
* and start printing the coordinates for my path*/
int size = coor_lo.size();
for (int x = 0; x < size; x++) {
sb.append(Double.toString((Double) coor_lo.get(x)));
sb.append(",");
sb.append(Double.toString((Double) coor_la.get(x)));
sb.append(",0\n");
}
sb.setLength(sb.length() - 2);
String s = sb.toString();
Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show();
xmlSerializer.text(s);
xmlSerializer.endTag(null, "coordinates");
xmlSerializer.endTag(null, "LinearRing");
xmlSerializer.endTag(null, "outerBoundaryIs");
xmlSerializer.endTag(null, "Polygon");
xmlSerializer.endTag(null, "Placemark");
xmlSerializer.endTag(null, "Document");
xmlSerializer.endTag(null, "kml");
xmlSerializer.endDocument();
xmlSerializer.flush();
fileos.close();
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception", "Exception occured in writing");
}
}
}
我不知道创建KML文件是否正确。我只是在StackOverflow中阅读过。