How to Get Address From mySQL via PHP in Google Map

this is a simple tutorial how we get address from database and then view the address location using google map.
first create a company company

CREATE  TABLE  `address` (
`id` INT NOT  NULL  AUTO_INCREMENT  PRIMARY  KEY ,
 `company_name` VARCHAR( 100  )  NOT  NULL ,
 `address` VARCHAR( 255  )  NOT  NULL ,
 `city` VARCHAR( 100  )  NOT  NULL
) ENGINE  =  MYISAM ;

let’s insert some data

INSERT  INTO  `address` (  `id` ,  `nama_perusahaan` ,  `alamat` ,  `kota`  )
VALUES (
NULL ,  'akatsuki corp',  'sudirman',  'jakarta'
), (
NULL ,  'konoha corp',  'kuningan',  'jakarta'
);

and this is the code for view the company location on the map.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />

<?
 mysql_connect("localhost","root","");
 mysql_select_db("myblog");
 $q="select * from address";
 $res=mysql_query($q);
 $data=array();
 while($datarow=mysql_fetch_assoc($res))
 {
 $data[]=$datarow['company_name']."|".$datarow['address']."|".$datarow['city'];
 }
 $numdata=count($data);
?>
 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=yourkey&sensor=false"
 type="text/javascript"></script>
 <script type="text/javascript">

 function initialize() {
 if (GBrowserIsCompatible()) {
 var map = new GMap2(document.getElementById("map_canvas"));
 var geocoder = new GClientGeocoder();

 map.addControl(new GSmallMapControl());
 map.addControl(new GMapTypeControl());
 map.setCenter(new GLatLng(37.4419, -122.1419), 13);

 <?
 for ($i=0;$i<$numdata;$i++)
 {
 //get each company data
 $companydata=explode("|",$data[$i]);
 $name=$companydata[0];
 $address=$companydata[1];
 $city=$companydata[2];

 ?>
 var address="<? echo $address;?>"+"+"+"<? echo $city;?>";
 geocoder.getLatLng(address,
 function(point) {
 if (!point) {
 alert(address + " not found");
 } else {
 map.setCenter(point, 12);
 var marker = new GMarker(point);
 map.addOverlay(marker);
 //marker.openInfoWindowHtml(address);
 }
 }
 );

 <? } ?>
 //}
 //});
 }
 }
 </script>

<title>Mehta Websolution</title>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:500px; height:300px;"></div>
</body>
</html>

Source : Share My php

Leave a Comment