2010-11-29 5 views
0

私はkmlファイルを解析して書き込み、sd cardに保存しているプロジェクトで作業していますが、私は問題があります.Googleマップは座標がすべて一緒に必要なパスをトレースするため、ActivityAndroidでのKMF解析

<?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> 

そして、私のメインクラス::ちょうどこの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()); 
     } 
} 

何?あなたは私は座標がすべて 一緒

座標が<coordinates>タグにすべて一緒にいることを必要とする

で何を意味するか

答えて

1

わからない、スポットごとに3つの値(緯度、経度、高さ)。ここで

がkmlファイルを読み込み、それに基づいてマップのパスを描画する方法完全なサンプルです:

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

0

パスを描画するには、 はどのようなことをされているのと同じ目印とラインストリング内のすべてのポイントを置きますあなたはしたいですか?問題はあまり明確ではありません。

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