我正在开发一个应用程序,该应用程序将使用PHP生成KML文件。通常情况下,我用单点来做这件事,没有问题或几乎没有问题,但对于多边形,我很难让它们风格化。
我试图做的是基于PHP的动态样式为多边形着色;然而,我甚至无法首先获得一个硬编码的样式。KML的结构显示在我遇到问题的代码示例之后。
如果我只想设置一个多边形颜色样式和一定的宽度,那么正确的样式dom语法应该是什么样子。
感谢您的帮助
这是初始代码:
// Creates the Document.
$dom = new DOMDocument('1.0', 'UTF-8');
$kmlfile = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');
$parentnode = $dom->appendChild($kmlfile);
$dfnode = $dom->createElement('Document');
$documentnode = $parentnode->appendChild($dfnode);
$gpsStyleNode = $dom->createElement('Style');
$gpsStyleNode->setAttribute('id', 'style_gps');
$gpsIconstyleNode = $dom->createElement('IconStyle');
$gpsIconstyleNode->setAttribute('id', 'icon_gps');
$gpsIconstyleNode->setAttribute('scale', '0.6');
$gpsIconstyleNode->setAttribute('color', 'ff0000ff');
$gpsIconNode = $dom->createElement('Icon');
$gpsHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/shapes/cross-hairs.png');
$gpsIconNode->appendChild($gpsHref);
$gpsIconstyleNode->appendChild($gpsIconNode);
$gpsStyleNode->appendChild($gpsIconstyleNode);
$gpslinetyleNode = $dom->createElement('PolyStyle');
$gpsStyleNode->appendChild($gpslinetyleNode);
$gpslinetyleNode->setAttribute('id', 'Icon_gps2');
$gpslinetyleNode->setAttribute('color', 'ff0000ff');
$gpslinetyleNode->setAttribute('width', '20');
$documentnode->appendChild($gpsStyleNode);
KML输出
<?xml version="1.0" encoding="UTF-8"?>
<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">
<Placemark id="12">
<name>PolygonName</name>
<description>test</description>
<styleUrl>Icon_gps2</styleUrl>
<Polygon>
<extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
(Dynamic Coords pulled from DB in proper format)
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</kml>
编辑以添加多边形代码
$placeobject = $dom->createElement('Placemark');
$placeNode = $nDoc->appendChild($placeobject);
$placeobject->setAttribute('id',$tmpid);
$placename = $dom->createElement('name','PolygonName');
$placeNode->appendChild($placename);
$placedesc = $dom->createElement('description', 'test');
$placeNode->appendChild($placedesc);
$stylenode =$dom->createElement('styleUrl','line_gps');
$placeNode->appendChild($stylenode);
$linenode = $dom->createElement('Polygon');
$placeNode->appendChild($linenode);
$lineextrude = $dom->createElement('extrude', '1');
$linenode->appendChild($lineextrude);
$linealtitude = $dom->createElement('altitudeMode', 'relativeToGround');
$linenode->appendChild($linealtitude);
$outerboundnode = $dom->createElement('outerBoundaryIs');
$linenode = $linenode->appendChild($outerboundnode);
$ringtype =$dom->createElement('LinearRing');
$linenode = $linenode->appendChild($ringtype);
$coordnode = $dom->createElement('coordinates',$locationstring);
$ringtype->appendChild($coordnode);
您正在设置预期作为元素的属性(颜色、比例、宽度)。
此外,宽度适用于LineStyle,而不适用于PolyStyle
$dom = new DOMDocument('1.0', 'UTF-8');
$nKml = $dom->appendChild($dom->createElementNS('http://earth.google.com/kml/2.1', 'kml'));
$nDoc = $nKml->appendChild($dom->createElement('Document'));
$idSuffix='_gps';
//Style
$nStyle = $nDoc->appendChild($dom->createElement('Style'));
$nStyle->setAttribute('id', 'style'.$idSuffix);
//IconStyle
$nIconStyle = $nStyle->appendChild($dom->createElement('IconStyle'));
$nIconStyle->setAttribute('id', 'icon'.$idSuffix);
//color
$nIconStyleColor=$nIconStyle->appendChild($dom->createElement('color'));
$nIconStyleColor->appendChild($dom->createTextNode('ff0000ff'));
//Icon
$nIconStyleIcon=$nIconStyle->appendChild($dom->createElement('Icon'));
//href
$nIconStyleHref=$nIconStyleIcon->appendChild($dom->createElement('href'));
$nIconStyleHref->appendChild($dom->createTextNode('http://maps.google.com/../cross-hairs.png'));
//PolyStyle
$nPolyStyle = $nStyle->appendChild($dom->createElement('PolyStyle'));
$nPolyStyle->setAttribute('id', 'poly'.$idSuffix);
//color
$nPolyStyleColor=$nPolyStyle->appendChild($dom->createElement('color'));
$nPolyStyleColor->appendChild($dom->createTextNode('ff0000ff'));
//LineStyle
$nLineStyle = $nStyle->appendChild($dom->createElement('LineStyle'));
$nLineStyle->setAttribute('id', 'line'.$idSuffix);
//width
$nLineStyleWidth=$nLineStyle->appendChild($dom->createElement('width'));
$nLineStyleWidth->appendChild($dom->createTextNode('20'));
$nLineStyleColor=$nLineStyle->appendChild($dom->createElement('color'));
$nLineStyleColor->appendChild($dom->createTextNode('ff0000ff'));
结果:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<Style id="style_gps">
<IconStyle id="icon_gps">
<color>ff0000ff</color>
<Icon>
<href>http://maps.google.com/../cross-hairs.png</href>
</Icon>
</IconStyle>
<PolyStyle id="poly_gps">
<color>ff0000ff</color>
</PolyStyle>
<LineStyle id="line_gps">
<width>20</width>
<color>ff0000ff</color>
</LineStyle>
</Style>
</Document>
</kml>