Android 中的 Kml 解析

发布于 2024-10-05 18:26:24 字数 4491 浏览 0 评论 0原文

我正在开发一个项目,该项目解析并编写一个 kml 文件并存储在 sd 卡 中,但我有一个问题,因为谷歌地图跟踪我需要的路径坐标都在一起,我的 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());
      }
}

我做错了什么?

I'm working in a project that parses and write a kml file and stores in sd card, but I have a problem, for google maps trace the path I need that the coordinates are all together, and my Activity just do like this kml output:

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

And My Main Class:

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());
      }
}

What I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-10-12 18:26:24

不确定你的意思

我需要坐标全部
在一起

标签中,每个点有 3 个值(纬度、经度、高度)。

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

如何使用kml文件在地图上绘制路径?

Not sure what you mean with

i need that the coordinates are all
together

The coordinates are all together in the <coordinates> tag, 3 values per spot (latitude, longitude, height).

Here's a complete sample how to read the kml file and draw the map path based on it:

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

偷得浮生 2024-10-12 18:26:24

要绘制路径,请将所有点放在同一个地方标记和线串
这就是你想做的吗?问题不是很清楚。

例如

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

To draw a path, Put all points in the same placeMark and LineString
Is that what you want to do? The question is not very clear.

e.g.

<PlaceMark>
...
 <LineString>
    ...
    <coordinates>
     -7.178449630737305,41.48063600063324,274.0
  ... append other points here ....
    </coordinates>
 </LineString>
</PlaceMark>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文