Android中的Kml解析

时间:2010-11-29 15:57:55

标签: android xml parsing kml

我正在一个项目中解析并编写kml文件并存储在sd card,但我有一个问题,因为谷歌地图跟踪我需要的路径,坐标全部在一起,我的Activity只是喜欢这个kml输出:

<?xml version="1.0"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
   <Placemark>
       <name>Untitled Path</name>
       <Style id="RedLine">
           <LineStyle>
               <color>7f0000ff</color>
               <width>4</width>
           </LineStyle>
        </Style>
        <styleUrl>#RedLine</styleUrl>
        <LineString>
             <tessellate>1</tessellate>
             <altitudeMode>absolute</altitudeMode>
             <coordinates>
                  -7.178449630737305,41.48063600063324,274.0
             </coordinates>
        </LineString>
   </Placemark>
   <Placemark>
        <name>Untitled Path</name>
        <Style id="RedLine">
             <LineStyle>
                  <color>7f0000ff</color>
                  <width>4</width>
             </LineStyle>
        </Style>
        <styleUrl>#RedLine</styleUrl>
        <LineString>
             <tessellate>1</tessellate>
             <altitudeMode>absolute</altitudeMode>
             <coordinates>
                 -7.178449630737305,41.48063600063324,274.0
             </coordinates>
        </LineString>
    </Placemark>
</kml>

我的主要班级:

private void WriteToFile(Location loc) {
    if (!logToGpx && !logToKml) {
        return;
    }
    try {
       boolean brandNewFile = false;
       // if (root.canWrite()){
       // File gpxFolder = new File("/sdcard/GPSLogger");
       File gpxFolder = new File(Environment.getExternalStorageDirectory(), "GPSLogger");
           Log.i("MAIN", String.valueOf(gpxFolder.canWrite()));
           if (!gpxFolder.exists()) {
               gpxFolder.mkdirs();
               brandNewFile = true;
           }
           if (logToGpx) {
                WriteToGpxFile(loc, gpxFolder, brandNewFile);
           }
           if (logToKml) {
                WriteToKmlFile(loc, gpxFolder, brandNewFile);
           }
    } catch (Exception e) {
        Log.e("Main", "Nao foi possivel criar o ficheiro " + e.getMessage());
        SetStatus("Nao e possivel escrever no ficheiro. " + e.getMessage());
    }
}

private void WriteToKmlFile(Location loc, File gpxFolder, boolean brandNewFile) {
     try {
         File kmlFile = new File(gpxFolder.getPath(), currentFileName + ".kml");
         if (!kmlFile.exists()) {
               kmlFile.createNewFile();
               brandNewFile = true;
         }
         Date now = new Date();
         //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
         //String dateTimeString = sdf.format(now);
         if (brandNewFile) {
              FileOutputStream initialWriter = new FileOutputStream(kmlFile, true);
              BufferedOutputStream initialOutput = new BufferedOutputStream(initialWriter);
              String initialXml = "<?xml version=\"1.0\"?>"+ "<kml xmlns=\"http://www.opengis.net/kml/2.2\">" + "</kml>";
              initialOutput.write(initialXml.getBytes());
              // initialOutput.write("\n".getBytes());
              initialOutput.flush();
              initialOutput.close();
         }

         long startPosition = kmlFile.length() - 6;

         String placemark = "<Placemark><name>" + now.toLocaleString()
             + "</name><description>" + now.toLocaleString()
             + "</description>" + "<Point><coordinates>"
             + String.valueOf(loc.getLongitude()) + ","
             + String.valueOf(loc.getLatitude()) + ","
             + String.valueOf(loc.getAltitude())
             + "</coordinates></Point></Placemark></kml>";

         RandomAccessFile raf = new RandomAccessFile(kmlFile, "rw");
         raf.seek(startPosition);
         raf.write(placemark.getBytes());
         raf.close();

      } catch (IOException e) {
          Log.e("Main", "Error in writting " + e.getMessage());
          SetStatus("Error in writting. " + e.getMessage());
      }
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

不确定你的意思

  

我需要坐标全部   一起

坐标全部在<coordinates>标签中,每个点3个值(纬度,经度,高度)。

以下是如何读取kml文件并根据它绘制地图路径的完整示例:

How to draw a path on a map using kml file?

答案 1 :(得分:0)

要绘制路径,请将所有点放在相同的placeMark和LineString中 那是你想做的吗?问题不是很清楚。

e.g。

<PlaceMark>
...
 <LineString>
    ...
    <coordinates>
     -7.178449630737305,41.48063600063324,274.0
  ... append other points here ....
    </coordinates>
 </LineString>
</PlaceMark>