当前位置:首页 > PHP教程 > php高级应用 > 列表

php google api 接口程序

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-10 08:44:28 浏览: 评论:0 

 

常用的google地图开发参数

phproogle::apiKey

.这是谷歌的用户API密钥,这也可以设置使用时,一个新的实例是instanciated构造.

phproogle::addGoogleMapControl() phproogle::addGoogleMapControl()

此方法设置,如缩放控制控制器的谷歌地图,地图控制等此方法可以多次调用设置地图多个控件,Options are:选项有:

* GLargeMapControl GLargeMapControl

* GSmallMapControl GSmallMapControl

* GSmallZoomControl GSmallZoomControl

* GScaleControl GScaleControl

* GMapTypeControl GMapTypeControl

* GHierarchicalMapTypeControl GHierarchicalMapTypeControl

* GOverviewMapControl GOverviewMapControl

<?php $map -> addGoogleMapControl ( "GSmallMapControl" ); ?> 

phproogle::mapType phproogle::mapType

This sets the map type to one of four options:这将设置地图类型的四个选项之一:

* normal正常

* satellite卫星

* hybrid混合

* physical物理

<?php $map -> 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字符串,用于在文件头,显示与谷歌API密钥连接字符串最频繁。 

phproogle::drawMap() phproogle::drawMap()

此方法返回的JavaScript制作完成的地图本身.

  1. <?php 
  2.  
  3.  
  4. class phproogleMap { 
  5.  
  6. /* 
  7. * @The google api key 
  8. */ 
  9. private $apiKey ; 
  10.  
  11. /* 
  12. * @the map zoom level 
  13. */ 
  14. private $mapZoom = 8 ; 
  15.  
  16. /* 
  17. * @The width of the map div 
  18. */ 
  19. private $mapWidth = 350 ; 
  20.  
  21. /* 
  22. * @The height of the map div 
  23. */ 
  24. private $mapHeight = 300 ; 
  25.  
  26. /* 
  27. * @The map center 
  28. */ 
  29. private $mapCenter ; 
  30.  
  31. /* 
  32. * The array of map types 
  33. */ 
  34. private $googleMapTypes ; 
  35.  
  36. /* 
  37. * @The map type 
  38. */ 
  39. private $mapType = 'normal' ; 
  40.  
  41. /* 
  42. * @The array of marker points 
  43. */ 
  44. private $markerPoints = array(); 
  45.  
  46. /* 
  47. * @The array of marker addresses 
  48. */ 
  49. private $markerAddresses = array(); 
  50.  
  51. /* 
  52. * @The maps controls 
  53. */ 
  54. private $googleMapControls = array(); 
  55.  
  56. /* 
  57. * @The ID of the map div 
  58. */ 
  59. private $mapDivID ; 
  60.  
  61. /* 
  62. * The constructor 
  63. * 
  64. * @param apiKey 
  65. * 
  66. * @access public 
  67. * 
  68. * @return void 
  69. * 
  70. */ 
  71. public function __construct ( $apiKey = null ) 
  72. { 
  73. $this -> apiKey = is_null ( $apiKey ) ? '' : $apiKey ; 
  74. /*** set the map types ***/ 
  75. $this -> setGoogleMapTypes (); 
  76. } 
  77.  
  78.  
  79. /* 
  80. * 
  81. * @setter 
  82. * 
  83. * @access public 
  84. * 
  85. */ 
  86. public function __set ( $name , $value ) 
  87. { 
  88. switch ( $name ) 
  89. { 
  90. case 'apiKey' : 
  91. if(! is_string ( $value )) 
  92. { 
  93. throw new Exception ( $name , $value , 'string' ); 
  94. } 
  95. $this -> $name = $value ; 
  96. break; 
  97.  
  98. case 'mapZoom' : 
  99. if( filter_var ( $value , FILTER_VALIDATE_INT , array( "options" => array( "min_range" => 0 , "max_range" => 19 ))) == false ) 
  100. { 
  101. throw new Exception ( " $name is out of range" ); 
  102. } 
  103. $this -> $name = $value ; 
  104. break; 
  105.  
  106. case 'mapWidth' : 
  107. if( filter_var ( $value , FILTER_VALIDATE_INT , array( "options" => array( "min_range" => 100 , "max_range" => 900 ))) == false ) 
  108. { 
  109. throw new Exception ( " $name is out of range for" ); 
  110. } 
  111. $this -> $name = $value ; 
  112. break; 
  113.  
  114. case 'mapHeight' : 
  115. if( filter_var ( $value , FILTER_VALIDATE_INT , array( "options" => array( "min_range" => 100 , "max_range" => 900 ))) == false ) 
  116. { 
  117. throw new Exception ( " $name is out of range for" ); 
  118. } 
  119. $this -> $name = $value ; 
  120. break; 
  121.  
  122. case 'mapType' : 
  123. if(! array_key_exists ( $value , $this -> googleMapTypes ) ) 
  124. { 
  125. throw new Exception ( " $name is not a valid map type" ); 
  126. } 
  127. $this -> $name = $value ; 
  128. break; 
  129.  
  130. case 'mapDivID' : 
  131. if( ! is_string ( $value ) ) 
  132. { 
  133. throw new Exception ( " $name is not a valid ID" ); 
  134. } 
  135. $this -> $name = $value ; 
  136. break; 
  137.  
  138. case 'mapCenter' : 
  139. if( ! is_array ( $value ) ) 
  140. { 
  141. throw new Exception ( " $name is not a valid array" ); 
  142. } 
  143. $this -> $name = $value ; 
  144. break; 
  145.  
  146. default: 
  147. throw new Exception ( "Invalid Parameter $name " ); 
  148. } 
  149. } 
  150.  
  151.  
  152. /* 
  153. * 
  154. * @getter 
  155. * 
  156. * @access public 
  157. * 
  158. */ 
  159. public function __get ( $name ) 
  160. { 
  161. switch ( $name ) 
  162. { 
  163. case 'apiKey' : 
  164. return $this -> apiKey ; 
  165. break; 
  166.  
  167. case 'mapZoom' : 
  168. return $this -> mapZoom ; 
  169. break; 
  170.  
  171. case 'mapWidth' : 
  172. return $this -> mapWidth ; 
  173. break; 
  174.  
  175. case 'mapHeight' : 
  176. return $this -> mapHeight ; 
  177. break; 
  178.  
  179. case 'mapType' : 
  180. return $this -> mapType ; 
  181. break; 
  182.  
  183. case 'mapDivID' : 
  184. return $this -> mapDivID ; 
  185. break; 
  186.  
  187. case 'mapCenter' ; 
  188. return $this -> mapCenter ; 
  189. break; 
  190. } 
  191. /*** if we are here, throw an excepton ***/ 
  192. throw new Exception ( " $name is invalid" ); 
  193. } 
  194.  
  195.  
  196. /* 
  197. * 
  198. * @isset 
  199. * 
  200. * @access public 
  201. * 
  202. */ 
  203. public function __isset ( $name ) 
  204. { 
  205. switch ( $name ) 
  206. { 
  207. case 'apiKey' : 
  208. $this -> apiKey = $name ; 
  209. break; 
  210.  
  211. case 'mapZoom' : 
  212. $this -> mapZoom = $name ; 
  213. break; 
  214.  
  215. case 'mapWidth' : 
  216. $this -> mapWidth = $name ; 
  217. break; 
  218.  
  219. case 'mapHeight' : 
  220. $this -> mapHeight = $name ; 
  221. break; 
  222.  
  223. case 'mapType' : 
  224. $this -> mapType = $name ; 
  225. break; 
  226.  
  227. case 'mapDivID' : 
  228. $this -> mapDivID = $name ; 
  229. break; 
  230.  
  231. case 'mapCenter' ; 
  232. $this -> mapCenter = $name ; 
  233. break; 
  234.  
  235. default: 
  236. return false ; 
  237. } 
  238. } 
  239.  
  240.  
  241. /* 
  242. * 
  243. * @Set the map types 
  244. * 
  245. * @access private 
  246. * 
  247. * @return void 
  248. * 
  249. */ 
  250. private function setGoogleMapTypes () 
  251. { 
  252. $this -> googleMapTypes = array( 'physical' => 'G_PHYSICAL_MAP' , 'normal' => 'G_NORMAL_MAP' , 'satellite' => 'G_SATELLITE_MAP' , 'hybrid' => 'G_HYBRID_MAP' ); 
  253. } 
  254.  
  255. /* 
  256. * 
  257. * @add to the array of google maps controls 
  258. * 
  259. * @access public 
  260. * 
  261. * @return void 
  262. * 
  263. */ 
  264. public function addGoogleMapControl ( $control ) 
  265. { 
  266. $n = sizeof ( $this -> googleMapControls ); 
  267. $this -> googleMapControls [] = $control ; 
  268. } 
  269. /* 
  270. * 
  271. * @get pinpoint marker by address 
  272. * 
  273. * @access public 
  274. * 
  275. * @param string $address 
  276. * 
  277. * @param string $html 
  278. * 
  279. * @return void 
  280. * 
  281. */ 
  282. public function addMarkerAddress ( $address , $html ) 
  283. { 
  284. $s = sizeof ( $this -> markerAddresses ); 
  285. $this -> markerAddresses [ $s ][ 'address' ] = $address ; 
  286. $this -> markerAddresses [ $s ][ 'html' ]    = $html ; 
  287. } 
  288.  
  289.  
  290.  
  291. /* 
  292. * 
  293. * @get pinpoint mark by latitude or longitude 
  294. * 
  295. * @access public 
  296. * 
  297. * @param string $lat 
  298. * 
  299. * @param string $long 
  300. * 
  301. * @param string $html 
  302. * 
  303. * @return void 
  304. * 
  305. */ 
  306. public function addMarker ( $lat , $long , $html ) 
  307. { 
  308. $pointer = sizeof ( $this -> markerPoints ); 
  309. $this -> markerPoints [ $pointer ][ 'lat' ]  = $lat ; 
  310. $this -> markerPoints [ $pointer ][ 'long' ] = $long ; 
  311. $this -> markerPoints [ $pointer ][ 'html' ] = $html ; 
  312. } 
  313.  
  314.  
  315. /* 
  316. * 
  317. * @The javascript for google to connect 
  318. * 
  319. * @access public 
  320. * 
  321. * @return string 
  322. * 
  323. */ 
  324. public function googleJS () 
  325. { 
  326. return '<script src="http://maps.google.com/maps?file=api&v=2&key=' . $this -> apiKey . '" type="text/javascript"></script>' . " " ; 
  327. } 
  328.  
  329. private function noJavascript () 
  330. { 
  331. return '<noscript><b>JavaScript must be enabled in order for you to use Google Maps.</b> 
  332. However, it seems JavaScript is either disabled or not supported by your browser. 
  333. To view Google Maps, enable JavaScript by changing your browser options, and then 
  334. try again. 
  335. </noscript>' ; 
  336. } 
  337.  
  338.  
  339. public function drawMap () 
  340. { 
  341. $js = '<div id="' . $this -> mapDivID . '" style="width: ' . $this -> mapWidth . 'px; height: ' . $this -> mapHeight . 'px"></div>' ; 
  342. $js .= $this -> noJavascript (); 
  343.  
  344. $js .= ' 
  345. <script type="text/javascript"> 
  346. //<![CDATA[ 
  347.  
  348. if (GBrowserIsCompatible()) { 
  349.  
  350. geocoder = new GClientGeocoder(); 
  351.  
  352. function createMarker(point,html) { 
  353. var marker = new GMarker(point); 
  354. GEvent.addListener(marker, "click", function() { 
  355. marker.openInfoWindowHtml(html); 
  356. }); 
  357. return marker; 
  358. } 
  359.  
  360. // Display the map, with some controls and set the initial location 
  361. var map = new GMap2(document.getElementById("' . $this -> mapDivID . '"));' . " " ; 
  362.  
  363. /*** set the map controls here ***/ 
  364. if( sizeof ( $this -> googleMapControls ) > 0 ) 
  365. { 
  366. foreach( $this -> googleMapControls as $control ) 
  367. { 
  368. $js .= 'map.addControl(new ' . $control . '());' . " " ; 
  369. } 
  370. } 
  371. /*** set the map center, zooom, and type ***/ 
  372. list( $lat , $long ) = $this -> mapCenter ; 
  373. $js .= 'map.setCenter(new GLatLng(' . $lat . ',' . $long . '), ' . $this -> mapZoom . ', ' . $this -> googleMapTypes [ $this -> mapType ]. ');' . " " ; 
  374.  
  375.  
  376. if( sizeof ( $this -> markerAddresses ) > 0 ) 
  377. { 
  378. foreach( $this -> markerAddresses as $add ) 
  379. { 
  380. $base_url = "http://maps.google.com/maps/geo?output=xml" . "&key=" . $this -> apiKey ; 
  381. $request_url = file_get_contents ( $base_url . "&q=" . urlencode ( $add [ 'address' ])); 
  382. $xml = simplexml_load_string ( $request_url ); 
  383. $status = $xml -> Response -> Status -> code ; 
  384. $point = $xml -> Response -> Placemark -> Point -> coordinates ; 
  385. list( $long , $lat , $d ) = explode ( ',' , $point ); 
  386. $js .= 'var point = new GLatLng(' . $lat . ',' . $long . ');' . " " ; 
  387. $js .= "var marker = createMarker(point,'" . $add [ 'html' ]. "')" . " " ; 
  388. $js .= 'map.addOverlay(marker);' . " " ; 
  389. } 
  390. } 
  391.  
  392.  
  393. /*** set the markers here ***/ 
  394. foreach( $this -> markerPoints as $data ) 
  395. { 
  396. $js .= 'var point = new GLatLng(' . $data [ 'lat' ]. ',' . $data [ 'long' ]. ');' . " " ; 
  397. $js .= "var marker = createMarker(point,'" . $data [ 'html' ]. "')" . " " ; 
  398. $js .= 'map.addOverlay(marker);' . " " ; 
  399. } 
  400. $js .= ' 
  401. GMap.prototype.centerAndZoomOnBounds = function(bounds) { 
  402. // make 10% bigger so all markers show completely 
  403. var span = new GSize((bounds.maxX - bounds.minX) * 1.1, (bounds.maxY - bounds.minY)*1.1); 
  404. var center = new GPoint(bounds.minX + span.width / 2., bounds.minY + span.height / 2.); 
  405.  
  406. var newZoom = this.spec.getLowestZoomLevel(center, span, this.viewSize); 
  407. if (this.getZoomLevel() != newZoom) { 
  408. this.centerAndZoom(center, newZoom); 
  409. } else { 
  410. this.recenterOrPanToLatLng(center); 
  411. } 
  412. } 
  413.  
  414. // display a warning if the browser was not compatible 
  415. } else { 
  416. alert("Sorry, the Google Maps API is not compatible with this browser"); 
  417. } 
  418.  
  419. //]]> 
  420. </script>' ; 
  421.  
  422. return $js ; 
  423.  
  424. } 
  425.  
  426. } /*** end of class ***/ 
  427.  
  428.  
  429. try 
  430. { 
  431. /*** a new phproogle instance ***/ 
  432. $map = new phproogleMap (); 
  433.  
  434. /*** the google api key ***/ 
  435. $map -> apiKey = 'YOUR_GOOGLE_API_KEY' ; 
  436.  
  437. /*** zoom is 0 - 19 ***/ 
  438. $map -> mapZoom = 14 ; 
  439.  
  440. /*** the map width ***/ 
  441. $map -> mapWidth = 350 ; 
  442.  
  443. /*** the map height ***/ 
  444. $map -> mapHeight = 300 ; 
  445.  
  446. /*** set the map type ***/ 
  447. $map -> mapType = 'normal' ; 
  448.  
  449. /*** set the map center ***/ 
  450. $map -> mapCenter = array(- 33.862828 , 151.216974 ); 
  451.  
  452. /*** add some markers with latitude and longitude  ***/ 
  453. $map -> addMarker (- 33.858362 , 151.214876 , '<h2>Sydney Opera House</h2><p>For those with culture</p>' ); 
  454. $map -> addMarker (- 33.862828 , 151.216974 , '<h3>Royal Botanic Gardens</h2><a href="http://phpro.org">A link here</a>' ); 
  455.  
  456.  
  457. /*** add some controls ***/ 
  458. $map -> addGoogleMapControl ( 'GMapTypeControl' ); 
  459. $map -> addGoogleMapControl ( 'GSmallMapControl' ); 
  460. $map -> addGoogleMapControl ( 'GOverviewMapControl' ); 
  461.  
  462. /*** add some marker addresses ***/ 
  463. $map -> addMarkerAddress ( '2 Pitt St Sydney NSW Australia' , '<h2>Head Office</h2>' ); 
  464. $map -> addMarkerAddress ( '122 Pitt St Sydney NSW Australia' , '<h2>The Factory</h2>' ); 
  465.  
  466. /*** set the map div id ***/ 
  467. $map -> mapDivID = 'map' ; 
  468. } 
  469. catch( Exception $e ) 
  470. { 
  471. echo $e -> getMessage (); 
  472. //开源代码phpfensi.com 
  473. } 
  474. ?> 
  475.  
  476. <html> 
  477. <head> 
  478. <?php echo $map -> googleJS (); ?> 
  479. </head> 
  480. <body> 
  481. <?php echo $map -> drawMap (); ?> 
  482. </body> 
  483. </html> 

Tags: php google api 接口程序

分享到: