Use PHP, MySql and Google Map API v3 for displaying data on map

Displaying multiple=Displaying data on maps can be useful in many situations.

By integrating tools like PHP, MySQL and Google Maps, you can relatively easy build customized maps for your website or blog.

In this post we’ll take a closer look on the possibilities, and build a  interactive map based on PHP, MySql and Google Map API v3.

To see what we’re building, see live example here

Geocoded data

The script in this post uses MySQL for storing the data that’s going to be displayed on the map. This method works fine if you’re adding multiple points to your map (10+), and want a dynamic way to retrieve data data. If you’re going to display less than 10 Points the solution in this post is a little overkill.

Before proceeding, you need some geocoded data (data that contains lat/long information) to display on the map. If you don’t have geocoded data, you can find a post here, where you can learn how to geocode addresses for usage on eg. Google Maps.

You can use the following test data for this example:

 `name` text NOT NULL,
 `desc` text NOT NULL,
 `lat` text NOT NULL,
 `lon` text NOT NULL,

-- Data dump for the table `poi_example`

INSERT INTO `poi_example` (`id`, `name`, `desc`, `lat`, `lon`) VALUES
(1, '100 Club', 'Oxford Street, London  W1<br/>3 Nov 2010 : Buster Shuffle<br/>', '51.514980', '-0.144328'),
(2, '93 Feet East', '150 Brick Lane, London  E1 6RU<br/>7 Dec 2010 : Jenny & Johnny<br/>', '51.521710', '-0.071737'),
(3, 'Adelphi Theatre', 'The Strand, London  WC2E 7NA<br/>11 Oct 2010 : Love Never Dies', '51.511010', '-0.120140'),
(4, 'Albany, The', '240 Gt. Portland Street, London  W1W 5QU', '51.521620', '-0.143394'),
(5, 'Aldwych Theatre', 'Aldwych, London  WC2B 4DF<br/>11 Oct 2010 : Dirty Dancing', '51.513170', '-0.117503'),
(6, 'Alexandra Palace', 'Wood Green, London  N22<br/>30 Oct 2010 : Lynx All-Nighter', '51.596490', '-0.109514');

When you have a MySQL database with Geocoded content you’re ready to proceed.

Extracting from MySQL

Next thing is to create a small piece of PHP that can connect and extract data from MySQL

// connection to mysql

$dbname            ='<database name>'; //Name of the database
$dbuser            ='<database username>'; //Username for the db
$dbpass            ='<database password>'; //Password for the db
$dbserver        ='<database server>'; //Name of the mysql server

$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());

// extracting and looping through data

 $query = mysql_query("SELECT * FROM poi_example");
 while ($row = mysql_fetch_array($query)){
echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');\n");

As you can see, the output of the script is has the following format: “addMarker(lat, long, marker data);“, this is the marker format that you can use for adding multiple markers fir Google Maps API V3.

Google Maps API V3

Next thing is to embed the PHP code into the javascript required for displaying multiple infowindows in Google Map API V3.

For this example, we have used the code from August LI as inspiration.

The script has the following features:

  • “var icon”: specifies a customizable icon. In this example a icon from Google is used, but you can add your own icons as well
  • “var popup”: specifies the maximum width of the info window. In this case 300 pixels
  • “addMarker”: contains latitude and longitude of the points, and a possibility to display whatever HTML content you like in the info window that corresponds to each point.   In this example we’re just displaying a headline and description
  • The map automatically zooms and centers to the most detailed view where all the map markers can be displayed in the same map. This is automatically calculated based on the size of the map, and the locations of the map markers.

The most important limit of this script is, that this technique is primarily useful to display a limited amount of map markers (below 100). If you’re going to display more points, you should consider a marker cluster solution.

The final script with the PHP and javascript code merged looks like this:

$dbname            ='insert mysql database name'; //Name of the database
$dbuser            ='insert mysql user name'; //Username for the db
$dbpass            ='insert mysql password'; //Password for the db
$dbserver          ='insert mysql database server address'; //Name of the mysql server

$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());
 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 <title>Google Map API V3 with markers</title>
 <style type="text/css">
 body { font: normal 10pt Helvetica, Arial; }
 #map { width: 350px; height: 300px; border: 0px; padding: 0px; }
 <script src="" type="text/javascript"></script>
 <script type="text/javascript">
 //Sample code written by August Li
 var icon = new google.maps.MarkerImage("",
 new google.maps.Size(32, 32), new google.maps.Point(0, 0),
 new google.maps.Point(16, 32));
 var center = null;
 var map = null;
 var currentPopup;
 var bounds = new google.maps.LatLngBounds();
 function addMarker(lat, lng, info) {
 var pt = new google.maps.LatLng(lat, lng);
 var marker = new google.maps.Marker({
 position: pt,
 icon: icon,
 map: map
 var popup = new google.maps.InfoWindow({
 content: info,
 maxWidth: 300
 google.maps.event.addListener(marker, "click", function() {
 if (currentPopup != null) {
 currentPopup = null;
 }, marker);
 currentPopup = popup;
 google.maps.event.addListener(popup, "closeclick", function() {
 currentPopup = null;
 function initMap() {
 map = new google.maps.Map(document.getElementById("map"), {
 center: new google.maps.LatLng(0, 0),
 zoom: 14,
 mapTypeId: google.maps.MapTypeId.ROADMAP,
 mapTypeControl: false,
 mapTypeControlOptions: {
 style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
 navigationControl: true,
 navigationControlOptions: {
 style: google.maps.NavigationControlStyle.SMALL
 $query = mysql_query("SELECT * FROM poi_example");
 while ($row = mysql_fetch_array($query)){
 echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');\n");
 center = bounds.getCenter();

 <body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
 <div id="map"></div>

The final result looks like this:

Using Google Map API v3, php and mysql to display multiple=

See live example here


With the sample data and scripts in this post, you can easily get started working with multiple map markers on Google Map API V3. As mentioned earlier, the only limit in this script is that if performs best with < 100 markers at the same time.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

52 Replies to Use PHP, MySql and Google Map API v3 for displaying data on map

  1. Ampped Aeon says:

    Having an issue getting marker clustering to work with the above code. Any ideas why?

Trackbacks for this post

  1. Google map wizz level | Zenk

Comments are now closed for this article.