2011-12-19 4 views
6

Spero che ci sia un genio del php qui da qualche parte che può aiutarmi con questo. Ho una tabella di database chiamato baeir, ha i seguenti campi:

b_id (chiave primaria)
b_name
hrepparid (non ha bisogno di essere visualizzato)
Stjarna (non ha bisogno di essere visualizzato)
lat
GNL
commenti

La tabella contiene un nome di una fattoria (b_name), le sue coordinate geografiche (lat, lng) e commenti - se ce ne sono.

Quello che mi serve è ottenere questi dati in un file kml. Ho provato a passare attraverso il tutorial sulla pagina web di google e ho avuto modo di funzionare, ma una volta che provo a modificare, va a h ***. Per quanto posso capire, un file kml è fondamentalmente un file xml ma sfortunatamente le mie capacità di php non sono abbastanza avanzate per gestirlo.

Spero che qualcuno possa aiutare :-))Creazione di un file kml da un database mysql con php

Questo è il codice che ho da parte di Google, e sembra un po 'troppo ... dettagliato per i miei gusti. Qualche idea su come semplificare il codice in modo da poter inserire le mie variabili e nient'altro?

<?php 
require('phpsqlajax_dbinfo.php'); 

// Opens a connection to a MySQL server. 

$connection = mysql_connect ($server, $username, $password); 

if (!$connection) 
{ 
    die('Not connected : ' . mysql_error()); 
} 
// Sets the active MySQL database. 
$db_selected = mysql_select_db($database, $connection); 

if (!$db_selected) 
{ 
    die('Can\'t use db : ' . mysql_error()); 
} 

// Selects all the rows in the markers table. 
$query = 'SELECT * FROM markers WHERE 1'; 
$result = mysql_query($query); 

if (!$result) 
{ 
    die('Invalid query: ' . mysql_error()); 
} 

// Creates the Document. 
$dom = new DOMDocument('1.0', 'UTF-8'); 


// Creates the root KML element and appends it to the root document. 
$node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml'); 
$parNode = $dom->appendChild($node); 

// Creates a KML Document element and append it to the KML element. 
$dnode = $dom->createElement('Document'); 
$docNode = $parNode->appendChild($dnode); 


// Creates the two Style elements, one for restaurant and one for bar, and append the elements to the Document element. 
$restStyleNode = $dom->createElement('Style'); 
$restStyleNode->setAttribute('id', 'restaurantStyle'); 
$restIconstyleNode = $dom->createElement('IconStyle'); 
$restIconstyleNode->setAttribute('id', 'restaurantIcon'); 
$restIconNode = $dom->createElement('Icon'); 
$restHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png'); 
$restIconNode->appendChild($restHref); 
$restIconstyleNode->appendChild($restIconNode); 
$restStyleNode->appendChild($restIconstyleNode); 
$docNode->appendChild($restStyleNode); 

$barStyleNode = $dom->createElement('Style'); 
$barStyleNode->setAttribute('id', 'barStyle'); 
$barIconstyleNode = $dom->createElement('IconStyle'); 
$barIconstyleNode->setAttribute('id', 'barIcon'); 
$barIconNode = $dom->createElement('Icon'); 
$barHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon27.png'); 
$barIconNode->appendChild($barHref); 
$barIconstyleNode->appendChild($barIconNode); 
$barStyleNode->appendChild($barIconstyleNode); 
$docNode->appendChild($barStyleNode); 

// Iterates through the MySQL results, creating one Placemark for each row. 
while ($row = @mysql_fetch_assoc($result)) 
{ 
    // Creates a Placemark and append it to the Document. 

    $node = $dom->createElement('Placemark'); 
    $placeNode = $docNode->appendChild($node); 

    // Creates an id attribute and assign it the value of id column. 
    $placeNode->setAttribute('id', 'placemark' . $row['id']); 

    // Create name, and description elements and assigns them the values of the name and address columns from the results. 
    $nameNode = $dom->createElement('name',htmlentities($row['name'])); 
    $placeNode->appendChild($nameNode); 
    $descNode = $dom->createElement('description', $row['address']); 
    $placeNode->appendChild($descNode); 
    $styleUrl = $dom->createElement('styleUrl', '#' . $row['type'] . 'Style'); 
    $placeNode->appendChild($styleUrl); 

    // Creates a Point element. 
    $pointNode = $dom->createElement('Point'); 
    $placeNode->appendChild($pointNode); 

    // Creates a coordinates element and gives it the value of the lng and lat columns from the results. 
    $coorStr = $row['lng'] . ',' . $row['lat']; 
    $coorNode = $dom->createElement('coordinates', $coorStr); 
    $pointNode->appendChild($coorNode); 
} 


$kmlOutput = $dom->saveXML(); 
while (@ob_end_clean()); 
header('content-type:text/xml;'); 
echo $kmlOutput; 

?> 

risposta

2

come accennato in precedenza, il seguente Google Maps esercitazione risponde alla tua domanda quasi del tutto: http://code.google.com/apis/kml/articles/phpmysqlkml.html

purtroppo, non propone codice per poligono analisi (che sarà necessario, se si sta gestendo la visualizzazione delle aree della farm), ma è possibile adattare il metodo di analisi LinesString e ottenerlo. fai attenzione a incorporare correttamente il tag ExternalBoundaryIs nel tag Polygon E ricorda che devi duplicare il punto di partenza per disegnare correttamente il poligono.

$lineNode = $dom->createElement('Polygon'); 
$placeNode = $placeNode->appendChild($lineNode); 
$exnode = $dom->createElement('extrude', '1'); 
$lineNode->appendChild($exnode); 
$almodenode =$dom->createElement(altitudeMode,'relativeToGround'); 
$lineNode->appendChild($almodenode); 
$outerboundnode = $dom->createElement('outerBoundaryIs'); 
$placeNode = $placeNode->appendChild($outerboundnode); 
$ringnode =$dom->createElement('LinearRing'); 
$placeNode = $placeNode->appendChild($ringnode); 
    // optional styletag colors the polygon 
//$stylenode =$dom->createElement(styleUrl,'#transYellowPoly'); 
//$lineNode->appendChild($stylenode); 

//Create a coordinates element and give it the value of the lng and lat columns from the results 
//$coorNode = $dom->createElement('coordinates',$row['coordinates']); 
$coorNode = $dom->createElement('coordinates',$coordinates); 
$placeNode = $placeNode->appendChild($coorNode); 
+1

non dimenticare che l'API GOOGLEMAPS NON È GRATUITA, prima di andare e investire tempo a sviluppare un'applicazione che la utilizza. –

1

kml non ha nulla a che fare con il tuo php. Il tuo file kml è fondamentalmente un file xml che è specificamente utilizzato nelle applicazioni google earth e google map. Il tuo file Kml deve superare un test di validità per poterlo utilizzare. È possibile utilizzare il seguente link per convalidare il file kml. Kml validator. Il tuo file kml conterrà i tag predefiniti come pointcoordinatedescription in modo che possa essere analizzato. Se si vuole dare uno sguardo su come il file KML sembra, qui si va

Sample Kml File

Inutile dire che il tuo file KML deve contenere le informazioni nei tag corretto e quindi è necessario analizzarlo. e-g lat e lng possono entrare nel tag delle coordinate. il tag descrizione può contenere la descrizione del testo, ad esempio un commento.

+0

ok forse ho formulato questo torto, ma quello che mi serve è un file PHP per generare il file KML dal database mysql ... So kml non ha nulla a che fare direttamente con PHP, ma so anche che puoi generare un file kml con php e questo è ciò di cui ho bisogno. – user1088537

+0

sfortunatamente io non sono un ragazzo php, ma penso che questo link dovrebbe aiutarti, se non l'hai già visto: http://code.google.com/apis/kml/articles/phpmysqlkml.html – defau1t

+0

grazie refhat, l'ho provato, ma non appena l'ho modificato, non ho potuto farlo funzionare. – user1088537

2

Prova questa:

<?php 
require('phpsqlajax_dbinfo.php'); 
function xmlEntities($str) 
{ 
    $xml = array('&#34;','&#38;','&#38;','&#60;','&#62;','&#160;','&#161;','&#162;','&#163;','&#164;','&#165;','&#166;','&#167;','&#168;','&#169;','&#170;','&#171;','&#172;','&#173;','&#174;','&#175;','&#176;','&#177;','&#178;','&#179;','&#180;','&#181;','&#182;','&#183;','&#184;','&#185;','&#186;','&#187;','&#188;','&#189;','&#190;','&#191;','&#192;','&#193;','&#194;','&#195;','&#196;','&#197;','&#198;','&#199;','&#200;','&#201;','&#202;','&#203;','&#204;','&#205;','&#206;','&#207;','&#208;','&#209;','&#210;','&#211;','&#212;','&#213;','&#214;','&#215;','&#216;','&#217;','&#218;','&#219;','&#220;','&#221;','&#222;','&#223;','&#224;','&#225;','&#226;','&#227;','&#228;','&#229;','&#230;','&#231;','&#232;','&#233;','&#234;','&#235;','&#236;','&#237;','&#238;','&#239;','&#240;','&#241;','&#242;','&#243;','&#244;','&#245;','&#246;','&#247;','&#248;','&#249;','&#250;','&#251;','&#252;','&#253;','&#254;','&#255;'); 
    $html = array('&quot;','&amp;','&amp;','&lt;','&gt;','&nbsp;','&iexcl;','&cent;','&pound;','&curren;','&yen;','&brvbar;','&sect;','&uml;','&copy;','&ordf;','&laquo;','&not;','&shy;','&reg;','&macr;','&deg;','&plusmn;','&sup2;','&sup3;','&acute;','&micro;','&para;','&middot;','&cedil;','&sup1;','&ordm;','&raquo;','&frac14;','&frac12;','&frac34;','&iquest;','&Agrave;','&Aacute;','&Acirc;','&Atilde;','&Auml;','&Aring;','&AElig;','&Ccedil;','&Egrave;','&Eacute;','&Ecirc;','&Euml;','&Igrave;','&Iacute;','&Icirc;','&Iuml;','&ETH;','&Ntilde;','&Ograve;','&Oacute;','&Ocirc;','&Otilde;','&Ouml;','&times;','&Oslash;','&Ugrave;','&Uacute;','&Ucirc;','&Uuml;','&Yacute;','&THORN;','&szlig;','&agrave;','&aacute;','&acirc;','&atilde;','&auml;','&aring;','&aelig;','&ccedil;','&egrave;','&eacute;','&ecirc;','&euml;','&igrave;','&iacute;','&icirc;','&iuml;','&eth;','&ntilde;','&ograve;','&oacute;','&ocirc;','&otilde;','&ouml;','&divide;','&oslash;','&ugrave;','&uacute;','&ucirc;','&uuml;','&yacute;','&thorn;','&yuml;'); 
    $str = str_replace($html,$xml,$str); 
    $str = str_ireplace($html,$xml,$str); 
    return $str; 
} 

// Opens a connection to a MySQL server. 

$connection = mysql_connect ($server, $username, $password); 

if (!$connection) 
{ 
    die('Not connected : ' . mysql_error()); 
} 
// Sets the active MySQL database. 
$db_selected = mysql_select_db($database, $connection); 

if (!$db_selected) 
{ 
    die('Can\'t use db : ' . mysql_error()); 
} 

// Selects all the rows in the markers table. 
$query = 'SELECT * FROM baeir WHERE 1'; 
$result = mysql_query($query); 

if (!$result) 
{ 
    die('Invalid query: ' . mysql_error()); 
} 

// Creates the Document. 
$dom = new DOMDocument('1.0', 'UTF-8'); 


// Creates the root KML element and appends it to the root document. 
$node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml'); 
$parNode = $dom->appendChild($node); 

// Creates a KML Document element and append it to the KML element. 
$dnode = $dom->createElement('Document'); 
$docNode = $parNode->appendChild($dnode); 


// Creates the two Style elements, one for restaurant and one for bar, and append the elements to the Document element. 
$restStyleNode = $dom->createElement('Style'); 
$restStyleNode->setAttribute('id', 'restaurantStyle'); 
$restIconstyleNode = $dom->createElement('IconStyle'); 
$restIconstyleNode->setAttribute('id', 'restaurantIcon'); 
$restIconNode = $dom->createElement('Icon'); 
$restHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png'); 
$restIconNode->appendChild($restHref); 
$restIconstyleNode->appendChild($restIconNode); 
$restStyleNode->appendChild($restIconstyleNode); 
$docNode->appendChild($restStyleNode); 

$barStyleNode = $dom->createElement('Style'); 
$barStyleNode->setAttribute('id', 'barStyle'); 
$barIconstyleNode = $dom->createElement('IconStyle'); 
$barIconstyleNode->setAttribute('id', 'barIcon'); 
$barIconNode = $dom->createElement('Icon'); 
$barHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon27.png'); 
$barIconNode->appendChild($barHref); 
$barIconstyleNode->appendChild($barIconNode); 
$barStyleNode->appendChild($barIconstyleNode); 
$docNode->appendChild($barStyleNode); 

// Iterates through the MySQL results, creating one Placemark for each row. 
while ($row = @mysql_fetch_assoc($result)) 
{ 
    // Creates a Placemark and append it to the Document. 

    $node = $dom->createElement('Placemark'); 
    $placeNode = $docNode->appendChild($node); 

    // Creates an id attribute and assign it the value of id column. 
    $placeNode->setAttribute('id', 'placemark' . $row['b_id ']); 

    // Create name, and description elements and assigns them the values of the name and address columns from the results. 
    $nameNode = $dom->createElement('name',xmlEntities(htmlentities($row['b_name']))); 
    $placeNode->appendChild($nameNode); 
    $descNode = $dom->createElement('description', $row['comments']); 
    $placeNode->appendChild($descNode); 
    //$styleUrl = $dom->createElement('styleUrl', '#' . $row['type'] . 'Style'); 
    //$placeNode->appendChild($styleUrl); 

    // Creates a Point element. 
    $pointNode = $dom->createElement('Point'); 
    $placeNode->appendChild($pointNode); 

    // Creates a coordinates element and gives it the value of the lng and lat columns from the results. 
    $coorStr = $row['lng'] . ',' . $row['lat']; 
    $coorNode = $dom->createElement('coordinates', $coorStr); 
    $pointNode->appendChild($coorNode); 
} 


$kmlOutput = $dom->saveXML(); 
while (@ob_end_clean()); 
header('content-type:text/xml;'); 
echo $kmlOutput; 

?> 
+0

Ciao saratis! Grazie, ma non ha funzionato: -/ – user1088537

+0

Perché no, cosa è andato storto? –

+0

Questa pagina contiene i seguenti errori: errore sulla riga 2 nella colonna 292: entità 'Aacute' non definita – user1088537