php google api 介面程式_PHP教程

來源:互聯網
上載者:User
php google api 介面程式

php google api 介面程式

這裡是一個完全funtional類產生Google地圖。 允許多類定製,使使用者能夠快速實現在幾分鐘之內就沒有任何網站的JavaScript或Google地圖Google地圖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 'JavaScript must be enabled in order for you to use Google Maps.
However, it seems JavaScript is either disabled or not supported by your browser.
To view Google Maps, enable JavaScript by changing your browser options, and then
try again.
' ;
}


public function drawMap ()
{
$js = ' mapDivID . '" style="width: ' . $this -> mapWidth . 'px; height: ' . $this -> mapHeight . 'px">' ;
$js .= $this -> noJavascript ();

$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 , '

Sydney Opera House

For those with culture

' );
$map -> addMarker (- 33.862828 , 151.216974 , '

Royal Botanic Gardens

A link here' );


/*** add some controls ***/
$map -> addGoogleMapControl ( 'GMapTypeControl' );
$map -> addGoogleMapControl ( 'GSmallMapControl' );
$map -> addGoogleMapControl ( 'GOverviewMapControl' );

/*** add some marker addresses ***/
$map -> addMarkerAddress ( '2 Pitt St Sydney NSW Australia' , '

Head Office

' );
$map -> addMarkerAddress ( '122 Pitt St Sydney NSW Australia' , '

The Factory

' );

/*** set the map div id ***/
$map -> mapDivID = 'map' ;
}
catch( Exception $e )
{
echo $e -> getMessage ();
}
?>



googleJS (); ?>


drawMap (); ?>

常用的google地圖開發參數

phproogle::apiKey
.這是Google的使用者API密鑰。 .這也可以設定使用時,一個新的執行個體是instanciated構造。
phproogle::addGoogleMapControl() phproogle::addGoogleMapControl()
此方法設定,如縮放控制控制器的Google地圖,地圖控制等此方法可以多次調用設定地圖多個控制項。 Options are:選項有:

* GLargeMapControl GLargeMapControl
* GSmallMapControl GSmallMapControl
* GSmallZoomControl GSmallZoomControl
* GScaleControl GScaleControl
* GMapTypeControl GMapTypeControl
* GHierarchicalMapTypeControl GHierarchicalMapTypeControl
* GOverviewMapControl GOverviewMapControl

addGoogleMapControl ( "GSmallMapControl" ); ?>
phproogle::mapType phproogle::mapType
This sets the map type to one of four options:這將設定地圖類型的四個選項之一:

* normal正常
* satellite衛星
* hybrid混合
* physical物理

mapType = "normal" ; ?>
phproogle::mapCenter phproogle::mapCenter
T這將設定分區範圍內的地圖中心。 該值是一個包含的緯度和經度的地圖中心的順序。
$map->mapCenter = array(-33.862828, 151.216974); 1 $map->mapCenter = array(-33.862828, 151.216974); 1
phproogle::mapZoom phproogle::mapZoom
這將設定地圖縮放層級。 零值是最廣泛的縮放層級,並顯示整個世界。 雖然19日是最高變焦,將顯示建築物。 Each zoom level doubles the zoom.每個縮放層級的兩倍變焦。 12.預設值為12。
phproogle::addMarker() phproogle::addMarker()
This function is used to create and place markers upon the map.這個函數用於建立和地點後,地表徵圖記。 the addMarker method takes three args.在addMarker argS的方法有三個。

* float $latitude浮動$緯度
* float $longitude浮動$經度
* string $html字串$的HTML

.經度和緯度都是數字值和HTML可以是任何HTML或文本將在標記球囊內。
phproogle::addMarkerAddress() phproogle::addMarkerAddress()
此方法類似於phproogle::addMarker()和地方在地圖上的標記。 然而,而不是接受經度和緯度來放置標誌,這種方法需要兩個值。

* string $address字串$地址
* string $html字串$的HTML

參數的地址,如“二街宮人至法國巴黎簡單的地址”。 balloon.在HTML參數再次,任何文本或HTML放置在資訊氣球。
phproogle::mapDivID phproogle::mapDivID
當設定,這將設定組ID的映射居住於預設值是“地圖”
phproogle::mapWidth phproogle::mapWidth
顧名思義,這將設定div的寬度該地圖居住於預設寬度為350像素。
phproogle::mapHeight phproogle::mapHeight
這將設定分區的高度該地圖居住於預設值是300像素。
phproogle::googleJS() phproogle::googleJS()
此方法返回的JavaScript字串,用於在檔案頭,顯示與GoogleAPI密鑰連接字串最頻繁。
phproogle::drawMap() phproogle::drawMap()
此方法返回的JavaScript製作完成的地圖本身。


http://www.bkjia.com/PHPjc/444981.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444981.htmlTechArticlephp google api 介面程式 php google api 介面程式 這裡是一個完全funtional類產生Google地圖。 允許多類定製,使使用者能夠快速實現在幾分鐘之內就沒...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.