2

グーグルマップを表示するウェブサイトに使用するために、GPS座標をテキストファイルにコピーしています。以前は、phpスクリプトを使用してgps座標といくつかの長い文字列を取得し、表示できるkmlドキュメントを作成していました。それは最善の方法ではないようです。テキストファイルgpsinfo.txtがあり、開いたときに次のように見える4つのデータポイントの情報が含まれています。

7.784606,63.10403 7.784606,64.10403 7.784606,65.10403 7.784606,66.10403

そのテキストファイルを使用して、Webページに4つのポイントを作成するにはどうすればよいですか?

phpファイルを使用して自分のWebページで読み取れるjsファイルを作成することを考えていましたが、それは非常にクリーンな方法のようには思えません。

4

2 に答える 2

2

phpを使用して同じテキストファイルを読み取ってから、必要なものを作成することができます。

例:

<?php

$contents = file_get_contents("gpsinfo.txt");
$contentsArray = explode(" ", $contents);

$gpsCoords = array(); // your final result to use when constructing javascript for maps

foreach($contentsArray as $key => $gpsItem)
{
    $gpsArray = explode("," $gpsItem);
    $gpsArray[$key]['lat'] = $gpsItem[0];  
    $gpsArray[$key]['lon'] = $gpsItem[1];
    // The latter two might change as I am not sure which is 
    // the lon and which is the lat at your end
}

?>

このコードは、コードスタイルに応じて、座標の配列を返す関数で使用することも、単にインラインで使用することもできます。

Another way of doing it is to store the coordinates into a JSON array and then try to fetch it and feed it directly into javascript. Though in my example you would just need to store the data in some specific way: i.e. call in php to construct an array or call a function in javascript for each coordinate which would place the marker on the map. PHP runs way before javascript (serverside, whereas js runs client side), which means there are multiple ways of approaching a solution.

于 2012-07-18T08:19:12.977 に答える
0

これは私がこれを解決するために行っていることです:

phpファイルでいくつかのWebページをスクレイプしてGPS座標を取得し、それらをテキストファイルにJSON形式でコピーします。

  [{ "boat": "Edda", "coordinates" : { "lat" : 7.80086, "lon": 64.75658}}, { "......

次に、ファイルを読み取り、ajaxを使用してWebページからアクセスし、文字列を出力するphpファイルがあります。その後、Webページがそれを処理します。

ウェブページ上のajax:

<script type="text/javascript" language="JavaScript">
var point;
microAjax("BackgroundFiles/genjsonGPS.php", function (res) {

  var json_obj = JSON.parse(res.toString());

  initialize(json_obj);
  window.setTimeout(function(){

    }, 500);      
});

  </script>

genjsonGPS.php:

<?php

$myFile = "vesselGPS.txt";
$fh = fopen($myFile, 'r');
$vesselGPS = fread($fh, filesize($myFile));
fclose($fh);

echo($vesselGPS);


?>
于 2012-08-01T08:15:40.003 に答える