Google Map Integration in PHP

Introduction

This article explains Google Maps Integration in PHP. You can integrate Google Maps into your pages but before doing that you need the Google Maps API Library version 3.0. You can download the Google Maps Library from http://www.github.com/ and after downloading the library you will put it in your PHP directory and set your path in your PHP script to that directory.

We will first define a boilerplate template and save the "googlemap_template.php" file.

<html>

<head>

<?php

//call this function from this Jsmin file

echo $G_map->getHeaderJS();

echo $G_map->getMapJS();

?>

</head>

<body>

<?php

//print Onload function

echo $G_map->printOnLoad();

//print map

echo $G_map->printMap();

//print sidebar

echo $G_map->printSidebar();

?>

</body>

</html>

Example

Google Maps satellite imagery, single marker example.

<?php

error_reporting(E_ALL ^ E_NOTICE);

//include google map API library file

require_once("src/GoogleMap.php");

require_once("src/JSMin.php");

//object of google map api class

$G_map = new GoogleMapAPI();

$G_map->addMarkerByAddress(

"Eiffel Tower, Paris, France",

"First Example of goole map",

"Eiffel Tower Description" );

//include google template file

require_once('gmap_template.php');

?>

Output

google1.jpg

Example 

This is an example of the Traffic Routes Overlay.

<?php

error_reporting(E_ALL ^ E_NOTICE);

//include google map API library file

require_once("src/GoogleMap.php");

require_once("src/JSMin.php");

//object of google map api class

$G_map = new GoogleMapAPI();

$G_map->addMarkerByAddress( "new york, NY", "traffic of new york", "example of new york traffic map" );

$G_map->setMapType( 'map' );

$G_map->setZoomLevel( 16 );

$G_map->enableTrafficOverlay();

//include google template file

require_once('gmap_template.php');

?>

Output

google2.jpg

Example

Google Maps satellite imagery, multiple marker example:

<?php

error_reporting(E_ALL ^ E_NOTICE);

//include google map API library file

require_once("src/GoogleMap.php");

require_once("src/JSMin.php");

//object of google map api class

$G_map = new GoogleMapAPI();

$G_map->addMarkerByAddress( "Saskatoon, SK", "", "Home" );

$G_map->addMarkerByAddress( "Vancouver, BC", "", "West Coast" );

$G_map->addMarkerByAddress( "Montreal, QC", "", "Hockey" );

$G_map->addMarkerByAddress( "Playa del Carmen, Mexico", "", "Tropical vacation" );

$G_map->setMapType( 'terrain' );

//include google template file

require_once('gmap_template.php');

?> 

Output

google3.jpg

As you can see, use of the Google Maps Library for displaying Google Maps is very easy.


Similar Articles