Show
Ignore:
Timestamp:
02/23/2008 01:41:41 PM (9 months ago)
Author:
Shadowhand
Message:

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

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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