我有一项任务是创建一个允许用户上传GPX文件(XML)的小型Web应用程序。它看起来像这样:
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" version="1.0" creator="GPSBabel - http://www.gpsbabel.org" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
<time>2008-10-17T20:11:41Z</time>
<bounds minlat="48.063300000" minlon="8.567683333" maxlat="48.765550000" maxlon="10.039016667"/>
<trk>
<name>PRESALTTRK</name>
<desc>
IGCHDRS~HFFXA500~HFPLTPilotincharge:~HPCM2Crew2:~HFGTYGliderType:SF25C2000~HFGIDGliderID:D-KFEL~HFDTM100GPSDatum:WGS84~HFRFWFirmwareVersion:Flarm04.04~HFRHWHardwareVersion:Flarm06~HFFTYFRType:Flarm~HFGPSu-blox:LEA-4P,16,8191~HFPRSPressAltSensor:Intersema MS5534B,8191~HFCCLCompetitionClass:Motorsegler~HFCIDCompetitionID:~
</desc>
<trkseg>
<trkpt lat="48.063300000" lon="8.567683333">
<ele>675.000000</ele>
<time>2008-09-11T16:22:01Z</time>
</trkpt>
<trkpt lat="48.063783333" lon="8.568383333">
<ele>675.000000</ele>
<time>2008-09-11T16:22:08Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
然后,用户应该能够选择多个必须转换为KML文件并在Google地图中显示的跟踪点。
我设法上传了GPX文件,并希望在HTML选择框中显示跟踪点。
<script type="text/javascript">
function addTrackpoint(lat, lon)
{
var x = document.getElementById("box");
var option = document.createElement("option");
option.text = lat+", "+lon;
x.add(option);
}
</script>
</head>
<body>
<button type="button" onclick="for (var i = 0; i < <?php echo "$j"?>; i++) {
addTrackpoint(<?php echo "$lat[$i], $lon[$i]"?>);
}">Trackpoints anzeigen</button>
<form action="select.htm">
<p>
<select id ="box" name="trackpoint_box" size="<?php echo "$j"?>" multiple>,
</select>
</p>
</form>
</body>
我无法弄清楚如何增加$ i以显示不同的跟踪点,因为它是一个PHP变量。但它也感觉我的解决方案只是糟糕而复杂。对于我想要实现的目标,是否有更优雅的解决方案?
好的,我现在正尝试使用AJAX和JSON。我已经加载并转换了XML:
$xml = simplexml_load_file($_FILES["file"]["name"]);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
我尝试像这样加载JSON:
<script type="application/javascript">
function loadJSON()
{
var data_file = "<?php echo $json; ?>";
var http_request = new XMLHttpRequest();
.........
.........
var jsonObj = JSON.parse(http_request.responseText);
我现在如何访问我的属性?我试过这个
document.getElementById("Name").innerHTML = jsonObj.["@attributes"[5]].lat;
它不起作用。我的JSON文件对于每个跟踪点都是这样的:
{"@attributes":
{
"lat":"48.105116667",
"lon":"8.637700000"
},
"ele":"1000.000000",
"time":"2008-09-11T16:25:52Z"
},
{"@attributes": .......