Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7093861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:29:32+00:00 2026-05-28T08:29:32+00:00

I hope there is a php genius here somewhere who can help me with

  • 0

I hope there is a php genius here somewhere who can help me with this. I have a database table called baeir, it has the following fields:


b_id (primary key)

b_name

hrepparid (does not need to be displayed)

stjarna (does not need to be displayed)

lat

lng

comments


The table contains a name of a farm (b_name), its geographical coordinates (lat, lng) and comments – if there are any.

What I need is to get this data into a kml file. I have tried to go through the tutorial on google’s webpage and I got it to work BUT once I try to edit, it goes to h***. As far as I can understand, a kml file is basically an xml file but unfortunately my php skills are not advanced enough to handle this.

I hope someone can help :-))

This is the code I have from google, and it looks a bit too…detailed for my liking. Any ideas how to simplify the code so that I can get my variables into it and nothing else?

<?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;

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:29:33+00:00Added an answer on May 28, 2026 at 8:29 am

    as mentioned above, the following Google Maps tutorial answers your question ALMOST completely: http://code.google.com/apis/kml/articles/phpmysqlkml.html

    unfortunately, it does not propose code for polygon parsing (which you will need, if you are managing farm areas display), but you can adapt the LinesString parsing method and achieve it. beware of properly embedding the outerBoundaryIs tag in the Polygon tag AND remember that you must duplicate the starting point for the polygon to be drawn correctly.

        $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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope there's a SharePoint expert here on SO who can help with this.
I hope there's someone who can help me suggest a suitable data model to
I hope somebody can help work out to do this. Basically, I have a
Still wrapping my head around SQL and PHP, but hope someone can help with
This is about RAW php coding. I have a page - config.php and there
i hope you can help me with this one. The code is supposed to
I have a simple question about Java's XML API and I hope there's a
I hope someone out there can shed some light on the topic of creating
Is there any way (utilizing Reflection I hope) that I can make an instantiated
Hope this makes sense... Is there a simple way to return a set of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.