Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Upgrading SilverStripe /

Ask questions about upgrading SilverStripe to the latest version.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

GoogleMaps


Go to End
Reply


4 Posts   2250 Views

Avatar
mhull

Community Member, 79 Posts

17 December 2008 at 11:06pm

So i had the folowing code working until I upgraded to 2.3. Can anyone help me getting it working again?

class GMapPage extends Page {
	static $db = array (
		'GoogleMapsScriptURL' => 'Text',
		'GoogleMapsAPIKey' => 'Text',
		'MapControl' => 'Boolean',
		'MapControlSize' => 'VarChar',
		'MapTypeControl' => 'Boolean',
		'MapSearchControl' => 'Boolean',
		'MapMarker' => 'Boolean',
		'MapMarkerInfo' => 'Boolean',
		'Latitude' => 'Text',
		'Longitude' => 'Text',
		'Zoom' => 'Int',
		'AddressLine1' => 'Text',
		'AddressLine2' => 'Text',
		'AddressLine3' => 'Text',
		'PostCode' => 'Text',
	);
	
	static $allowed_children = "none";
	
	function getCMSFields() {
    	$fields = parent::getCMSFields();
		
	    $fields->addFieldToTab('Root.Content.MapConfig', new TextField('GoogleMapsScriptURL'),'Content');
        $fields->addFieldToTab('Root.Content.MapConfig', new TextField('GoogleMapsAPIKey'),'Content');
		$fields->addFieldToTab(
			'Root.Content.MapOptions',
			new FieldGroup(
				new CheckboxField("MapControl", "Add a Map Control"),
				new OptionsetField(
					"MapControlSize",
					" - Choose type",
					array(
						"Small" => "Small",
						"Large" => "Large",
			    	)
				)
			),
			'Content'
		);
		$fields->addFieldToTab(
			'Root.Content.MapOptions',
			new FieldGroup(
				new CheckboxField("MapTypeControl", "Add a Map Type Control"),
				new CheckboxField("MapSearchControl", "Add a Map Search Control"),
				new CheckboxField("MapMarker", "Add a Map Marker"),
				new CheckboxField("MapMarkerInfo", "Add an Info Window to the Marker")
			),
			'Content'
		);
		$fields->addFieldToTab('Root.Content.MapOptions', new FieldGroup(new NumericField("Zoom", "Enter map zoom value, 1-15", "13")),'Content');			
		$fields->addFieldToTab(
			'Root.Content.MapOptions',
			new FieldGroup(
				new HeaderField('Co-ordinates',2),
				new LiteralField('Blank','<p></h3>Leave blank to use Address</h3></p>'),
				new TextField('Latitude','Latitude'),
				new TextField('Longitude','Longitude')
			),
			'Content'
		);
		$fields->addFieldToTab("Root.Content.Address", new TextField('AddressLine1'),'Content');
      	$fields->addFieldToTab("Root.Content.Address", new TextField('AddressLine2'),'Content');
      	$fields->addFieldToTab("Root.Content.Address", new TextField('AddressLine3'),'Content');
      	$fields->addFieldToTab("Root.Content.Address", new TextField('PostCode'),'Content');
		
        return $fields;
   }
   
}

class GMapPage_Controller extends Page_Controller {
	function GetGoogleMapsScript() {
		$gmap = DataObject::get_one('GMapPage');
		
        Requirements::javascript($gmap->GoogleMapsScriptURL.$gmap->GoogleMapsAPIKey);
		Requirements::javascript("http://www.google.com/uds/api?file=uds.js&amp;v=1.0");
		Requirements::javascript("http://www.google.com/uds/solutions/localsearch/gmlocalsearch.js");
		Requirements::insertHeadTags(
			"<style type='text/css'>
			@import url(http://www.google.com/uds/css/gsearch.css);
			@import url(http://www.google.com/uds/solutions/localsearch/gmlocalsearch.css);
			</style>"
		); 
		
		$this->address = $gmap->AddressLine1 . ", " . $gmap->AddressLine2;
		$gmap->infoWindow = "'" . $this->address . "'";
		(is_Null($gmap->Latitude) OR is_Null($gmap->Longitude)) ? ($gmap->Latitude = $gmap->Longitude = "null") : ($gmap->Latitude);
		$gmap->MapControlSize = "G" . $gmap->MapControlSize . "MapControl()";
		
		$code .= <<<ScriptCode
		<script type="text/javascript">
		//<![CDATA[
		var map = null;
	    var geocoder = null;
		var checkLatLng = $gmap->Latitude;
		var MapControl = $gmap->MapControl;
		var MapTypeControl = $gmap->MapTypeControl;
		var MapSearchControl = $gmap->MapSearchControl;
		var MapMarker = $gmap->MapMarker;
		var MapMarkerInfo = $gmap->MapMarkerInfo;
		
		function load() {
			if (GBrowserIsCompatible()) {
				map = new GMap2(document.getElementById("Map"));
				if (null==checkLatLng) {
					geocoder = new GClientGeocoder();
					showAddress("$this->address");
				}
				else {
					var latlng = new GLatLng($gmap->Latitude, $gmap->Longitude);
					map.setCenter(latlng, $gmap->Zoom);
				}
				if (MapControl == "1")  {map.addControl(new $gmap->MapControlSize);}
				if (MapTypeControl == "1") {map.addControl(new GMapTypeControl());}
				if (MapSearchControl == "1") {
			        // bind a search control to the map, suppress result list
					map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));
				}
			  
			  
			  }
			  else {
				alert("Your browser is not compatible with the mapping tool.");
			  }
		}
		GSearch.setOnLoadCallback(load);

		function showAddress(address) {
			if (geocoder) {
				geocoder.getLatLng(
					address,
					function(point) {
					if (!point) {
						alert(address + " not found");
					} else {
						map.setCenter(point, $gmap->Zoom);
						if (MapMarker == "1") {
							var marker = new GMarker(point);
							map.addOverlay(marker);
						}
						if (MapMarkerInfo == "1") {
							marker.openInfoWindowHtml($gmap->infoWindow);
							GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml($gmap->infoWindow);});
						}
					}
				  }
				);
			}
		}
		//]]>
		</script>
ScriptCode;

        return $code;
   	}   
}
?>

The error is showing Undefined variable: code and highlighting line 155: ScriptCode;

Any help would be much appreciated

Avatar
(deleted)

Community Member, 473 Posts

18 December 2008 at 8:00am

The error is because you are using the variable before you define it. To fix, simply have $code = ''; before you first add to it.

Avatar
mhull

Community Member, 79 Posts

19 December 2008 at 12:22am

Many Thanks for your reply.
All fixed

Avatar
Brady.Dyer

Community Member, 21 Posts

6 January 2009 at 11:54pm

Can you post the corrected code above? I would like to use this page type on my site too.