Source for file maps.php

Documentation is available at maps.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Google
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Google Maps embed class for the Joomla Platform.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Google
  17.  * @since       12.3
  18.  */
  19. {
  20.  
  21.     /**
  22.      * @var    JHttp  The HTTP client object to use in sending HTTP requests.
  23.      * @since  12.3
  24.      */
  25.     protected $http;
  26.  
  27.     /**
  28.      * Constructor.
  29.      *
  30.      * @param   JRegistry  $options  Google options object
  31.      * @param   JUri       $uri      URL of the page being rendered
  32.      * @param   JHttp      $http     Http client for geocoding requests
  33.      *
  34.      * @since   12.3
  35.      */
  36.     public function __construct(JRegistry $options nullJUri $uri nullJHttp $http null)
  37.     {
  38.         parent::__construct($options null$uri null);
  39.         $this->http = $http $http new JHttp($this->options);
  40.     }
  41.  
  42.     /**
  43.      * Method to get the API key
  44.      *
  45.      * @return  string  The Google Maps API key
  46.      *
  47.      * @since   12.3
  48.      */
  49.     public function getKey()
  50.     {
  51.         return $this->getOption('key');
  52.     }
  53.  
  54.     /**
  55.      * Method to set the API key
  56.      *
  57.      * @param   string  $key  The Google Maps API key
  58.      *
  59.      * @return  JGoogleEmbedMaps  The object for method chaining
  60.      *
  61.      * @since   12.3
  62.      */
  63.     public function setKey($key)
  64.     {
  65.         $this->setOption('key'$key);
  66.  
  67.         return $this;
  68.     }
  69.  
  70.     /**
  71.      * Method to get the id of the map div
  72.      *
  73.      * @return  string  The ID
  74.      *
  75.      * @since   12.3
  76.      */
  77.     public function getMapID()
  78.     {
  79.         return $this->getOption('mapid'$this->getOption('mapid''map_canvas';
  80.     }
  81.  
  82.     /**
  83.      * Method to set the map div id
  84.      *
  85.      * @param   string  $id  The ID
  86.      *
  87.      * @return  JGoogleEmbedMaps  The object for method chaining
  88.      *
  89.      * @since   12.3
  90.      */
  91.     public function setMapID($id)
  92.     {
  93.         $this->setOption('mapid'$id);
  94.  
  95.         return $this;
  96.     }
  97.  
  98.     /**
  99.      * Method to get the class of the map div
  100.      *
  101.      * @return  string  The class
  102.      *
  103.      * @since   12.3
  104.      */
  105.     public function getMapClass()
  106.     {
  107.         return $this->getOption('mapclass'$this->getOption('mapclass''';
  108.     }
  109.  
  110.     /**
  111.      * Method to set the map div class
  112.      *
  113.      * @param   string  $class  The class
  114.      *
  115.      * @return  JGoogleEmbedMaps  The object for method chaining
  116.      *
  117.      * @since   12.3
  118.      */
  119.     public function setMapClass($class)
  120.     {
  121.         $this->setOption('mapclass'$class);
  122.  
  123.         return $this;
  124.     }
  125.  
  126.     /**
  127.      * Method to get the style of the map div
  128.      *
  129.      * @return  string  The style
  130.      *
  131.      * @since   12.3
  132.      */
  133.     public function getMapStyle()
  134.     {
  135.         return $this->getOption('mapstyle'$this->getOption('mapstyle''';
  136.     }
  137.  
  138.     /**
  139.      * Method to set the map div style
  140.      *
  141.      * @param   string  $style  The style
  142.      *
  143.      * @return  JGoogleEmbedMaps  The object for method chaining
  144.      *
  145.      * @since   12.3
  146.      */
  147.     public function setMapStyle($style)
  148.     {
  149.         $this->setOption('mapstyle'$style);
  150.  
  151.         return $this;
  152.     }
  153.  
  154.     /**
  155.      * Method to get the map type setting
  156.      *
  157.      * @return  string  The class
  158.      *
  159.      * @since   12.3
  160.      */
  161.     public function getMapType()
  162.     {
  163.         return $this->getOption('maptype'$this->getOption('maptype''ROADMAP';
  164.     }
  165.  
  166.     /**
  167.      * Method to set the map type ()
  168.      *
  169.      * @param   string  $type  Valid types are ROADMAP, SATELLITE, HYBRID, and TERRAIN
  170.      *
  171.      * @return  JGoogleEmbedMaps  The object for method chaining
  172.      *
  173.      * @since   12.3
  174.      */
  175.     public function setMapType($type)
  176.     {
  177.         $this->setOption('maptype'strtoupper($type));
  178.  
  179.         return $this;
  180.     }
  181.  
  182.     /**
  183.      * Method to get additional map options
  184.      *
  185.      * @return  string  The options
  186.      *
  187.      * @since   12.3
  188.      */
  189.     public function getAdditionalMapOptions()
  190.     {
  191.         return $this->getOption('mapoptions'$this->getOption('mapoptions'array();
  192.     }
  193.  
  194.     /**
  195.      * Method to add additional map options
  196.      *
  197.      * @param   array  $options  Additional map options
  198.      *
  199.      * @return  JGoogleEmbedMaps  The object for method chaining
  200.      *
  201.      * @since   12.3
  202.      */
  203.     public function setAdditionalMapOptions($options)
  204.     {
  205.         $this->setOption('mapoptions'$options);
  206.  
  207.         return $this;
  208.     }
  209.  
  210.     /**
  211.      * Method to get additional map options
  212.      *
  213.      * @return  string  The options
  214.      *
  215.      * @since   12.3
  216.      */
  217.     public function getAdditionalJavascript()
  218.     {
  219.         return $this->getOption('extrascript'$this->getOption('extrascript''';
  220.     }
  221.  
  222.     /**
  223.      * Method to add additional javascript
  224.      *
  225.      * @param   array  $script  Additional javascript
  226.      *
  227.      * @return  JGoogleEmbedMaps  The object for method chaining
  228.      *
  229.      * @since   12.3
  230.      */
  231.     public function setAdditionalJavascript($script)
  232.     {
  233.         $this->setOption('extrascript'$script);
  234.  
  235.         return $this;
  236.     }
  237.  
  238.     /**
  239.      * Method to get the zoom
  240.      *
  241.      * @return  int  The zoom level
  242.      *
  243.      * @since   12.3
  244.      */
  245.     public function getZoom()
  246.     {
  247.         return $this->getOption('zoom'$this->getOption('zoom'0;
  248.     }
  249.  
  250.     /**
  251.      * Method to set the map zoom
  252.      *
  253.      * @param   int  $zoom  Zoom level (0 is whole world)
  254.      *
  255.      * @return  JGoogleEmbedMaps  The object for method chaining
  256.      *
  257.      * @since   12.3
  258.      */
  259.     public function setZoom($zoom)
  260.     {
  261.         $this->setOption('zoom'$zoom);
  262.  
  263.         return $this;
  264.     }
  265.  
  266.     /**
  267.      * Method to set the center of the map
  268.      *
  269.      * @return  mixed  A latitude longitude array or an address string
  270.      *
  271.      * @since   12.3
  272.      */
  273.     public function getCenter()
  274.     {
  275.         return $this->getOption('mapcenter'$this->getOption('mapcenter'array(00);
  276.     }
  277.  
  278.     /**
  279.      * Method to set the center of the map
  280.      *
  281.      * @param   mixed  $location       A latitude/longitude array or an address string
  282.      * @param   mixed  $title          Title of marker or false for no marker
  283.      * @param   array  $markeroptions  Options for marker
  284.      *
  285.      * @return  JGoogleEmbedMaps  The latitude/longitude of the center or false on failure
  286.      *
  287.      * @since   12.3
  288.      */
  289.     public function setCenter($location$title true$markeroptions array())
  290.     {
  291.         if ($title)
  292.         {
  293.             $title is_string($title$title null;
  294.  
  295.             if (!$marker $this->addMarker($location$title$markeroptions))
  296.             {
  297.                 return false;
  298.             }
  299.             $location $marker['loc'];
  300.         }
  301.         elseif (is_string($location))
  302.         {
  303.             $geocode $this->geocodeAddress($location);
  304.  
  305.             if (!$geocode)
  306.             {
  307.                 return false;
  308.             }
  309.  
  310.             $location $geocode['geometry']['location'];
  311.             $location array_values($location);
  312.         }
  313.  
  314.         $this->setOption('mapcenter'$location);
  315.  
  316.         return $this;
  317.     }
  318.  
  319.     /**
  320.      * Add a marker to the map
  321.      *
  322.      * @param   mixed  $location  A latitude longitude array or an address string
  323.      * @param   mixed  $title     The hover-text for the marker
  324.      * @param   array  $options   Options for marker
  325.      *
  326.      * @return  mixed  The marker or false on failure
  327.      *
  328.      * @since   12.3
  329.      */
  330.     public function addMarker($location$title null$options array())
  331.     {
  332.         if (is_string($location))
  333.         {
  334.             if (!$title)
  335.             {
  336.                 $title $location;
  337.             }
  338.  
  339.             $geocode $this->geocodeAddress($location);
  340.  
  341.             if (!$geocode)
  342.             {
  343.                 return false;
  344.             }
  345.  
  346.             $location $geocode['geometry']['location'];
  347.         }
  348.         elseif (!$title)
  349.         {
  350.             $title implode(', '$location);
  351.         }
  352.  
  353.         $location array_values($location);
  354.         $marker array('loc' => $location'title' => $title'options' => $options);
  355.  
  356.         $markers $this->listMarkers();
  357.         $markers[$marker;
  358.         $this->setOption('markers'$markers);
  359.  
  360.         return $marker;
  361.     }
  362.  
  363.     /**
  364.      * List the markers added to the map
  365.      *
  366.      * @return  array  A list of markers
  367.      *
  368.      * @since   12.3
  369.      */
  370.     public function listMarkers()
  371.     {
  372.         return $this->getOption('markers'$this->getOption('markers'array();
  373.     }
  374.  
  375.     /**
  376.      * Delete a marker from the map
  377.      *
  378.      * @param   int  $index  Index of marker to delete (defaults to last added marker)
  379.      *
  380.      * @return  array The latitude/longitude of the deleted marker
  381.      *
  382.      * @since   12.3
  383.      */
  384.     public function deleteMarker($index null)
  385.     {
  386.         $markers $this->listMarkers();
  387.  
  388.         if ($index === null)
  389.         {
  390.             $index count($markers1;
  391.         }
  392.  
  393.         if ($index >= count($markers|| $index 0)
  394.         {
  395.             throw new OutOfBoundsException('Marker index out of bounds.');
  396.         }
  397.  
  398.         $marker $markers[$index];
  399.         unset($markers[$index]);
  400.         $markers array_values($markers);
  401.         $this->setOption('markers'$markers);
  402.  
  403.         return $marker;
  404.     }
  405.  
  406.     /**
  407.      * Checks if the javascript is set to be asynchronous
  408.      *
  409.      * @return  boolean  True if asynchronous
  410.      *
  411.      * @since   12.3
  412.      */
  413.     public function isAsync()
  414.     {
  415.         return $this->getOption('async'=== null true $this->getOption('async');
  416.     }
  417.  
  418.     /**
  419.      * Load javascript asynchronously
  420.      *
  421.      * @return  JGoogleEmbedMaps  The object for method chaining
  422.      *
  423.      * @since   12.3
  424.      */
  425.     public function useAsync()
  426.     {
  427.         $this->setOption('async'true);
  428.  
  429.         return $this;
  430.     }
  431.  
  432.     /**
  433.      * Load javascript synchronously
  434.      *
  435.      * @return  JGoogleEmbedAMaps  The object for method chaining
  436.      *
  437.      * @since   12.3
  438.      */
  439.     public function useSync()
  440.     {
  441.         $this->setOption('async'false);
  442.  
  443.         return $this;
  444.     }
  445.  
  446.     /**
  447.      * Method to get callback function for async javascript loading
  448.      *
  449.      * @return  string  The ID
  450.      *
  451.      * @since   12.3
  452.      */
  453.     public function getAsyncCallback()
  454.     {
  455.         return $this->getOption('callback'$this->getOption('callback''initialize';
  456.     }
  457.  
  458.     /**
  459.      * Method to set the callback function for async javascript loading
  460.      *
  461.      * @param   string  $callback  The callback function name
  462.      *
  463.      * @return  JGoogleEmbedMaps  The object for method chaining
  464.      *
  465.      * @since   12.3
  466.      */
  467.     public function setAsyncCallback($callback)
  468.     {
  469.         $this->setOption('callback'$callback);
  470.  
  471.         return $this;
  472.     }
  473.  
  474.     /**
  475.      * Checks if a sensor is set to be required
  476.      *
  477.      * @return  boolean  True if asynchronous
  478.      *
  479.      * @since   12.3
  480.      */
  481.     public function hasSensor()
  482.     {
  483.         return $this->getOption('sensor'=== null false $this->getOption('sensor');
  484.     }
  485.  
  486.     /**
  487.      * Require access to sensor data
  488.      *
  489.      * @return  JGoogleEmbedMaps  The object for method chaining
  490.      *
  491.      * @since   12.3
  492.      */
  493.     public function useSensor()
  494.     {
  495.         $this->setOption('sensor'true);
  496.  
  497.         return $this;
  498.     }
  499.  
  500.     /**
  501.      * Don't require access to sensor data
  502.      *
  503.      * @return  JGoogleEmbedAMaps  The object for method chaining
  504.      *
  505.      * @since   12.3
  506.      */
  507.     public function noSensor()
  508.     {
  509.         $this->setOption('sensor'false);
  510.  
  511.         return $this;
  512.     }
  513.  
  514.     /**
  515.      * Checks how the script should be loaded
  516.      *
  517.      * @return  string  Autoload type (onload, jquery, mootools, or false)
  518.      *
  519.      * @since   12.3
  520.      */
  521.     public function getAutoload()
  522.     {
  523.         return $this->getOption('autoload'$this->getOption('autoload''false';
  524.     }
  525.  
  526.     /**
  527.      * Automatically add the callback to the window
  528.      *
  529.      * @param   string  $type  The method to add the callback (options are onload, jquery, mootools, and false)
  530.      *
  531.      * @return  JGoogleEmbedAMaps  The object for method chaining
  532.      *
  533.      * @since   12.3
  534.      */
  535.     public function setAutoload($type 'onload')
  536.     {
  537.         $this->setOption('autoload'$type);
  538.  
  539.         return $this;
  540.     }
  541.  
  542.     /**
  543.      * Get code to load Google Maps javascript
  544.      *
  545.      * @return  string  Javascript code
  546.      *
  547.      * @since   12.3
  548.      */
  549.     public function getHeader()
  550.     {
  551.         if (!$this->getOption('key'))
  552.         {
  553.             throw new UnexpectedValueException('A Google Maps API key is required.');
  554.         }
  555.  
  556.         $zoom $this->getZoom();
  557.         $center $this->getCenter();
  558.         $maptype $this->getMapType();
  559.         $id $this->getMapID();
  560.         $scheme $this->isSecure('https' 'http';
  561.         $key $this->getKey();
  562.         $sensor $this->hasSensor('true' 'false';
  563.  
  564.         $setup 'var mapOptions = {';
  565.         $setup .= "zoom: {$zoom},";
  566.         $setup .= "center: new google.maps.LatLng({$center[0]},{$center[1]}),";
  567.         $setup .= "mapTypeId: google.maps.MapTypeId.{$maptype},";
  568.         $setup .= substr(json_encode($this->getAdditionalMapOptions())1-1);
  569.         $setup .= '};';
  570.         $setup .= "var map = new google.maps.Map(document.getElementById('{$id}'), mapOptions);";
  571.  
  572.         foreach ($this->listMarkers(as $marker)
  573.         {
  574.             $loc $marker['loc'];
  575.             $title $marker['title'];
  576.             $options $marker['options'];
  577.  
  578.             $setup .= 'new google.maps.Marker({';
  579.             $setup .= "position: new google.maps.LatLng({$loc[0]},{$loc[1]}),";
  580.             $setup .= 'map: map,';
  581.             $setup .= "title:'{$title}',";
  582.             $setup .= substr(json_encode($options)1-1);
  583.             $setup .= '});';
  584.         }
  585.  
  586.         $setup .= $this->getAdditionalJavascript();
  587.  
  588.         if ($this->isAsync())
  589.         {
  590.             $asynccallback $this->getAsyncCallback();
  591.  
  592.             $output '<script type="text/javascript">';
  593.             $output .= "function {$asynccallback}() {";
  594.             $output .= $setup;
  595.             $output .= '}';
  596.  
  597.             $onload "function() {";
  598.             $onload .= 'var script = document.createElement("script");';
  599.             $onload .= 'script.type = "text/javascript";';
  600.             $onload .= "script.src = '{$scheme}://maps.googleapis.com/maps/api/js?key={$key}&sensor={$sensor}&callback={$asynccallback}';";
  601.             $onload .= 'document.body.appendChild(script);';
  602.             $onload .= '}';
  603.         }
  604.         else
  605.         {
  606.             $output "<script type='text/javascript' src='{$scheme}://maps.googleapis.com/maps/api/js?key={$key}&sensor={$sensor}'>";
  607.             $output .= '</script>';
  608.             $output .= '<script type="text/javascript">';
  609.  
  610.             $onload "function() {";
  611.             $onload .= $setup;
  612.             $onload .= '}';
  613.         }
  614.  
  615.         switch ($this->getAutoload())
  616.         {
  617.             case 'onload':
  618.             $output .= "window.onload={$onload};";
  619.             break;
  620.  
  621.             case 'jquery':
  622.             $output .= "$(document).ready({$onload});";
  623.             break;
  624.  
  625.             case 'mootools':
  626.             $output .= "window.addEvent('domready',{$onload});";
  627.             break;
  628.         }
  629.  
  630.         $output .= '</script>';
  631.  
  632.         return $output;
  633.     }
  634.  
  635.     /**
  636.      * Method to retrieve the div that the map is loaded into
  637.      *
  638.      * @return  string  The body
  639.      *
  640.      * @since   12.3
  641.      */
  642.     public function getBody()
  643.     {
  644.         $id $this->getMapID();
  645.         $class $this->getMapClass();
  646.         $style $this->getMapStyle();
  647.  
  648.         $output "<div id='{$id}'";
  649.  
  650.         if (!empty($class))
  651.         {
  652.             $output .= " class='{$class}'";
  653.         }
  654.  
  655.         if (!empty($style))
  656.         {
  657.             $output .= " style='{$style}'";
  658.         }
  659.  
  660.         $output .= '></div>';
  661.  
  662.         return $output;
  663.     }
  664.  
  665.     /**
  666.      * Method to get the location information back from an address
  667.      *
  668.      * @param   string  $address  The address to geocode
  669.      *
  670.      * @return  array  An array containing Google's geocode data
  671.      *
  672.      * @since   12.3
  673.      */
  674.     public function geocodeAddress($address)
  675.     {
  676.         $url 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' urlencode($address);
  677.         $response $this->http->get($url);
  678.  
  679.         if ($response->code 200 || $response->code >= 300)
  680.         {
  681.             throw new RuntimeException('Error code ' $response->code ' received geocoding address: ' $response->body '.');
  682.         }
  683.  
  684.         $data json_decode($response->bodytrue);
  685.  
  686.         if (!$data)
  687.         {
  688.             throw new RuntimeException('Invalid json received geocoding address: ' $response->body '.');
  689.         }
  690.         if ($data['status'!= 'OK')
  691.         {
  692.             return null;
  693.         }
  694.  
  695.         return $data['results'][0];
  696.     }
  697. }

Documentation generated on Tue, 19 Nov 2013 15:07:38 +0100 by phpDocumentor 1.4.3