pragyan /cms/googleMaps.class.php

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

		</script>
JS;
		return $mainjs;
	
	}
	
}
Back to Top