Home >Backend Development >PHP Tutorial >php google api interface program_PHP tutorial
php google api interface program
php google api interface program
Here is a completely funtional class to generate Google maps. Allows for multiple classes of customization, enabling users to quickly implement a website in minutes without any knowledge of JavaScript or Google Maps API.
class phproogleMap {
/*
* @The google api key
*/
private $apiKey ;
/*
* @the map zoom level
*/
private $mapZoom = 8 ;
/*
* @The width of the map div
*/
private $mapWidth = 350 ;
/*
* @The height of the map div
*/
private $mapHeight = 300 ;
/*
* @The map center
*/
private $mapCenter ;
/*
* The array of map types
*/
private $ googleMapTypes ;
/*
* @The map type
*/
private $mapType = 'normal' ;
/*
* @The array of marker points
*/
private $markerPoints = array();
/*
* @The array of marker addresses
*/
private $markerAddresses = array( );
/*
* @The maps controls
*/
private $googleMapControls = array();
/*
* @The ID of the map div
*/
private $mapDivID ;
/*
* The constructor
*
* @param apiKey
*
* @access public
*
* @return void
*
*/
public function __construct ( $apiKey = null )
{
$this -> apiKey = is_null ( $ apiKey ) ? '' : $apiKey ;
/*** set the map types ***/
$this -> setGoogleMapTypes ();
}
/*
*
* @setter
*
* @access public
*
*/
public function __set ( $name , $value )
{
switch ( $ name )
{
case 'apiKey' :
if(! is_string ( $value ))
{
throw new Exception ( $name , $value , 'string' );
}
$this -> $name = $value ;
break;
case 'mapZoom' :
if( filter_var ( $value , FILTER_VALIDATE_INT , array( "options" = > array( "min_range" => 0 , "max_range" => 19 ))) == false )
{
throw new Exception ( " $name is out of range" );
}
$this -> $name = $value ;
break;
case 'mapWidth' :
if( filter_var ( $value , FILTER_VALIDATE_INT , array( "options" => ; array( "min_range" => 100 , "max_range" => 900 ))) == false )
{
throw new Exception ( " $name is out of range for" );
}
$this -> $name = $value ;
break;
case 'mapHeight' :
if( filter_var ( $value , FILTER_VALIDATE_INT , array( "options" => ; array( "min_range" => 100 , "max_range" => 900 ))) == false )
{
throw new Exception ( " $name is out of range for" );
}
$this -> $name = $value ;
break;
case 'mapType' :
if(! array_key_exists ( $value , $this -> googleMapTypes ) )
{
throw new Exception ( " $name is not a valid map type" );
}
$this -> $name = $value ;
break;
case 'mapDivID' :
if( ! is_string ( $value ) )
{
throw new Exception ( " $name is not a valid ID" );
}
$this -> $name = $value ;
break;
case 'mapCenter' :
if( ! is_array ( $value ) )
{
throw new Exception ( " $ name is not a valid array" );
}
$this -> $name = $value ;
break;
default:
throw new Exception ( "Invalid Parameter $name " );
}
}
/*
*
* @getter
*
* @access public
*
*/
public function __get ( $name )
{
switch ( $name )
{
case 'apiKey' :
return $this -> apiKey ;
break;
case 'mapZoom' :
return $this -> mapZoom ;
break;
case 'mapWidth' :
return $this -> ; mapWidth ;
break;
case 'mapHeight' :
return $this -> mapHeight ;
break;
case 'mapType' :
return $this -> mapType ;
break;
case 'mapDivID' :
return $this -> mapDivID ;
break;
case 'mapCenter' ;
return $this -> mapCenter ;
break;
}
/*** if we are here, throw an excepton ***/
throw new Exception ( " $name is invalid" );
}
/*
*
* @isset
*
* @access public
*
*/
public function __isset ( $name )
{
switch ( $name )
{
case 'apiKey' :
$this -> apiKey = $name ;
break;
case 'mapZoom' :
$this -> mapZoom = $name ;
break;
case 'mapWidth' :
$this -> mapWidth = $name ;
break ;
case 'mapHeight' :
$this -> mapHeight = $name ;
break;
case 'mapType' :
$this -> mapType = $name ;
break;
case 'mapDivID' :
$this -> mapDivID = $name ;
break;
case 'mapCenter' ;
$this -> mapCenter = $name ;
break;
default:
return false ;
}
}
/*
*
* @Set the map types
*
* @access private
*
* @return void
*
*/
private function setGoogleMapTypes ()
{
$this -> googleMapTypes = array( 'physical' => 'G_PHYSICAL_MAP' , 'normal' => 'G_NORMAL_MAP' , 'satellite' => 'G_SATELLITE_MAP' , 'hybrid' => 'G_HYBRID_MAP' );
}
/*
*
* @add to the array of google maps controls
*
* @access public
*
* @return void
*
*/
public function addGoogleMapControl ( $control )
{
$n = sizeof ( $this -> googleMapControls );
$this -> googleMapControls [] = $control ;
}
/*
*
* @get pinpoint marker by address
*
* @access public
*
* @param string $address
*
* @param string $html
*
* @return void
*
*/
public function addMarkerAddress ( $address , $html )
{
$s = sizeof ( $this -> markerAddresses );
$this -> markerAddresses [ $s ][ 'address' ] = $address ;
$this -> markerAddresses [ $s ][ 'html' ] = $html ;
}
/*
*
* @get pinpoint mark by latitude or longitude
*
* @access public
*
* @param string $lat
*
* @param string $long
*
* @param string $html
*
* @return void
*
*/
public function addMarker ( $lat , $long , $html )
{
$pointer = sizeof ( $this -> markerPoints );
$this -> markerPoints [ $pointer ][ 'lat' ] = $lat ;
$this -> markerPoints [ $pointer ][ 'long' ] = $long ;
$this -> markerPoints [ $pointer ][ 'html' ] = $html ;
}
/*
*
* @The javascript for google to connect
*
* @access public
*
* @return string
*
*/
public function googleJS ()
{
return '' . "n" ;
}
private function noJavascript ()
{
return '' ;
}
public function drawMap ()
{
$js = '
$js .= '
' ;
return $js ;
}
} /*** end of class ***/
try
{
/*** a new phproogle instance ***/
$map = new phproogleMap ();
/*** the google api key ***/
$map -> apiKey = 'YOUR_GOOGLE_API_KEY' ;
/*** zoom is 0 - 19 ***/
$map -> mapZoom = 14 ;
/*** the map width ***/
$map -> mapWidth = 350 ;
/*** the map height ***/
$map -> mapHeight = 300 ;
/*** set the map type ***/
$map -> mapType = 'normal' ;
/*** set the map center ***/
$map -> mapCenter = array(- 33.862828 , 151.216974 );
/*** add some markers with latitude and longitude ***/
$map -> addMarker (- 33.858362 , 151.214876 , '
For those with culture
' );
/*** add some controls ***/
$map -> addGoogleMapControl ( 'GMapTypeControl' );
$map -> addGoogleMapControl ( 'GSmallMapControl' );
$map -> addGoogleMapControl ( 'GOverviewMapControl' );
/*** add some marker addresses ***/
$map -> addMarkerAddress ( '2 Pitt St Sydney NSW Australia' , '
/*** set the map div id ***/
$map -> mapDivID = 'map' ;
}
catch( Exception $e )
{
echo $e -> ; getMessage ();
}
?>
Commonly used Google map development parameters
phproogle::apiKey
. This is Google’s user API key. .This can also be set using the constructor when a new instance is instantiated.
phproogle::addGoogleMapControl() phproogle::addGoogleMapControl()
This method sets multiple controls on the Google Map, such as zoom control controller, map control, etc. This method can be called multiple times to set up multiple map controls. Options are:
* GLargeMapControl GLargeMapControl
* G ScaleControl GScaleControl
* GMapTypeControl GMapTypeControl
GOverviewMapControl
addGoogleMapControl ( "GSmallMapControl" ); ?>
phproogle::mapType phproogle::mapType
phproogle::mapCenter phproogle::mapCenter
This will set the center of the map within the partition range. The value is a sequence containing the latitude and longitude of the map center. > this The map zoom level will be set. A value of zero is the widest zoom level and displays the entire world. Although the 19th is the highest zoom, the buildings will be shown. Each zoom level doubles the zoom. Each zoom level doubles the zoom. 12. The default value is 12.
phproogle::addMarker() phproogle::addMarker()
* float $latitude float$latitude
* float $longitude float$longitude
* string $html string$ HTML
. Longitude and latitude are both numeric values and HTML can be any HTML or text that will be inside the markup balloon.
phproogle::addMarkerAddress() phproogle::addMarkerAddress()
This method is similar to phproogle::addMarker() and places a marker on the map. However, instead of accepting longitude and latitude to place the flag, this method requires two values.
* string $address string$ address
* string $html string$ HTML
The address of the parameter, such as "Simple address from Erjie Palace to Paris, France". balloon.In the HTML parameter again, any text or HTML is placed inside the message balloon.
When set, this will set the group ID that the map resides in. The default value is "map"
phproogle::mapWidth phproogle::mapWidth
As the name suggests, this will Set the width of the div that the map lives in with a default width of 350 pixels.
This will set the height of the partition that the map resides in. The default value is 300 pixels.
phproogle::googleJS() phproogle::googleJS()
This method returns a JavaScript string that is used in file headers, most often to display the connection string with the Google API key.
This method returns the map itself created by JavaScript.