需要注意
File file =new File(Uri.decode(uri.getEncodedPath()));
Document document = reader.read(file);
需要把本地文件路径先创建成文件类型然后传给Document
解析名称的时候需要注意gx:xxx 只需要 “:” 后面的名称
生成Kml方法 filePath 生成路径
public void createKml(String filePath) {
Element root = DocumentHelper.createElement("kml"); //根节点是kml
Document document = DocumentHelper.createDocument(root);
document.setXMLEncoding("UTF-8"); //给根节点kml添加属性
root.addNamespace("", "http://www.opengis.net/kml/2.2");
root.addNamespace("gx", "http://www.google.com/kml/ext/2.2"); //给根节点kml添加子节点
Element documentElement = root.addElement("Document");
Element folderDe = documentElement.addElement("Folder");
folderDe.addElement("Name").addText("Dome生成");
Element PlacemarkDe = folderDe.addElement("Placemark");
Element styleE = PlacemarkDe.addElement("Style");
Element LineStyle = styleE.addElement("LineStyle");
LineStyle.addElement("color").addText("ed0000ff");
LineStyle.addElement("width").addText("5");
PlacemarkDe.addElement("name").addText("运动轨迹");
Element trackE = PlacemarkDe.addElement("gx:Track");
for (int s=0;s < record.getPathline().size();s++){
LatLng tp = record.getPathline().get(s);
trackE.addElement("gx:coord").addText(tp.latitude + "," + tp.longitude);
trackE.addElement("when").setText(record.getmPathLineDatas().get(s));
}
try {
Writer fileWriter = new FileWriter(filePath); //换行
OutputFormat format = new OutputFormat();
format.setEncoding("UTF-8");
format.setNewlines(true); // 生成缩进
format.setIndent(true); //dom4j提供了专门写入文件的对象
XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
ToastUtils.showLong("生成成功");
} catch (IOException e) {
e.printStackTrace();
}
}
解析Kml方法
//解析kml.
mHandler.post(new Runnable() {
@Override
public void run() {
//查看文件类型
Intent intent=getIntent();
if (intent!=null){
String action = intent.getAction();
if(intent.ACTION_VIEW.equals(action)) {
LogUtils.v(intent.getDataString());
Uri uri = intent.getData();
LogUtils.v("url", Uri.decode(uri.getEncodedPath()).replace("/external_files/storage/emulated/0/", "/sdcard")+" "+Uri.decode(uri.getEncodedPath()).indexOf("/external_files/storage/emulated/0"));
URLConnection.getFileNameMap().getContentTypeFor(Uri.decode(uri.getEncodedPath()));
LogUtils.v("文件类型",URLConnection.getFileNameMap().getContentTypeFor(Uri.decode(uri.getEncodedPath())));
SAXReader reader = new SAXReader();
Document document = null;
try {
File file=null;
//小米 微信
if (Uri.decode(uri.getEncodedPath()).indexOf("/external_files/storage/emulated/0/")==-1){
file = new File(Uri.decode(uri.getEncodedPath()).replace("/external/", "/sdcard/"));
}else {
//小米 QQ
file = new File(Uri.decode(uri.getEncodedPath()).replace("/external_files/storage/emulated/0/", "/sdcard/"));
}
document = reader.read(file);
Element rootElement = document.getRootElement();
Element responseDocument = rootElement.element("Document").element("Folder").element("Placemark");
Element responseTrack = responseDocument.element("Track");
List listCoord = responseTrack.elements("coord");
List listWhen = responseTrack.elements("when");
PathRecord pathRecord = new PathRecord();
for (int i = 0; i < listCoord.size(); i++) {
Element element = (Element) listCoord.get(i);
Element element2 = (Element) listWhen.get(i);
String[] lat_lng = element.getText().split(",");
LatLng latLng = new LatLng(Double.parseDouble(lat_lng[0]),
Double.parseDouble(lat_lng[1]));
LogUtils.v(Double.parseDouble(lat_lng[0])+" "+Double.parseDouble(lat_lng[1]));
pathRecord.addpoint(latLng);
pathRecord.addData(element2.getText());
}
aMap.addPolyline(new PolylineOptions().addAll(pathRecord.getPathline()).width(10).color(Color.argb(255, 1, 1, 1)));
}catch (DocumentException e) {
e.printStackTrace();
}
}
}
}
});
我的这个需求是从微信和qq用其他应用打开 然后解析到地图上
把应用添加到微信,QQ用其他应用打开列表 把下面代码放到第一个打开的Activity 下面
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" tools:ignore="AppLinkUrlError" />
<data android:scheme="content" />
<data android:mimeType="application/vnd.google-earth.kml+xml" />
</intent-filter>
网友评论