PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/cms/googleMaps.class.php

https://github.com/swat/pragyan
PHP | 114 lines | 94 code | 11 blank | 9 comment | 8 complexity | 9443379c0ab182083da566d86f24b60b MD5 | raw file
  1. <?php
  2. if(!defined('__PRAGYAN_CMS'))
  3. {
  4. header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
  5. echo "<h1>403 Forbidden</h1><h4>You are not authorized to access the page.</h4>";
  6. echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
  7. exit(1);
  8. }
  9. /**
  10. * @package pragyan
  11. * @class googlemaps Class to render Google maps using its API and Geocoding technique
  12. * @author Abhishek Shrivastava
  13. * @copyright (c) 2010 Pragyan Team
  14. * @license http://www.gnu.org/licenses/ GNU Public License
  15. * For more details, see README
  16. */
  17. class googlemaps
  18. {
  19. var $latlong="-34.397, 150.644"; ///< Just a random number :)
  20. var $zoom="14";
  21. var $maptype="ROADMAP"; ///< Other options : SATELLITE, HYBRID, TERRAIN
  22. var $divid="map_canvas";
  23. var $divwidth="300px";
  24. var $divheight="300px";
  25. var $counter="0";
  26. var $includejs="<script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>";
  27. var $mainjs = "";
  28. function render($text)
  29. {
  30. global $sourceFolder;
  31. global $uploadFolder;
  32. global $urlRequestRoot, $cmsFolder;
  33. global $STARTSCRIPTS;
  34. preg_match_all("/\[googlemaps\](.*?)\[\/googlemaps\]/si", $text, $matches);
  35. if(count($matches[0])==0)
  36. return $text;
  37. $address = array();
  38. for ($i = 0; $i < count($matches[0]); $i++) {
  39. $position = strpos($text, $matches[0][$i]);
  40. $address[] = $matches[1][$i];
  41. $div=$this->get_div($i);
  42. $text = substr_replace($text, $div, $position, strlen($matches[0][$i]));
  43. }
  44. $mainjs=$this->generate_js($i,$address);
  45. $STARTSCRIPTS.="googlemaps_initialize();";
  46. return $this->includejs.$mainjs.$text;
  47. //return $text;
  48. }
  49. function get_div($id)
  50. {
  51. $div = " <div id=\"{$this->divid}{$id}\" style=\"width: {$this->divwidth}; height: {$this->divheight};\"></div>";
  52. return $div;
  53. }
  54. function generate_js($count,$address)
  55. {
  56. $varmaps= array();
  57. $varaddr= array();
  58. $varobj= array();
  59. for($i=0;$i<$count;$i++)
  60. {
  61. $varmaps[]="maps$i";
  62. $varaddr[]=<<<ADDR
  63. var address$i = "{$address[$i]}";
  64. geocoder.geocode( { 'address': address$i}, function(results, status) {
  65. if (status == google.maps.GeocoderStatus.OK) {
  66. map$i.setCenter(results[0].geometry.location);
  67. var marker = new google.maps.Marker({
  68. map: map$i,
  69. position: results[0].geometry.location
  70. });
  71. } else {
  72. alert("Geocode was not successful for the following reason: " + status);
  73. }
  74. });
  75. ADDR;
  76. $varobj[]="map$i = new google.maps.Map(document.getElementById(\"{$this->divid}{$i}\"), myOptions);";
  77. }
  78. $varmapsj=implode(",",$varmaps);
  79. $varaddrj=implode("\n",$varaddr);
  80. $varobjj=implode("\n",$varobj);
  81. $mainjs=<<<JS
  82. <script>
  83. var geocode;
  84. var $varmapsj;
  85. function codeAddress() {
  86. $varaddrj
  87. }
  88. function googlemaps_initialize() {
  89. geocoder = new google.maps.Geocoder();
  90. var latlng = new google.maps.LatLng({$this->latlong});
  91. var myOptions = {
  92. zoom: {$this->zoom},
  93. center: latlng,
  94. mapTypeId: google.maps.MapTypeId.{$this->maptype}
  95. }
  96. $varobjj
  97. codeAddress();
  98. }
  99. </script>
  100. JS;
  101. return $mainjs;
  102. }
  103. }