CREATE TABLE `markers` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR (60) NOT NULL ,
`address` VARCHAR (80) NOT NULL ,
`lat` FLOAT (10, 6) NOT NULL ,
`lng` FLOAT (10, 6) NOT NULL ,
`type` VARCHAR (30) NOT NULL
) ENGINE = MYISAM;
Pan Africa Market, "1521 1st Ave, Seattle, WA", 47.608941, -122.340145, restaurant
Buddha Thai & Bar, "2222 2nd Ave, Seattle, WA", 47.613591, -122.344394, bar
The Melting Pot, "14 Mercer St, Seattle, WA", 47.624562, -122.356442, restaurant
Ipanema Grill, "1225 1st Ave, Seattle, WA", 47.606366, -122.337656, restaurant
Sake House, "2230 1st Ave, Seattle, WA", 47.612825, -122.34567, bar
Crab Pot, "1301 Alaskan Way, Seattle, WA", 47.605961, -122.34036, restaurant
Mama's Mexican Kitchen, "2234 2nd Ave, Seattle, WA", 47.613975, -122.345467, bar
Wingdome, "1416 E Olive Way, Seattle, WA", 47.617215, -122.326584, bar
Piroshky Piroshky, "1908 Pike pl, Seattle, WA", 47.610127, -122.342838, restaurant
INSERT INTO `markers` (` name`, `address`,` lat`, `lng`,` type`) VALUES ( 'Pan Africa Market' , '1521 1st Ave, Seattle, WA' , '47 .608941 ' , ' - 122.340145 ' , ' restaurant ' );
INSERT INTO `markers` (` name`, `address`,` lat`, `lng`,` type`) VALUES ( 'Buddha Thai & Bar' , '2222 2nd Ave, Seattle, WA' , '47 .613591 ' , ' -122.344394 ' , ' bar ' );
INSERT INTO `markers` (` name`, `address`,` lat`, `lng`,` type`) VALUES ( 'The Melting Pot' , '14 Mercer St, Seattle, WA ' , '47 .624562' , '- 122.356442 ' , ' restaurant ' );
INSERT INTO `markers` (` name`, `address`,` lat`, `lng`,` type`) VALUES ( 'Ipanema Grill' , '1225 1st Ave, Seattle, WA' , '47 .606366 ' , ' -122.337656 ' , ' restaurant ' );
INSERT INTO `markers` (` name`, `address`,` lat`, `lng`,` type`) VALUES ( 'Sake House' , '2230 1st Ave, Seattle, WA' , '47 .612825 ' , ' -122.34567 ' , ' bar ' );
INSERT INTO `markers` (` name`, `address`,` lat`, `lng`,` type`) VALUES ( 'Crab Pot' , '1301 Alaskan Way, Seattle, WA' , '47 .605961 ' , ' -122.34036 ' , ' restaurant ' );
INSERT INTO `markers` (` name`, `address`,` lat`, `lng`,` type`) VALUES ( 'Mama Mexican Kitchen' , '2234 2nd Ave, Seattle, WA' , '47. 613975 ' , ' - 122.345467 ' , ' bar ' );
INSERT INTO `markers` (` name`, `address`,` lat`, `lng`,` type`) VALUES ( 'Wingdome' , '1416 E Olive Way, Seattle, WA' , '47 .617215 ' , ' -122.326584 ' , ' bar ' );
INSERT INTO `markers` (` name`, `address`,` lat`, `lng`,` type`) VALUES ( 'Piroshky Piroshky' , '1908 Pike pl, Seattle, WA' , '47 .610127 ' , ' -122.342838 ' , ' restaurant ' );
mysql_connect()
, mysql_select_db()
, my_sql_query()
, and mysql_error()
.<?
$username="username";
$password="password";
$database="username-databaseName";
?>
dom_xml
extension to create a simple file with markers, which was subsequently processed using JavaScript. In this tutorial you need to get a KML file. Instead of parsing KML with JavaScript, we will immediately build this file with Placemark-specific tags. Further, both methods of generating a KML file will be shown: using the built-in DOM functionality in PHP5 and using the dom_xml
extension in PHP4.DOMDocument()
works on your PHP server. If the DOM functionality is disabled, you can try to create an XML file, as described in the Pamela article. Or use the KML creation methods shown below.<document>
, you must create two styles - one for restaurants, the other for bars - which will later represent the markers through the <styleURL>
.SELECT *
query (select all) to the table with markers and go through all the results of this sample. For each row in the table, create a <Placemark>
element. The information extracted from the database is used to create child elements for <Placemark>
such as <name>
, <description>
, <styleURL>
, <Point>
. The value written in the <styleURL>
will depend on the value of the type
entry. Then add the <Placemark>
child element to <Placemark>
, and as the value of this element, we write down the coordinates of the institution contained in the lat
and lng
fields.<?php
require('phpsqlajax_dbinfo.php');
// MySQL-.
$connection = mysql_connect ($server, $username, $password);
if (!$connection)
{
die(' : ' . mysql_error());
}
// .
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected)
{
die(' : ' . mysql_error());
}
// markers.
$query = 'SELECT * FROM markers WHERE 1';
$result = mysql_query($query);
if (!$result)
{
die(' : ' . mysql_error());
}
// .
$dom = new DOMDocument('1.0', 'UTF-8');
// KML .
$node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');
$parNode = $dom->appendChild($node);
// Document KML.
$dnode = $dom->createElement('Document');
$docNode = $parNode->appendChild($dnode);
// : , . KML.
$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);
// Placemark.
while ($row = @mysql_fetch_assoc($result))
{
// Placemark Document.
$node = $dom->createElement('Placemark');
$placeNode = $docNode->appendChild($node);
// id.
$placeNode->setAttribute('id', 'placemark' . $row['id']);
// name description .
$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);
// Point.
$pointNode = $dom->createElement('Point');
$placeNode->appendChild($pointNode);
// coordinates.
$coorStr = $row['lng'] . ',' . $row['lat'];
$coorNode = $dom->createElement('coordinates', $coorStr);
$pointNode->appendChild($coorNode);
}
$kmlOutput = $dom->saveXML();
header('Content-type: application/vnd.google-earth.kml+xml');
echo $kmlOutput;
?>
dom_xml
extension<?php
require('phpsqlajax_dbinfo.php');
// MySQL-.
$connection=mysql_connect ($server, $username, $password);
if (!$connection)
{
die(' : ' . mysql_error());
}
// .
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected)
{
die (' : ' . mysql_error());
}
// markers.
$query = 'SELECT * FROM markers WHERE 1';
$result = mysql_query($query);
if (!$result)
{
die(' : ' . mysql_error());
}
// .
$dom = new domxml_new_doc('1.0');
// KML .
$node = $dom->create_element_ns('http://earth.google.com/kml/2.1', 'kml');
$parNode = $dom->append_child($node);
// Document KML.
$dnode = $dom->create_element('Document');
$docNode = $parNode->append_child($dnode);
// : - , - .
$restStyleNode = $dom->create_element('Style');
$restStyleNode->set_attribute('id', 'restaurantStyle');
$restIconstyleNode = $dom->create_element('IconStyle');
$restIconstyleNode->set_attribute('id', 'restaurantIcon');
$restIconNode = $dom->create_element('Icon');
$restHref = $dom->create_element('href', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png');
$restIconNode->append_child($restHref);
$restIconstyleNode->append_child($restIconNode);
$restStyleNode->append_child($restIconstyleNode);
$docNode->append_child($restStyleNode);
$barStyleNode = $dom->create_element('Style');
$barStyleNode->set_attribute('id', 'barStyle');
$barIconstyleNode = $dom->create_element('IconStyle');
$barIconstyleNode->set_attribute('id', 'barIcon');
$barIconNode = $dom->create_element('Icon');
$barHref = $dom->create_element('href', 'http://maps.google.com/mapfiles/kml/pal2/icon27.png');
$barIconNode->append_child($barHref);
$barIconstyleNode->append_child($barIconNode);
$barStyleNode->append_child($barIconstyleNode);
$docNode->append_child($barStyleNode);
// Placemark.
while ($row = @mysql_fetch_assoc($result))
{
// Placemark Document.
$node = $dom->create_element('Placemark');
$placeNode = $docNode->append_child($node);
// id.
$placeNode->set_attribute('id', 'placemark' . $row['id']);
// name description .
$nameNode = $dom->create_element('name',htmlentities($row['name']));
$placeNode->append_child($nameNode);
$descNode = $dom-> create_element('description', $row['address']);
$placeNode->append_child($descNode);
$styleUrl = $dom->create_element('styleUrl', '#' . $row['type'] . 'Style');
$placeNode->append_child($styleUrl);
// Point.
$pointNode = $dom->create_element('Point');
$placeNode->append_child($pointNode);
// coordinates.
$coorStr = $row['lng'] . ',' . $row['lat'];
$coorNode = $dom->create_element('coordinates', $coorStr);
$pointNode->append_child($coorNode);
}
$kmlOutput = $dom->dump_mem(TRUE, 'UTF-8');
header('Content-type: application/vnd.google-earth.kml+xml');
echo $kmlOutput;
?>
createElement
). In PHP4, this name is written in lower case with the “_” character necessary to separate the words in the function name.DOMDocument
, and in PHP4 it is domxml_new_doc
.echo
function to output KMLSELECT *
query to the table with markers;<Placemark>
element and pass it the values ​​of the name of the institution and its address through the htmlentities
function to exclude the entry of special characters;<?php
require('phpsqlajax_dbinfo.php');
// MySQL-.
$connection = mysql_connect ($server, $username, $password);
if (!$connection)
{
die(' : ' . mysql_error());
}
// .
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected)
{
die (' : ' . mysql_error());
}
// markers.
$query = 'SELECT * FROM markers WHERE 1';
$result = mysql_query($query);
if (!$result)
{
die(' : ' . mysql_error());
}
// .
$kml = array('<? xml version="1.0" encoding="UTF-8"?>');
$kml[] = '<kml xmlns="http://earth.google.com/kml/2.1">';
$kml[] = ' <Document>';
$kml[] = ' <Style id="restaurantStyle">';
$kml[] = ' <IconStyle id="restuarantIcon">';
$kml[] = ' <Icon>';
$kml[] = ' <href>http://maps.google.com/mapfiles/kml/pal2/icon63.png</href>';
$kml[] = ' </Icon>';
$kml[] = ' </IconStyle>';
$kml[] = ' </Style>';
$kml[] = ' <Style id="barStyle">';
$kml[] = ' <IconStyle id="barIcon">';
$kml[] = ' <Icon>';
$kml[] = ' <href>http://maps.google.com/mapfiles/kml/pal2/icon27.png</href>';
$kml[] = ' </Icon>';
$kml[] = ' </IconStyle>';
$kml[] = ' </Style>';
// .
while ($row = @mysql_fetch_assoc($result))
{
$kml[] = ' <Placemark id="placemark' . $row['id'] . '">';
$kml[] = ' <name>' . htmlentities($row['name']) . '</name>';
$kml[] = ' <description>' . htmlentities($row['address']) . '</description>';
$kml[] = ' <styleUrl>#' . ($row['type']) .'Style</styleUrl>';
$kml[] = ' <Point>';
$kml[] = ' <coordinates>' . $row['lng'] . ',' . $row['lat'] . '</coordinates>';
$kml[] = ' </Point>';
$kml[] = ' </Placemark>';
}
// XML-
$kml[] = ' </Document>';
$kml[] = '</kml>';
$kmlOutput = join("\n", $kml);
header('Content-type: application/vnd.google-earth.kml+xml');
echo $kmlOutput;
?>
<? xml version = "1.0" encoding = "UTF-8" ? >
< kml xmlns = "http://earth.google.com/kml/2.1" >
< Document >
< Style id = "restaurantStyle" >
< IconStyle id = "restuarantIcon" >
< Icon >
< href > http://maps.google.com/mapfiles/kml/pal2/icon63.png </ href >
</ Icon >
</ IconStyle >
</ Style >
< Style id = "barStyle" >
< IconStyle id = "barIcon" >
< Icon >
< href > http://maps.google.com/mapfiles/kml/pal2/icon27.png </ href >
</ Icon >
</ IconStyle >
</ Style >
< Placemark id = "placemark1" >
Pan Africa Market </ name >
< description > 1521 1st Ave, Seattle, WA </ description >
< styleUrl > #restaurantStyle </ styleUrl >
< Point >
< coordinates > -122.340141,47.608940 </ coordinates >
</ Point >
</ Placemark >
< Placemark id = "placemark2" >
< name > Buddha Thai & Bar </ name >
< description > 2222 2nd Ave, Seattle, WA </ description >
< styleUrl > #barStyle </ styleUrl >
< Point >
< coordinates > -122.344391,47.613590 </ coordinates >
</ Point >
</ Placemark >
< Placemark id = "placemark3" >
< name > The Melting Pot </ name >
< description > 14 Mercer St, Seattle, WA </ description >
< styleUrl > #restaurantStyle </ styleUrl >
< Point >
< coordinates > -122.356445,47.624561 </ coordinates >
</ Point >
</ Placemark >
< Placemark id = "placemark4" >
< name > Ipanema Grill </ name >
< description > 1225 1st Ave, Seattle, WA </ description >
< styleUrl > #restaurantStyle </ styleUrl >
< Point >
< coordinates > -122.337654,47.606365 </ coordinates >
</ Point >
</ Placemark >
< Placemark id = "placemark5" >
< name > Sake House </ name >
< description > 2230 1st Ave, Seattle, WA </ description >
< styleUrl > #barStyle </ styleUrl >
< Point >
< coordinates > -122.345673,47.612823 </ coordinates >
</ Point >
</ Placemark >
< Placemark id = "placemark6" >
< name > Crab Pot </ name >
< description > 1301 Alaskan Way, Seattle, WA </ description >
< styleUrl > #restaurantStyle </ styleUrl >
< Point >
< coordinates > -122.340363,47.605961 </ coordinates >
</ Point >
</ Placemark >
< Placemark id = "placemark7" >
< name > Mama's Mexican Kitchen </ name >
< description > 2234 2nd Ave, Seattle, WA </ description >
< styleUrl > #barStyle </ styleUrl >
< Point >
< coordinates > -122.345467,47.613976 </ coordinates >
</ Point >
</ Placemark >
< Placemark id = "placemark8" >
< name > Wingdome </ name >
< description > 1416 E Olive Way, Seattle, WA </ description >
< styleUrl > #barStyle </ styleUrl >
< Point >
< coordinates > -122.326584,47.617214 </ coordinates >
</ Point >
</ Placemark >
< Placemark id = "placemark9" >
< name > Piroshky Piroshky </ name >
< description > 1908 Pike pl, Seattle, WA </ description >
< styleUrl > #restaurantStyle </ styleUrl >
< Point >
< coordinates > -122.342834,47.610126 </ coordinates >
</ Point >
</ Placemark >
</ Document >
</ kml >
<Placemark>
element. <linestring>
inside <Placemark>
. Then we will perform a query to the database and sample the coordinates by the id
field.<?php
require('phpsqlajax_dbinfo.php');
// MySQL-
$connection = mysql_connect ($server, $username, $password);
if (!$connection)
{
die(' : ' . mysql_error());
}
//
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected)
{
die (' : ' . mysql_error());
}
// markers
$query = " SELECT GROUP_CONCAT(lng, ',', lat, ',', '100' separator ' ') AS coordinates FROM markers WHERE type = 'restaurant';";
$result = mysql_query($query);
if (!$result)
{
die(' : ' . mysql_error());
}
// KML-,
$dom = new DOMDocument('1.0','UTF-8');
// KML Document
$node = $dom->createElementNS('http://earth.google.com/kml/2.1','kml');
$parNode = $dom->appendChild($node);
// Folder
$fnode = $dom->createElement('Folder');
$folderNode = $parNode->appendChild($fnode);
//
$row = @mysql_fetch_assoc($result);
// Placemark
$node = $dom->createElement('Placemark');
$placeNode = $folderNode->appendChild($node);
// id
$placeNode->setAttribute('id','linestring1');
// name, description adress
$nameNode = $dom->createElement('name','My path');
$placeNode->appendChild($nameNode);
$descNode= $dom->createElement('description', 'This is the path that I took through my favorite restaurants in Seattle');
$placeNode->appendChild($descNode);
// LineString
$lineNode = $dom->createElement('LineString');
$placeNode->appendChild($lineNode);
$exnode = $dom->createElement('extrude', '1');
$lineNode->appendChild($exnode);
$almodenode =$dom->createElement(altitudeMode,'relativeToGround');
$lineNode->appendChild($almodenode);
// coordinates
$coorNode = $dom->createElement('coordinates',$row['coordinates']);
$lineNode->appendChild($coorNode);
$kmlOutput = $dom->saveXML();
// KML
header('Content-type: application/vnd.google-earth.kml+xml');
echo $kmlOutput;
?>
<? xml version = '1.0' encoding = 'UTF-8' ? >
< kml xmlns = 'http: //earth.google.com/kml/2.1' >
< Folder >
< Placemark id = 'linestring1' >
< path > my path </ name >
< description >
< LineString >
< extrude > 1 </ extrude >
< altitudeMode > relativeToGround </ altitudeMode >
< coordinates > -122.340141,47.608940,100 -122.356445,47.624561,100
-122.337654,47.606365,100 -122.340363,47.605961,100
-122.342834,47.610126,100
</ coordinates >
</ LineString >
</ Placemark >
</ Folder >
</ kml >
<? xml version = '1.0' encoding = 'UTF-8' ? >
< kml xmlns = 'http://earth.google.com/kml/2.1' >
< Folder >
< NetworkLink >
< Link >
< href > http://example.com/phpsql_genkml.kml </ href >
< refreshMode > onInterval </ refreshMode >
< refreshInterval > 3600 </ refreshInterval >
</ Link >
</ Networklink >
< NetworkLink >
< Link >
< href > http://example.com/phpsql_genkml_ls.kml </ href >
< refreshMode > onInterval </ refreshMode >
< refreshInterval > 3600 </ refreshInterval >
</ Link >
</ Networklink >
</ Folder >
</ kml >
function load ()
{
var map;
var geoXml;
if (GBrowserIsCompatible ())
{
map = new GMap2 ( document .getElementById ( 'map' ));
map.addControl ( new GSmallMapControl ());
map.addControl ( new GMapTypeControl ());
geoXml = new GGeoXml ( 'http://example.com/phpmysql_kmlnl.kml' );
map.addOverlay (geoXml);
map.setCenter ( new GLatLng (47.613976, -122.345467), 13);
}
}
<NetworkLink>
files, that use <viewFormat>
. This function allows you to send any parameters to your script. You can also use this function to modify the returned data. Or use <TimeStamp>
or <TimeSpan>
, which allow you to animate your tags. You can also create a database table with a more complex structure, which will store data about polygons <Polygons>
, which can also be displayed on the map. Or create a page with which other people can also add new data to your card. You have endless possibilities!Source: https://habr.com/ru/post/38448/
All Articles