Simple Google Maps Module for Silverstripe

I've created a very simple map page type for Silverstripe.

 It uses geocoding, so all you need to do is fill in a description of the address. For example, this map page has "london" as the address.

 Put the following code in a file called Map.php under mysite/code:

<?php

class Map extends Page {
    static $db = array(
    'Address'=>'Text'
    );
       
    static $has_one = array(
   );
  
   function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Content.Main', new TextField('Address','Address: eg; newport street, hay on wye, england'));
        return $fields;
    }
}

class Map_Controller extends Page_Controller {

}

?>

 

And the following code in a file called Map.ss under mysite/templates:

You'll need to get a google maps api key here and add it to the code below

   <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAlPAmzgl2GFTygXALPrHvURRUhrclDzGyVK4iB593UG7apj2Z1xTJnWpCeSB7NMdrmny1CNhYa7OyTg"
      type="text/javascript"></script>
    <style type="text/css">
      body {
        font-family: Arial, sans serif;
        font-size: 11px;
      }
    </style>
    <script type="text/javascript">
    //<![CDATA[

    var map;
    var geocoder = null;
    var addressMarker;

    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(54, -4.1), 5);
        map.setMapType(G_HYBRID_MAP);
        geocoder = new GClientGeocoder();


               showAddress("$Address");

      }
    }

    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              if (addressMarker) {
                map.removeOverlay(addressMarker);
              }
              addressMarker = new GMarker(point);
              map.setCenter(point, 15);
              map.addOverlay(addressMarker);
            }
          }
        );
      }
    }
    //]]>

    </script>




<div class="typography">
    <div class="split" style="margin-top:5px;">
        <% include SideBar %>
    </div>
    <div class="split" style="width:543px; margin-left:10px; margin-top:5px;" >
        <% include HozLine %>
        <h1>$Title</h1>
        <% include HozLine %>
        <div style="height:3px;"></div>
        $Content





    <div id="map" style="width: 450px; height: 300px"></div>




        $Form
    $PageComments
    </div>
</div>

<script>
load();
</script>

 

Generated with the default ContentController.ss template