读取kml文件

发布于 10-03 17:29 字数 80 浏览 6 评论 0原文

我下载了一个 KML 文件..有没有办法使用 javascript/php 循环遍历该文件并获取 KML 文件中的所有坐标?

谢谢

i download a KML file..Is there any way to use javascript/php to loop through the file and get all the coordinates in the KML file?

Thanks

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

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

发布评论

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

评论(3

↙厌世2024-10-10 17:29:56

您可以使用 jquery ajax 来读取/循环文件。就像普通的 XML 一样。

有关使用 jquery 的更多信息。 http://api.jquery.com/jQuery.ajax/

You can use jquery ajax to read/loop the file. Just like normal XML.

More info for using jquery. http://api.jquery.com/jQuery.ajax/

红衣飘飘貌似仙2024-10-10 17:29:56

如果 KML 文件大小合适,那么在将其发送到用户浏览器之前首先将其转换为 JSON(以 PHP 形式)可能是非常值得的。 JSON 将产生较小的文件大小并且解析速度更快(这可能带来边际效益如果 KML 文件很小,但如果很复杂则非常明显)。

// PHP:
$json = json_encode(simplexml_load_string($xml_string));

If the KML file is of a decent size, then it could be very worthwhile to first convert it to JSON (in PHP) before sending it to the user's browser. JSON will produce a lower filesize and also be faster to parse (which could be of marginal benefit if the KML file is small, but very noticeable if it's complex).

// PHP:
$json = json_encode(simplexml_load_string($xml_string));
如梦初醒的夏天2024-10-10 17:29:56

我必须做与OP类似的事情。对于这样的数据(来自 https://developers.google.com/kml/documentation/kml_tut< /a>):

<kml xmlns="http://www.opengis.net/kml/2.2">
  <Placemark>
    <name>Simple placemark</name>
    <description>Attached to the ground. Intelligently places itself 
       at the height of the underlying terrain.</description>
    <Point>
      <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
    </Point>
  </Placemark>
</kml>

我这样做了:

$xml = simplexml_load_string($data);

foreach($xml->Placemark as $Placemark){
    $name = $Placemark->name;
    echo $name."<br>";

    if ($Placemark->Point->coordinates == null) {
        continue;
    } else {
        $coordinates = explode(",", $Placemark->Point->coordinates);
        echo $coordinates[1].",".$coordinates[0]."<br>";
    }
}

I had to do similar to the OP. For data like this (from https://developers.google.com/kml/documentation/kml_tut):

<kml xmlns="http://www.opengis.net/kml/2.2">
  <Placemark>
    <name>Simple placemark</name>
    <description>Attached to the ground. Intelligently places itself 
       at the height of the underlying terrain.</description>
    <Point>
      <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
    </Point>
  </Placemark>
</kml>

I did this:

$xml = simplexml_load_string($data);

foreach($xml->Placemark as $Placemark){
    $name = $Placemark->name;
    echo $name."<br>";

    if ($Placemark->Point->coordinates == null) {
        continue;
    } else {
        $coordinates = explode(",", $Placemark->Point->coordinates);
        echo $coordinates[1].",".$coordinates[0]."<br>";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文