lat
(latitude) and lng
(longitude) card parameters. With the current resolution of Google Maps, we only need 6 digits after the decimal point. To reduce the disk space occupied by the database to a minimum, for lat
(latitude) and lng
(longitude) values, it is recommended to select the FLOAT
data type with parameter 10.6. This type allows you to store up to 6 decimal places and up to 4 before the comma, for example -123.456789 degrees. Your table should have an id
field, which will be the primary key for access to the records, as well as a type
field in which the institution type will be recorded (restaurant or bar).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";
?>
domxml_new_doc()
function to test the dom_xml
functionality. If you have access to dom_xml
functions, you can use them to create XML nodes and display the XML document on the screen. Using dom_xml
functions dom_xml
you can create XML documents of almost any complexity.SELECT *
(select all) to the table named “markers” and go through all the results of this sample. For each record in the table (each establishment), a node will be created in the XML document, the attributes of which will be fields from the corresponding table entry - this node will be attached to the parent node. After that you will receive a ready-made XML document.<?php
require("phpsqlajax_dbinfo.php");
// XML-
$doc = domxml_new_doc(" 1.0 ");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);
// MySQL-
$connection=mysql_connect (localhost, $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());
}
header("Content-type: text/xml");
// ;
while ($row = @mysql_fetch_assoc($result)){
// XML
$node = $doc->create_element("marker");
$newnode = $parnode->append_child($node);
$newnode->set_attribute("name", $row['name']);
$newnode->set_attribute("address", $row['address']);
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
$newnode->set_attribute("type", $row['type']);
}
$xmlfile = $doc->dump_mem();
echo $xmlfile;
?>
dom_xml
functions, you can create the XML file using the echo
function. When using the echo
function, you will need to write an assistant function (for example, parseToXML
) that will decode some special characters (>, <, ', ”) so that no XML errors occur.SELECT *
query (select all) to a table named “markers”
and go through all the results of this sample. Then you need to display the parent node using the echo
function and walk through the results of the sample. In this case, you must use the parseToXML
function parseToXML
we parseToXML
. The execution of this script should end with the closing markers
tag.<?php
require("phpsqlajax_dbinfo.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// MySQL-
$connection=mysql_connect (localhost, $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());
}
header("Content-type: text/xml");
// XML-,
echo '<markers>';
// ;
while ($row = @mysql_fetch_assoc($result)){
// XML
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}
// XML-
echo '</markers>';
?>
SELECT *
(select all) to the table named “markers” and go through all the results of this sample. For each record in the table (each establishment), a node will be created in the XML document, the attributes of which will be fields from the corresponding table entry - this node will be attached to the parent node. After that you will receive a ready-made XML document.<?php
require("phpsqlajax_dbinfo.php");
// XML-
$dom = new DOMDocument(" 1.0 ");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// MySQL-
$connection=mysql_connect (localhost, $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());
}
header("Content-type: text/xml");
// ;
while ($row = @mysql_fetch_assoc($result)){
// XML
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("type", $row['type']);
}
echo $dom->saveXML();
?>
< markers >
< marker name = "Pan Africa Market" address = "1521 1st Ave, Seattle, WA" lat = "47.608940" lng = "-122.340141" type = "restaurant" />
< marker name = "Buddha Thai & Bar" address = "2222 2nd Ave, Seattle, WA" lat = "47.613590" lng = "-122.344391" type = "bar" />
< marker name = "The Melting Pot" address = "14 Mercer St, Seattle, WA" lat = "47.624561" lng = "-122.356445" type = "restaurant" />
< marker name = "Ipanema Grill" address = "1225 1st Ave, Seattle, WA" lat = "47.606365" lng = "-122.337654" type = "restaurant" />
< marker name = "Sake House" address = "2230 1st Ave, Seattle, WA" lat = "47.612823" lng = "-122.345673" type = "bar" />
< marker name = "Crab Pot" address = "1301 Alaskan Way, Seattle, WA" lat = "47.605961" lng = "-122.340363" type = "restaurant" />
< marker name = "Mama's Mexican Kitchen" address = "2234 2nd Ave, Seattle, WA" lat = "47.613976" lng = "-122.345467" type = "bar" />
< marker name = "Wingdome" address = "1416 E Olive Way, Seattle, WA" lat = "47.617214" lng = "-122.326584" type = "bar" />
< marker name = "Piroshky Piroshky" address = "1908 Pike pl, Seattle, WA" lat = "47.610126" lng = "-122.342834" type = "restaurant" />
</ markers >
GDownloadURL
. GDownloadURL
is like a wrapper for the XMLHttpRequest
method, which is used to send requests in an XML form. The first parameter of the GDownloadURL
function is the path to the XML file. The second parameter is a function that will be executed when receiving a response from XML.GDownloadURL
works asynchronously - the function, which is the second parameter of GDownloadURL
, will not work once you have applied this method. The larger your XML file, the longer it will take to call this parameter function. You should not put any code responsible for placing markers on the map, after the method GDownloadURL
- it must be placed in the function-parameter.creatMarker
function, which places the markers on the map.GDownloadUrl ( "phpsqlajax_genxml.php" , function (data) {
var xml = GXml.parse (data);
var markers = xml.documentElement.getElementsByTagName ( "marker" );
for ( var i = 0; i <markers.length; i ++) {
var name = markers [i] .getAttribute ( "name" );
var address = markers [i] .getAttribute ( "address" );
var type = markers [i] .getAttribute ( "type" );
var point = new GLatLng (parseFloat (markers [i] .getAttribute ( "lat" )),
parseFloat (markers [i] .getAttribute ( "lng" )));
var marker = createMarker (point, name, address, type);
map.addOverlay (marker);
}
});
GIcon
class to define arbitrary images that will be displayed as a marker. First we define two objects: iconBlue and iconRed.GIcon
object with a specific type of institution: a restaurant or a bar. This approach will help you in the future to easily understand the markers created using data from an external XML file.var iconBlue = new GIcon ();
iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png' ;
iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' ;
iconBlue.iconSize = new GSize (12, 20);
iconBlue.shadowSize = new GSize (22, 20);
iconBlue.iconAnchor = new GPoint (6, 20);
iconBlue.infoWindowAnchor = new GPoint (5, 1);
var iconRed = new GIcon ();
iconRed.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png' ;
iconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' ;
iconRed.iconSize = new GSize (12, 20);
iconRed.shadowSize = new GSize (22, 20);
iconRed.iconAnchor = new GPoint (6, 20);
iconRed.infoWindowAnchor = new GPoint (5, 1);
')
var customIcons = [];
customIcons [ "restaurant" ] = iconBlue;
customIcons [ "bar" ] = iconRed;
createMarker
function. Since the customIcons
array was defined globally, we can transfer its elements to the constructor of the GMarker
class without interference. Then you need to write the HTML code that will be displayed in the info window.function createMarker (point, name, address, type) {
var marker = new GMarker (point, customIcons [type]);
var html = "<b>" + name + "</ b> <br/>" + address;
GEvent.addListener (marker, 'click' , function () {
marker.openInfoWindowHtml (html);
});
return marker;
}
load
function is activated. This function displays a map, and then calls the GDownloadUrl
method. Make sure that the GDownloadUrl
function performs its work correctly and the XML file is displayed in the browser correctly.Source: https://habr.com/ru/post/38290/
All Articles