使用PHP查询生成KML/KMZ/XML
我正在尝试使用php生成KML,但在第1行出现了一个错误屏幕,这是我的文档:使用PHP查询生成KML/KMZ/XML,php,xml,google-maps,kml,Php,Xml,Google Maps,Kml,我正在尝试使用php生成KML,但在第1行出现了一个错误屏幕,这是我的文档: <?php header('Content-type: text/xml'); include('../../../../../../config.php'); // Print the head of the document echo htmlentities('<?xml version="1.0" encoding="UTF-8"?>'); echo '</br>'; echo
<?php
header('Content-type: text/xml');
include('../../../../../../config.php');
// Print the head of the document
echo htmlentities('<?xml version="1.0" encoding="UTF-8"?>');
echo '</br>';
echo htmlentities('<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">');
echo '</br>';
echo htmlentities('<Document>');
echo '</br>';
// Finally query the database data
$result = mysql_query("SELECT * FROM acars_airports ORDER BY id DESC");
// Now iterate over all placemarks (rows)
while ($row = mysql_fetch_array($result)) {
// This writes out a placemark with some data
// -- Modify for your case --
echo htmlentities('<Placemark>');
echo '</br>';
echo htmlentities('<name>'.$row['icao'].'</name>');
echo '</br>';
echo htmlentities('<description>'.$row['name'].'</description>');
echo '</br>';
echo htmlentities('<Point>');
echo '</br>';
echo htmlentities('<coordinates>'.$row['lon'].' , '.$row['lat'].'</coordinates>');
echo '</br>';
echo htmlentities('</Point>');
echo '</br>';
echo htmlentities('</Placemark>');
echo '</br>';
};
// And finish the document
echo htmlentities('</Document>');
echo '</br>';
echo htmlentities('</kml>');
?>
创建XML的一种简单方法是XMLWriter:
$r=new XMLWriter();
$r->openMemory();
$r->startDocument('1.0','UTF-8');
$r->startElement('kml');
$r->startElement('document');
$r->startElement('Placemark');
$r->startElement('name');
$r->text($row['icao']);
$r->endElement();
$r->startElement('description');
$r->text($row['name']);
$r->endElement();
$r->startElement('Point');
$r->startElement('coordinates');
$r->text($row['lon'].' , '.$row['lat']);
$r->endElement(); // coordinates
$r->endElement(); // point
$r->endElement(); // Placemark
$r->endElement(); // document
$r->endElement(); // kml
$newxml = $r->outputMemory(true);
您仍然需要确定如何在mml节点中设置名称空间。请参见我已经创建了一个新代码,现在可以使用了,请将其放入您的HTML页面:
</html>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-15.869167, -47.920834),
zoom: 3,
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.HYBRID
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var nyLayer = new google.maps.KmlLayer(
'YOUR_REAL_KML_LINK',
{ suppressInfoWindows: false,
map: map});
google.maps.event.addListener(nYLayer,'click',function(){
infowindow.open(map, nYLayer);
});
}
</script>
</head>
<body onLoad="initialize()">
<div id="map_canvas" style="width:800; height:400;"></div>
</body>
</html>
html{高度:100%}
正文{高度:100%;边距:0;填充:0}
#地图画布{高度:100%}
函数初始化(){
变量映射选项={
中心:新google.maps.LatLng(-15.869167,-47.920834),
缩放:3,
disableDefaultUI:true,
mapTypeId:google.maps.mapTypeId.HYBRID
};
var map=new google.maps.map(document.getElementById(“map_canvas”),
地图选项);
var nyLayer=new google.maps.KmlLayer(
“你的真实KML链接”,
{suppressInfoWindows:false,
map:map});
google.maps.event.addListener(nYLayer,'click',function(){
打开(地图,nYLayer);
});
}
并使用此查询通过PHP生成KML:
<?php
header('Content-Type: application/vnd.google-earth.kml+xml kml');
header('Content-Disposition: attachment; filename="test.kml"');
include('database_config.php');
// Query the database data
$result = mysql_query("SELECT * FROM YOUR_DATA_TABLE");
// Print the head of the document
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">';
echo '<Document>';
// Now iterate over all placemarks (rows)
while ($row = mysql_fetch_array($result)) {
// This writes out a placemark with some data
echo '<Placemark>';
echo '<name>'.$row['name'].'</name>';
echo '<description>'.$row['description'].'</description>';
echo '<Point>';
echo '<coordinates>'.$row['lng'].' , '.$row['lat'].'</coordinates>';
echo '</Point>';
echo '</Placemark>';
};
// And finish the document
echo '</Document>';
echo '</kml>';
?>
简单点!谢谢你们的帮助,伙计们 在这种情况下,它使文档更易于阅读;)它使XML无效;)啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊!谢谢,让我们看看会发生什么!不我仍然看到一个错误屏幕:
第1行第1列的错误:文档为空
<?php
header('Content-Type: application/vnd.google-earth.kml+xml kml');
header('Content-Disposition: attachment; filename="test.kml"');
include('database_config.php');
// Query the database data
$result = mysql_query("SELECT * FROM YOUR_DATA_TABLE");
// Print the head of the document
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">';
echo '<Document>';
// Now iterate over all placemarks (rows)
while ($row = mysql_fetch_array($result)) {
// This writes out a placemark with some data
echo '<Placemark>';
echo '<name>'.$row['name'].'</name>';
echo '<description>'.$row['description'].'</description>';
echo '<Point>';
echo '<coordinates>'.$row['lng'].' , '.$row['lat'].'</coordinates>';
echo '</Point>';
echo '</Placemark>';
};
// And finish the document
echo '</Document>';
echo '</kml>';
?>