简体   繁体   English

从.txt中提取PHP数据并将其排序为kml

[英]PHP data extraction from .txt and sorting to kml

I'm new to javascript and have very little knowledge of PHP. 我是javascript的新手,对PHP的了解很少。 I'm trying to get the data from a google maps route formatted to a KML file. 我正在尝试从格式化为KML文件的Google地图路线获取数据。 Right now I'm using jquery stringify to save the coördinates of points of the route to a textfile. 现在,我正在使用jquery stringify将路线的点的坐标保存到文本文件。

Textfile looks like this: 文本文件如下所示:

[{"lat":51.101510000000005,"lng":4.948580000000001},{"lat":51.10208,"lng":4.94763},{"lat":51.102160000000005,"lng":4.94744},{"lat":51.102140000000006,"lng":4.944330000000001}] [{“ lat”:51.101510000000005,“ lng”:4.948580000000001},{“ lat”:51.10208,“ lng”:4.94763},{“ lat”:51.102160000000005,“ lng”:4.94744},{“ lat”:51.102140000000006, “ lng”:4.944330000000001}]

What I would like to do is get the coördinates of each Lat and Lng and put them in a kml file. 我想做的是获取每个Lat和Lng的坐标,并将它们放在kml文件中。 Putting them into the file is probably something that I will be able to figure out, but I can't seem to find how I can extract each and every Lat and Lng value. 将它们放入文件可能是我可以弄清楚的东西,但是我似乎找不到如何提取每个Lat和Lng值的方法。

If I'm Taking a wrong approach by using stringify in JS to save the original array to a text file,please be free to offer a better solution. 如果我在JS中使用stringify将原始数组保存到文本文件采用了错误的方法,请随意提供更好的解决方案。

Thank you in advance 先感谢您

Here is the code I have at the moment: 这是我目前的代码:

PHP 的PHP

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = $_POST['RoutePath'];
fwrite($myfile, $txt);
fclose($myfile);

Javascript Java脚本

directionsDisplay.setDirections(response);
console.log(response);
varRoutePath = response.routes[0].overview_path;
console.log(varRoutePath);

xml = JSON.stringify(varRoutePath);
console.log(xml);

function fuDownloadRoute() {
    console.log("fuDownloadRoute");
    console.log(xml);

    $.post('Test.php', { RoutePath: xml },
        function (data) {
            $('#result').html(data);
        });
}

Your coordinates are in JavaScript object notation ( JSON ) format. 您的座标采用JavaScript对象表示法( JSON )格式。 PHP has a function set for encoding and decoding JSON strings. PHP具有用于编码和解码JSON字符串的功能集。

In your particular case you might use json_decode() as follows to first convert the text into an array, and then iterate over each coordinate pair: 在您的特定情况下,您可以如下使用json_decode() ,首先将文本转换为数组,然后遍历每个坐标对:

$myjson = '[{"lat":51.101510000000005,"lng":4.948580000000001},{"lat":51.10208,"lng":4.94763},{"lat":51.102160000000005,"lng":4.94744},{"lat":51.102140000000006,"lng":4.944330000000001}]';
$coordinates = json_decode($myjson, true); // decode into associative array

foreach($coordinates as $pair) {
  echo $pair['lat'] .','. $pair['lng'] . PHP_EOL;
}

Output: 输出:

51.10151,4.94858 51.10151,4.94858
51.10208,4.94763 51.10208,4.94763
51.10216,4.94744 51.10216,4.94744
51.10214,4.94433 51.10214,4.94433

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM