Changeset 3179 for trunk/modules

Show
Ignore:
Timestamp:
07/21/2008 11:44:36 AM (4 months ago)
Author:
Shadowhand
Message:

Updated Gmap module:

  • Changed the format of api_uri to accept a component and an array of parameters, similar to html::anchor
  • Minor optimizations and cleanups
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/gmaps/libraries/Gmap.php

    r3163 r3179  
    1313 
    1414        /** 
     15         * Return GMap javascript url 
     16         * 
     17         * @param   string  API component 
     18         * @param   array   API parameters 
     19         * @return  string 
     20         */ 
     21         public static function api_uri($component = 'jsapi', $parameters = NULL) 
     22         { 
     23                if (empty($parameters['key'])) 
     24                { 
     25                        // Set the API key 
     26                        $parameters['key'] = Kohana::config('gmaps.api_key'); 
     27                } 
     28 
     29                if (empty($parameters['ie'])) 
     30                { 
     31                        // Set input encoding to UTF-8 
     32                        $parameters['ie'] = 'utf-8'; 
     33                } 
     34 
     35                if (empty($parameters['oe'])) 
     36                { 
     37                        // Set ouput encoding to input encoding 
     38                        $parameters['oe'] = $parameters['ie']; 
     39                } 
     40 
     41                return html::specialchars('http://www.google.com/'.$component.'?'.http_build_query($parameters), FALSE); 
     42         } 
     43 
     44        /** 
    1545         * Retrieves the latitude and longitude of an address. 
    1646         * 
     
    87117                return $xml; 
    88118        } 
    89          
     119 
    90120        /** 
    91121         * Returns an image map 
     
    99129         * @return string 
    100130         */ 
    101         public static function static_map($lat = 0, $lon = 0, $zoom = 6, $type = 'roadmap', $width = 300, $height = 300) 
    102         { 
    103                 $api_url = 'http://maps.google.com/staticmap?key='.Kohana::config('gmaps.api_key'); 
    104  
     131        public static function static_map($lat = 0, $lon = 0, $zoom = 6, $type = NULL, $width = 300, $height = 300) 
     132        { 
     133                // Valid map types 
    105134                $types = array('roadmap', 'mobile'); 
    106135 
    107                 $width = min(640, (int) $width); 
    108         $height = min(640, (int) $height); 
    109  
    110                 if ($width <= 0 OR $height <= 0) 
    111                         throw new Kohana_Exception('gmaps.invalid_dimensions', $width, $height); 
    112  
    113                 $api_url = $api_url.'&amp;size='.$width.'x'.$height; 
    114                  
     136                // Maximum width and height are 640px 
     137                $width = min(640, abs($width)); 
     138        $height = min(640, abs($height)); 
     139 
     140                $parameters['size'] = $width.'x'.$height; 
     141 
     142                // Minimum zoom = 0, maximum zoom = 19 
     143                $parameters['zoom'] = min(0, max(19, abs($zoom))); 
     144 
    115145                if (in_array($type, $types)) 
    116             $api_url = $api_url.'&amp;maptype='.$type; 
    117                  
     146                { 
     147                        // Set map type 
     148                        $parameters['maptype'] = $type; 
     149                } 
     150 
    118151                if (is_array($lat)) 
    119152                { 
    120                         foreach ($lat as $key => $value) 
    121                                 $markers[] = $key.','.$value; 
    122  
    123                         $api_url = $api_url.'&amp;markers='.implode('|', $markers); 
     153                        foreach ($lat as $_lat => $_lon) 
     154                        { 
     155                                $parameters['markers'][] = $_lat.','.$_lon; 
     156                        } 
     157 
     158                        $parameters['markers'] = implode('|', $parameters['markers']); 
    124159                } 
    125160                else 
    126161                { 
    127                         $api_url = $api_url.'&amp;center='.$lat.','.$lon.'&amp;zoom='.$zoom; 
    128                 } 
    129  
    130         return $api_url; 
     162                        $parameters['center'] = $lat.','.$lon; 
     163                } 
     164 
     165        return Gmap::api_url('staticmap', $parameters); 
    131166        } 
    132167 
     
    138173        protected $overview_control; 
    139174        protected $type_control = FALSE; 
    140          
     175 
    141176        // Map types 
    142177        protected $types = array(); 
     
    158193                $this->options = new Gmap_Options((array) $options); 
    159194        } 
    160          
    161         /** 
    162          * Return GMap javascript url 
    163          *  
    164          * @return string 
    165          */ 
    166          public function api_uri() 
    167          { 
    168             return 'http://www.google.com/jsapi?key='.Kohana::config('gmaps.api_key').'&amp;oe=utf-8'; 
    169          } 
    170195 
    171196        /** 
     
    196221        { 
    197222                // Set the control type 
    198                 $this->control = (strtolower($size) === 'small') ? 'Small' : 'Large'; 
    199  
    200                 return $this; 
    201         } 
    202          
     223                $this->control = (strtolower($size) == 'small') ? 'Small' : 'Large'; 
     224 
     225                return $this; 
     226        } 
     227 
    203228        /** 
    204229         * Set the GMap overview map. 
     
    213238                $size = (is_int($width) AND is_int($height)) ? 'new GSize('.$width.','.$height.')' : ''; 
    214239                $this->overview_control = 'map.addControl(new google.maps.OverviewMapControl('.$size.'));'; 
    215                  
    216                 return $this; 
    217         } 
    218          
     240 
     241                return $this; 
     242        } 
     243 
    219244        /** 
    220245         * Set the GMap type controls. 
     
    229254        { 
    230255                $this->type_control = TRUE; 
    231                  
     256 
    232257                $types = array 
    233258                ( 
    234259                        'G_NORMAL_MAP','G_SATELLITE_MAP','G_HYBRID_MAP','G_PHYSICAL_MAP' 
    235260                ); 
    236                  
     261 
    237262                if ($type !== NULL and in_array($type, $types, true)) 
    238263                { 
     
    240265                        $this->types[$type] = (strtolower($action) === 'remove') ? 'remove' : 'add'; 
    241266                } 
    242                  
     267 
    243268                return $this; 
    244269        } 
     
    274299                // Map 
    275300                $map = 'var map = new google.maps.Map2(document.getElementById("'.$this->id.'"));'; 
    276                  
     301 
    277302                // Map controls 
    278303                $controls[] = empty($this->control) ? '' : 'map.addControl(new google.maps.'.$this->control.'MapControl());'; 
     
    281306                if ($this->type_control === TRUE) 
    282307                { 
    283                         if (count($this->types) > 0)  
     308                        if (count($this->types) > 0) 
    284309                        { 
    285310                                foreach($this->types as $type => $action) 
    286311                                        $controls[] = 'map.'.$action.'MapType('.$type.');'; 
    287312                        } 
    288                          
     313 
    289314                        $controls[] = 'map.addControl(new google.maps.MapTypeControl());'; 
    290315                } 
    291                  
     316 
    292317                if ( ! empty($this->overview_control)) 
    293318                        $controls[] = $this->overview_control;