Jump to:

1136 Posts in 1984 Topics by 403 members

Upgrading SilverStripe

SilverStripe Forums » Upgrading SilverStripe » GoogleMaps

Ask questions about upgrading SilverStripe to the latest version.

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

Page: 1
Go to End
Author Topic: 1958 Views
  • mhull
    Avatar
    Community Member
    79 Posts

    GoogleMaps Link to this post

    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

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: GoogleMaps Link to this post

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

  • mhull
    Avatar
    Community Member
    79 Posts

    Re: GoogleMaps Link to this post

    Many Thanks for your reply.
    All fixed

  • Brady.Dyer
    Avatar
    Community Member
    21 Posts

    Re: GoogleMaps Link to this post

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

    1958 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.