
Process for using google map api in php:
-> Create a table in database.
-> A file to create a dynamic xml creator code in php
-> Index file to load and display the map including location indicator
Create table in database:
[php]
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 ;
[/php]
Insert the values of attributes in databases table.
as by code:
[php]
INSERT INTO `markers` (`name`, `address`, `lat`, `lng`, `type`) VALUES ('Mr.Insafeta', 'Mohammadpor, Dhaka, Bangladesh', '47.608941', '-122.340145', 'Home');
[/php]
Create database connect page in php (dbinfo.php):
[php]
<?
$username="username";
$password="password";
$database="username-databaseName";
?>
[/php]
Create a php code to auto generate xml file (genxml.php):
You have three process to generate xml file dynamically by php.
Process 1. If you sure that you are using php 5.1 and work domxml_new_doc() function on your sarver.
[php]
<?php
require("dbinfo.php");
// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$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;
?>
[/php]
Process 2. If you use php 4 and domxml_new_doc() function not work in sarver then you can create xml by manually using echo.
[php]
<?php
require("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;
}
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
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 '/>';
}
// End XML file
echo '</markers>';
?>
[/php]
Process 3. You can create xml file by this way too.
[php]
<?php
require("dbinfo.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$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();
?>
[/php]
Now create a index.php page to load map and display location from database informations:
[php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps AJAX + MySQL/PHP Example</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAjU0EJWnWPMv7oQ-jjS7dYxTPZYElJSBeBUeMSX5xXgq6lLjHthSAk20WnZ_iuuzhMt60X_ukms-AUg"
type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
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;
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(47.614495, -122.341861), 13);
GDownloadUrl("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);
}
});
}
}
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;
}
//]]>
</script>
</head>
<body onload="load()" onunload="GUnload()">
<div id="map" style="width: 500px; height: 300px"></div>
</body>
</html>
[/php]
Now run index.php script by browsing. wow what you did !!! I think its your desired location on map.
This is truly a genuinely great review for me, should admit which you are between the best bloggers I ever saw.Thanks for posting this informative article.
ReplyDeleteI am so glad this particular internet factor works and your post truly solved the problem. Usually takes a person up on that home help a person
ReplyDeletei really like what youve done above the fold
ReplyDeletei like the design.......... what font are these comments in?
ReplyDeleteHey! This post couldn't be written any better! Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Fairly certain he will have a good read. Many thanks for sharing!
ReplyDeleteI just want to say I am beginner to weblog and absolutely loved your web site. Likely I’m likely to bookmark your blog . You surely have wonderful article content. Thanks for sharing with us your web-site.
ReplyDeleteI just want to mention I am beginner to blogs and seriously savored you're web site. Probably I’m planning to bookmark your blog post . You actually have terrific articles and reviews. Many thanks for sharing with us your website page.
ReplyDeleteI simply want to mention I am just new to blogging and site-building and really loved this page. More than likely I’m likely to bookmark your website . You really have incredible well written articles. Thanks a lot for sharing your web site.
ReplyDeleteHmm is anyone else encountering problems with the images on this blog loading? I'm trying to find out if its a problem on my end or if it's the blog. Any feed-back would be greatly appreciated.
ReplyDeleteThis is a amazing site, would you be interested in doing an interview about just how you produced it? If so e-mail me!
ReplyDeleteJust discovered this blog through Google, what a pleasant surprise!
ReplyDeleteThis really solved my problem, thank you!
ReplyDeleteDo you have a spam problem on this site; I also am a blogger, and I was wondering your scenario; we have developed some good strategies and were looking to exchange solutions with other individuals, be positive to fire me an e-mail if serious.
ReplyDeleteI believe this web site holds some rattling wonderful info for everyone :D.
ReplyDeleteYou need to really moderate the commentary here
ReplyDeleteIm having a teeny problem. I cant get my reader to pickup your rss feed, Im using google reader by the way.
ReplyDeleteHow come you dont have your site viewable in wap format? Can not view anything in my iPad.
ReplyDeleteHi there very nice blog!! Man .. Excellent .. Amazing .. I'll bookmark your site and take the feeds alsoโ€ฆI'm glad to seek out so many useful info right here within the post, we need work out extra strategies in this regard, thanks for sharing.
ReplyDeleteYou are a very intelligent individual!
ReplyDeleteI feel one of your current ads initiated my web browser to resize, you may well want to get that on your blacklist.
ReplyDeleteI am continuously browsing online for articles that can aid me. Thx!
ReplyDelete[url=http://trustedpaydayloanonline.com/#18792]payday loan online[/url] - payday loan online , http://trustedpaydayloanonline.com/#21703 payday loan online
ReplyDeletei dnt knw how to use skype .any mates can help me plzzz
ReplyDeleteI have been reading this blog for any great though. Keep up the great job you are doing here.
ReplyDeleteSome genuinely wondrous work on behalf of the owner of this site, dead outstanding content material.
ReplyDelete