Changeset 2158

Show
Ignore:
Timestamp:
02/23/08 13:41:41 (6 months ago)
Author:
Shadowhand
Message:

Hooray! Gmaps module in a working (but early) form!

Location:
trunk/modules/gmaps
Files:
4 added
2 removed
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/gmaps/controllers/google_map.php

    r2154 r2158  
    11<?php defined('SYSPATH') or die('No direct script access.'); 
    2  
     2/** 
     3 * Gmaps module demo controller. 
     4 * 
     5 * $Id$ 
     6 * 
     7 * @package    Gmaps 
     8 * @author     Woody Gilk 
     9 * @copyright  (c) 2007-2008 Kohana Team 
     10 * @license    http://kohanaphp.com/license.html 
     11 */ 
    312class Google_Map_Controller extends Controller { 
    413 
    514    public function index() 
    615    { 
    7         View::factory('gmaps/api_demo')->render(TRUE); 
     16        $this->db = Database::instance('mysql://root:r00tdb@localhost/azmap'); 
     17 
     18        // Create a new Gmap 
     19        $map = new Gmap('map'); 
     20 
     21        // Set the map center point 
     22        $map->center(0, 0, 1)->controls('large'); 
     23 
     24        // Add a new marker 
     25        $map->add_marker(44.9801, -93.2519, '<strong>Minneapolis, MN</strong><p>Hello world!</p>'); 
     26 
     27        View::factory('gmaps/api_demo')->set('map', $map->render())->render(TRUE); 
    828    } 
    929 
    10     public function markers() 
     30    public function azmap() 
    1131    { 
    12         $map = new Gmap; 
    13          
    14         $markers[] = Gmap::marker('South Africa'); 
    15         $markers[] = Gmap::marker('8601 Edinbrook Crossing, 55443'); 
     32        $this->db = Database::instance('mysql://root:r00tdb@localhost/azmap'); 
    1633 
    17         foreach ($markers as $marker) 
     34        // Create a new Gmap 
     35        $map = new Gmap('map', array 
     36        ( 
     37            'ScrollWheelZoom' => TRUE, 
     38        )); 
     39 
     40        // Set the map center point 
     41        $map->center(0, 35, 2)->controls('large'); 
     42 
     43        // Set marker locations 
     44        foreach (ORM::factory('location')->find_all() as $location) 
    1845        { 
    19             echo html::anchor 
    20             ( 
    21                 'http://maps.google.com/?&q=' 
    22                 .rawurlencode($marker->address).'&ll=' 
    23                 .rawurlencode($marker->lon.','.$marker->lat) 
    24                 .'&z=10&iwloc=addr' 
    25             ).'<br/>'; 
     46            // Add a new marker 
     47            $map->add_marker($location->lat, $location->lon, 
     48                // Get the info window HTML 
     49                View::factory('gmaps/info_window')->bind('location', $location)->render()); 
    2650        } 
    2751 
     52        header('Content-type: text/javascript'); 
     53        echo $map->render(); 
    2854    } 
    2955 
    30     public function georss() 
     56    public function jquery() 
    3157    { 
    32         View::factory('gmaps/rss_demo')->render(TRUE); 
     58        View::factory('gmaps/jquery')->render(TRUE); 
    3359    } 
    3460 
    35     public function feed() 
     61    public function xml() 
    3662    { 
    37         header('Content-Type: text/xml; charset=utf-8'); 
    38         require Kohana::find_file('views', 'gmaps/rss', FALSE, 'xml'); 
     63        $this->db = Database::instance('mysql://root:r00tdb@localhost/azmap'); 
     64 
     65        // Get all locations 
     66        $locations = ORM::factory('location')->find_all(); 
     67 
     68        // Create the XML container 
     69        $xml = new SimpleXMLElement('<gmap></gmap>'); 
     70 
     71        foreach ($locations as $location) 
     72        { 
     73            // Create a new mark 
     74            $node = $xml->addChild('marker'); 
     75 
     76            // Set the latitutde and longitude 
     77            $node->addAttribute('lon', $location->lon); 
     78            $node->addAttribute('lat', $location->lat); 
     79 
     80            $node->html = View::factory('gmaps/xml')->bind('location', $location)->render(); 
     81 
     82            foreach ($location->as_array() as $key => $val) 
     83            { 
     84                // Skip the ID 
     85                if ($key === 'id') continue; 
     86 
     87                // Add the data to the XML 
     88                $node->$key = $val; 
     89            } 
     90        } 
     91 
     92        header('Content-Type: text/xml'); 
     93        echo $xml->asXML(); 
    3994    } 
    4095 
  • trunk/modules/gmaps/libraries/Gmap.php

    r2084 r2158  
    33 * Google Maps API integration. 
    44 * 
    5  *  License: 
    6  *  author    - Woody Gilk 
    7  *  copyright - (c) 2007 Kohana Team 
    8  *  license   - <http://kohanaphp.com/license.html> 
     5 * $Id$ 
     6 * 
     7 * @package    Gmaps 
     8 * @author     Kohana Team 
     9 * @copyright  (c) 2007-2008 Kohana Team 
     10 * @license    http://kohanaphp.com/license.html 
    911 */ 
    1012class Gmap_Core { 
    1113 
    1214    /** 
    13      * Creates a new marker to place on a map. 
    14      * 
    15      * @param   string|float  address|longitude 
    16      * @param   float         latitude 
    17      * @return  object        Gmap_Marker 
    18      */ 
    19     public static function marker($lon, $lat = NULL) 
    20     { 
    21         if ($lat === NULL) 
    22         { 
    23             // Get the latitude and longitude by address 
    24             list ($lon, $lat) = Gmap::address_to_ll($address = $lon); 
    25         } 
    26  
    27         // Create a new marker 
    28         $marker = new Gmap_Marker((float) $lat, (float) $lon); 
    29  
    30         // Set the address of the marker 
    31         isset($address) and $marker->address = $address; 
    32  
    33         return $marker; 
    34     } 
    35  
    36  
    37     /** 
    3815     * Retrieves the latitude and longitude of an address. 
    3916     * 
     
    4320    public static function address_to_ll($address) 
    4421    { 
     22        $lat = NULL; 
    4523        $lon = NULL; 
    46         $lat = NULL; 
    4724 
    4825        if ($xml = Gmap::address_to_xml($address)) 
    4926        { 
    5027            // Get the latitude and longitude from the Google Maps XML 
     28            // NOTE: the order (lon, lat) is the correct order 
    5129            list ($lon, $lat) = explode(',', $xml->Response->Placemark->Point->coordinates); 
    5230        } 
    5331 
    54         return array($lon, $lat); 
     32        return array($lat, $lon); 
    5533    } 
    5634 
     
    11593    } 
    11694 
    117     public function __construct($config = NULL) 
    118     { 
    119         empty($config) and $config = Config::item('gmaps'); 
    120     } 
    121  
    122     public function add_marker($value='') 
    123     { 
    124         # code... 
     95    // Map settings 
     96    protected $id; 
     97    protected $options; 
     98    protected $center; 
     99    protected $control; 
     100 
     101    // Map markers 
     102    protected $markers; 
     103 
     104    /** 
     105     * Set the GMap center point. 
     106     * 
     107     * @param   string  HTML map id attribute 
     108     * @param   array   array of GMap constructor options 
     109     * @return  void 
     110     */ 
     111    public function __construct($id = 'map', $options = NULL) 
     112    { 
     113        // Set map ID and options 
     114        $this->id = $id; 
     115        $this->options = new Gmap_Options((array) $options); 
     116    } 
     117 
     118    /** 
     119     * Set the GMap center point. 
     120     * 
     121     * @chainable 
     122     * @param   float    latitude 
     123     * @param   float    longitude 
     124     * @param   integer  zoom level (1-16) 
     125     * @return  object 
     126     */ 
     127    public function center($lat, $lon, $zoom = 6) 
     128    { 
     129        // Set center location and zoom 
     130        $this->center = array($lat, $lon, $zoom); 
     131 
     132        return $this; 
     133    } 
     134 
     135    /** 
     136     * Set the GMap controls size. 
     137     * 
     138     * @chainable 
     139     * @param   string   small or large 
     140     * @return  object 
     141     */ 
     142    public function controls($size = NULL) 
     143    { 
     144        // Set the control type 
     145        $this->controls = (strtolower($size) === 'small') ? 'Small' : 'Large'; 
     146 
     147        return $this; 
     148    } 
     149 
     150    /** 
     151     * Set the GMap marker point. 
     152     * 
     153     * @chainable 
     154     * @param   float   latitude 
     155     * @param   float   longitude 
     156     * @param   string  HTML for info window 
     157     * @return  object 
     158     */ 
     159    public function add_marker($lat, $lon, $html = '') 
     160    { 
     161        // Add a new marker 
     162        $this->markers[] = new Gmap_Marker($lat, $lon, $html); 
     163 
     164        return $this; 
     165    } 
     166 
     167    /** 
     168     * Render the map into GMap Javascript. 
     169     * 
     170     * @return  string 
     171     */ 
     172    public function render() 
     173    { 
     174        // Latitude, longitude, and zoom 
     175        list ($lat, $lon, $zoom) = $this->center; 
     176 
     177        // Map 
     178        $map = 'map = new GMap2(document.getElementById("'.$this->id.'"));'; 
     179 
     180        // Map controls 
     181        $controls = empty($this->controls) ? '' : 'map.addControl(new G'.$this->controls.'MapControl());'; 
     182 
     183        // Map centering 
     184        $center = 'map.setCenter(new GLatLng('.$lat.', '.$lon.'));'; 
     185 
     186        // Map zoom 
     187        $zoom = 'map.setZoom('.$zoom.');'; 
     188 
     189        // Render the Javascript 
     190        return View::factory('gmaps/javascript', array 
     191            ( 
     192                'map' => $map, 
     193                'options' => $this->options, 
     194                'controls' => $controls, 
     195                'center' => $center, 
     196                'zoom' => $zoom, 
     197                'markers' => $this->markers, 
     198            )) 
     199            ->render(); 
    125200    } 
    126201 
  • trunk/modules/gmaps/libraries/Gmap_Marker.php

    r2084 r2158  
    66    public $html; 
    77 
    8     // Address 
    9     public $address; 
    10  
    118    // Latitude and longitude 
    129    public $latitude; 
    1310    public $longitude; 
    1411 
    15     public function __construct($lon, $lat) 
     12    /** 
     13     * Create a new GMap marker. 
     14     * 
     15     * @param   float   latitude 
     16     * @param   float   longitude 
     17     * @param   string  HTML of info window 
     18     * @return  void 
     19     */ 
     20    public function __construct($lat, $lon, $html) 
    1621    { 
    1722        if ( ! is_numeric($lat) OR ! is_numeric($lon)) 
    1823            throw new Kohana_Exception('gmaps.invalid_marker', $lat, $lon); 
    1924 
    20         //http://maps.google.com/?q=South+Africa&ie=UTF8&ll=-32.175612,21.42334&spn=5.503756,11.260986&t=h&z=7 
     25        // Set the latitude and longitude 
     26        $this->latitude = $lat; 
     27        $this->longitude = $lon; 
    2128 
    22         $this->longitude = ($lon < 0) ? min(0, max($lon, -90))  : min(90, max($lon, 0)); 
    23         $this->latitude  = ($lat < 0) ? min(0, max($lat, -180)) : min(180, max($lat, 0)); 
     29        // Set the info window HTML 
     30        $this->html = $html; 
    2431    } 
    2532 
    26     public function __get($key) 
     33    public function render($tabs = 0) 
    2734    { 
    28         if ($key === 'lat' OR $key === 'latitude') 
     35        // Create the tabs 
     36        $tabs = empty($tabs) ? '' : str_repeat("\t", $tabs); 
     37 
     38        $output = array(); 
     39        $output[] = 'var m = new GMarker(new GLatLng('.$this->latitude.', '.$this->longitude.'));'; 
     40        if ($html = $this->html) 
    2941        { 
    30             return $this->latitude; 
     42            $output[] = 'GEvent.addListener(m, "click", function()'; 
     43            $output[] = '{'; 
     44            $output[] = "\t".'m.openInfoWindowHtml('; 
     45            $output[] = "\t\t'".implode("'+\n\t\t$tabs'", explode("\n", $html))."'"; 
     46            $output[] = "\t);"; 
     47            $output[] = '});'; 
    3148        } 
    32         elseif ($key === 'lon' OR $key === 'longitude') 
    33         { 
    34             return $this->longitude; 
    35         } 
     49        $output[] = 'map.addOverlay(m);'; 
    3650 
    37         return NULL; 
    38     } 
    39  
    40     public function __set($key, $value) 
    41     { 
    42          
     51        return implode("\n".$tabs, $output); 
    4352    } 
    4453 
  • trunk/modules/gmaps/views/gmaps/api_demo.php

    r2084 r2158  
    1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    32<html xmlns="http://www.w3.org/1999/xhtml"> 
    43<head> 
    54<meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
    65<title>Google Maps JavaScript API Example</title> 
    7 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo Config::item('gmaps.api_key') ?>"type="text/javascript"></script> 
    8 <script type="text/javascript"> 
    9 function showmap(){ 
    10 //<![CDATA[ 
    11  
    12     if (GBrowserIsCompatible()) { 
    13         var map = new GMap(document.getElementById("map")); 
    14         map.centerAndZoom(new GPoint(0,35), 16); 
    15         map.enableScrollWheelZoom(); 
    16  
    17         // Prevents the page from scrolling 
    18         GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent); 
    19         // map.getContainer().onmousewheel = wheelevent; 
    20     } 
    21  
    22     var icon = new GIcon(); 
    23     icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png"; 
    24     icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png"; 
    25     icon.iconSize = new GSize(12, 20); 
    26     icon.shadowSize = new GSize(22, 20); 
    27     icon.iconAnchor = new GPoint(6, 20); 
    28     icon.infoWindowAnchor = new GPoint(5, 1); 
    29  
    30     map.addControl(new GSmallMapControl()); 
    31     map.addControl(new GMapTypeControl()); 
    32  
    33     var marker0 = new GMarker(new GPoint(-93.328379, 45.109871)); 
    34     map.addOverlay(marker0); 
    35     GEvent.addListener(marker0, "click", function() { 
    36         marker0.openInfoWindowHtml("Minneapolis, Minnesota"); 
    37     }); 
    38  
    39     var marker1 = new GMarker(new GPoint(139.682282, 35.678451)); 
    40     map.addOverlay(marker1); 
    41     GEvent.addListener(marker1, "click", function() { 
    42         marker1.openInfoWindowHtml("Tokyo, Japan"); 
    43     }); 
    44  
    45 //]]> 
    46 } 
    47 function wheelevent(e) { 
    48     e.preventDefault(); 
    49     e.returnValue = false; 
    50 } 
    51 window.onload = showmap; 
    52 </script> 
     6<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo Config::item('gmaps.api_key') ?>" type="text/javascript"></script> 
    537</head> 
    548<body> 
    559<p>You can use your scroll wheel to zoom in and out of the map.</p> 
    5610<div id="map" style="width: 600px; height: 400px"></div> 
     11<script type="text/javascript"> 
     12<?php echo $map ?> 
     13</script> 
    5714</body> 
    5815</html> 
  • trunk/modules/gmaps/views/gmaps/javascript.php

    r2084 r2158  
    1 <script type="text/javascript"> 
     1if (GBrowserIsCompatible()) 
     2{ 
     3    // Initialize the GMap 
     4    <?php echo $map."\n" ?> 
     5    <?php echo $controls."\n" ?> 
     6    <?php echo $center."\n" ?> 
     7    <?php echo $zoom."\n" ?> 
     8    <?php echo $options->render(1)."\n" ?> 
    29 
    3 function showmap(){ 
    4     //<![CDATA[ 
    5  
    6     if (GBrowserIsCompatible()) { 
    7  
    8         var map = new GMap(document.getElementById("map")); 
    9         map.centerAndZoom(new GPoint(-77.035971,38.898590), 4); 
    10     } 
    11  
    12         var icon = new GIcon(); 
    13         icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png"; 
    14         icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png"; 
    15         icon.iconSize = new GSize(12, 20); 
    16         icon.shadowSize = new GSize(22, 20); 
    17         icon.iconAnchor = new GPoint(6, 20); 
    18         icon.infoWindowAnchor = new GPoint(5, 1); 
    19  
    20         map.addControl(new GSmallMapControl()); 
    21         map.addControl(new GMapTypeControl()); 
    22         var point0 = new GPoint(-77.035971,38.898590); 
    23         var marker0 = new GMarker(point0); 
    24  
    25         map.addOverlay(marker0) 
    26  
    27         GEvent.addListener(marker0, "click", function() { 
    28             marker0.openInfoWindowHtml("The White House"); 
    29         }); 
    30     //]]> 
    31  
     10    // Show map points 
     11<?php foreach ($markers as $marker): ?> 
     12    <?php echo $marker->render(1)."\n" ?> 
     13<?php endforeach ?> 
    3214} 
    33 window.onload = showmap; 
    34 </script> 
  • trunk/modules/gmaps/views/gmaps/jquery.php

    r2084 r2158  
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
     2<html xmlns="http://www.w3.org/1999/xhtml"> 
     3<head> 
     4<meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
     5<title>Gmaps jQuery + XML Example</title> 
     6<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo Config::item('gmaps.api_key') ?>" type="text/javascript"></script> 
     7<?php echo html::script('jquery.min') ?> 
     8 
     9<script type="text/javascript"> 
     10$(document).ready(function() 
     11{ 
     12    if (GBrowserIsCompatible()) 
     13    { 
     14        // Initialize the map. 
     15        map = new GMap(document.getElementById('map')); 
     16        map.addControl(new GLargeMapControl()); 
     17        map.centerAndZoom(new GPoint(0,35), 16); 
     18        map.enableScrollWheelZoom(); 
     19 
     20        // Disable the scrollwheel from scrolling the map 
     21        GEvent.addDomListener(map.getContainer(), 'DOMMouseScroll', function(e) 
     22        { 
     23            e.preventDefault(); 
     24            e.returnValue = false; 
     25        }); 
     26 
     27        // Load map markers 
     28        $.ajax 
     29        ({ 
     30            type: 'GET', 
     31            url: '<?php echo url::site('google_map/xml') ?>', 
     32            dataType: 'xml', 
     33            success: function(data, status) 
     34            { 
     35                $('marker', data).each(function() 
     36                { 
     37                    // Current marker 
     38                    var node = $(this); 
     39 
     40                    // Extract HTML 
     41                    var html = node.find('html').text(); 
     42 
     43                    // Create a new map marker 
     44                    var marker = new GMarker(new GLatLng(node.attr("lat"), node.attr("lon"))); 
     45                    GEvent.addListener(marker, "click", function() 
     46                    { 
     47                        marker.openInfoWindowHtml(html); 
     48                    }); 
     49 
     50                    // Add the marker to the map 
     51                    map.addOverlay(marker); 
     52                }); 
     53            }, 
     54            error: function(request, status, error) 
     55            { 
     56                alert('There was an error retrieving the marker information, please refresh the page to try again.'); 
     57            } 
     58        }); 
     59    } 
     60}); 
     61// Unload the map when the window is closed 
     62$(document.body).unload(function(){ GBrowserIsCompatible() && GUnload(); }); 
     63</script> 
     64</head> 
     65<body> 
     66<p>You can use your scroll wheel to zoom in and out of the map.</p> 
     67<div id="map" style="width:600px;height:400px;"></div> 
     68</body> 
     69</html>