Advertisement
Guest User

Untitled

a guest
Apr 20th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.74 KB | None | 0 0
  1. <?php
  2.  
  3. /***************************************************************
  4.  * Copyright notice
  5.  *
  6.  * (c) 2013 Yohann CERDAN <cerdanyohann@yahoo.fr>
  7.  * All rights reserved
  8.  *
  9.  * This script is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * This copyright notice MUST APPEAR in all copies of the script!
  15.  ***************************************************************/
  16.  
  17. /**
  18.  * Class to use the Google Maps v3 API
  19.  *
  20.  * @author Yohann CERDAN <cerdanyohann@yahoo.fr>
  21.  */
  22. class GoogleMapAPI
  23. {
  24.     /** GoogleMap ID for the HTML DIV and identifier for all the methods (to have several gmaps) **/
  25.     protected $googleMapId = 'googlemapapi';
  26.  
  27.     /** GoogleMap  Direction ID for the HTML DIV **/
  28.     protected $googleMapDirectionId = 'route';
  29.  
  30.     /** Width of the gmap **/
  31.     protected $width = '';
  32.  
  33.     /** Height of the gmap **/
  34.     protected $height = '';
  35.  
  36.     /** Icon width of the gmarker **/
  37.     protected $iconWidth = 57;
  38.  
  39.     /** Icon height of the gmarker **/
  40.     protected $iconHeight = 34;
  41.  
  42.     /** Infowindow width of the gmarker **/
  43.     protected $infoWindowWidth = 250;
  44.  
  45.     /** Default zoom of the gmap **/
  46.     protected $zoom = 9;
  47.  
  48.     /** Enable the zoom of the Infowindow **/
  49.     protected $enableWindowZoom = FALSE;
  50.  
  51.     /** Default zoom of the Infowindow **/
  52.     protected $infoWindowZoom = 3;
  53.  
  54.     /** Lang of the gmap **/
  55.     protected $lang = 'fr';
  56.  
  57.     /**Center of the gmap **/
  58.     protected $center = 'Paris France';
  59.  
  60.     /** Content of the HTML generated **/
  61.     protected $content = '';
  62.  
  63.     /** Add the direction button to the infowindow **/
  64.     protected $displayDirectionFields = FALSE;
  65.  
  66.     /** Hide the marker by default **/
  67.     protected $defaultHideMarker = FALSE;
  68.  
  69.     /** Extra content (marker, etc...) **/
  70.     protected $contentMarker = '';
  71.  
  72.     /** Use clusterer to display a lot of markers on the gmap **/
  73.     protected $useClusterer = FALSE;
  74.     protected $gridSize = 100;
  75.     protected $maxZoom = 9;
  76.     protected $clustererLibrarypath = 'http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer_packed.js';
  77.  
  78.     /** Enable automatic center/zoom **/
  79.     protected $enableAutomaticCenterZoom = FALSE;
  80.  
  81.     /** maximum longitude of all markers **/
  82.     protected $maxLng = -1000000;
  83.  
  84.     /** minimum longitude of all markers **/
  85.     protected $minLng = 1000000;
  86.  
  87.     /** max latitude of all markers **/
  88.     protected $maxLat = -1000000;
  89.  
  90.     /** min latitude of all markers **/
  91.     protected $minLat = 1000000;
  92.  
  93.     /** map center latitude (horizontal), calculated automatically as markers are added to the map **/
  94.     protected $centerLat = NULL;
  95.  
  96.     /** map center longitude (vertical),  calculated automatically as markers are added to the map **/
  97.     protected $centerLng = NULL;
  98.  
  99.     /** factor by which to fudge the boundaries so that when we zoom encompass, the markers aren't too close to the edge **/
  100.     protected $coordCoef = 0.01;
  101.  
  102.     /** Type of map to display **/
  103.     protected $mapType = 'ROADMAP';
  104.  
  105.     /** Include the JS or not (if you have multiple maps **/
  106.     protected $includeJs = TRUE;
  107.  
  108.     /**
  109.      * Class constructor
  110.      */
  111.     public function __construct() {
  112.     }
  113.  
  114.     /**
  115.      * Set the useClusterer parameter (optimization to display a lot of marker)
  116.      *
  117.      * @param boolean $useClusterer         use cluster or not
  118.      * @param int     $gridSize             grid size (The grid size of a cluster in pixel.Each cluster will be a square.If you want the algorithm to run faster, you can set this value larger.The default value is 100.)
  119.      * @param int     $maxZoom              maxZoom (The max zoom level monitored by a marker cluster.If not given, the marker cluster assumes the maximum map zoom level.When maxZoom is reached or exceeded all markers will be shown without cluster.)
  120.      * @param string  $clustererLibraryPath clustererLibraryPath
  121.      *
  122.      * @return void
  123.      */
  124.     public function setClusterer($useClusterer, $gridSize = 100, $maxZoom = 9, $clustererLibraryPath = '') {
  125.         $this->useClusterer = $useClusterer;
  126.         $this->gridSize = $gridSize;
  127.         $this->maxZoom = $maxZoom;
  128.         ($clustererLibraryPath == '')
  129.             ? $this->clustererLibraryPath = 'http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer_packed.js'
  130.             : $this->clustererLibraryPath = $clustererLibraryPath;
  131.     }
  132.  
  133.     /**
  134.      * Set the type of map, can be :
  135.      * HYBRID, TERRAIN, ROADMAP, SATELLITE
  136.      *
  137.      * @param string $type
  138.      * @return void
  139.      */
  140.     public function setMapType($type) {
  141.         $mapsType = array('ROADMAP', 'HYBRID', 'TERRAIN', 'SATELLITE');
  142.         if (!in_array(strtoupper($type), $mapsType)) {
  143.             $this->mapType = $mapsType[0];
  144.         } else {
  145.             $this->mapType = strtoupper($type);
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * Set the ID of the default gmap DIV
  151.      *
  152.      * @param string $googleMapId the google div ID
  153.      *
  154.      * @return void
  155.      */
  156.     public function setDivId($googleMapId) {
  157.         $this->googleMapId = $googleMapId;
  158.     }
  159.  
  160.     /**
  161.      * Set the ID of the default gmap direction DIV
  162.      *
  163.      * @param string $googleMapDirectionId GoogleMap  Direction ID for the HTML DIV
  164.      *
  165.      * @return void
  166.      */
  167.     public function setDirectionDivId($googleMapDirectionId) {
  168.         $this->googleMapDirectionId = $googleMapDirectionId;
  169.     }
  170.  
  171.     /**
  172.      * Set the size of the gmap
  173.      *
  174.      * @param int $width  GoogleMap  width
  175.      * @param int $height GoogleMap  height
  176.      *
  177.      * @return void
  178.      */
  179.     public function setSize($width, $height) {
  180.         $this->width = $width;
  181.         $this->height = $height;
  182.     }
  183.  
  184.     /**
  185.      * Set the with of the gmap infowindow (on marker clik)
  186.      *
  187.      * @param int $infoWindowWidth GoogleMap  info window width
  188.      *
  189.      * @return void
  190.      */
  191.     public function setInfoWindowWidth($infoWindowWidth) {
  192.         $this->infoWindowWidth = $infoWindowWidth;
  193.     }
  194.  
  195.     /**
  196.      * Set the size of the icon markers
  197.      *
  198.      * @param int $iconWidth  GoogleMap  marker icon width
  199.      * @param int $iconHeight GoogleMap  marker icon height
  200.      *
  201.      * @return void
  202.      */
  203.     public function setIconSize($iconWidth, $iconHeight) {
  204.         $this->iconWidth = $iconWidth;
  205.         $this->iconHeight = $iconHeight;
  206.     }
  207.  
  208.     /**
  209.      * Set the lang of the gmap
  210.      *
  211.      * @param string $lang GoogleMap  lang : fr,en,..
  212.      *
  213.      * @return void
  214.      */
  215.     public function setLang($lang) {
  216.         $this->lang = $lang;
  217.     }
  218.  
  219.     /**
  220.      * Set the zoom of the gmap
  221.      *
  222.      * @param int $zoom GoogleMap  zoom.
  223.      *
  224.      * @return void
  225.      */
  226.     public function setZoom($zoom) {
  227.         $this->zoom = $zoom;
  228.     }
  229.  
  230.     /**
  231.      * Set the zoom of the infowindow
  232.      *
  233.      * @param int $infoWindowZoom GoogleMap  zoom.
  234.      *
  235.      * @return void
  236.      */
  237.     public function setInfoWindowZoom($infoWindowZoom) {
  238.         $this->infoWindowZoom = $infoWindowZoom;
  239.     }
  240.  
  241.     /**
  242.      * Enable the zoom on the marker when you click on it
  243.      *
  244.      * @param int $enableWindowZoom GoogleMap  zoom.
  245.      *
  246.      * @return void
  247.      */
  248.     public function setEnableWindowZoom($enableWindowZoom) {
  249.         $this->enableWindowZoom = $enableWindowZoom;
  250.     }
  251.  
  252.     /**
  253.      * Enable theautomatic center/zoom at the gmap load
  254.      *
  255.      * @param int $enableAutomaticCenterZoom GoogleMap  zoom.
  256.      *
  257.      * @return void
  258.      */
  259.     public function setEnableAutomaticCenterZoom($enableAutomaticCenterZoom) {
  260.         $this->enableAutomaticCenterZoom = $enableAutomaticCenterZoom;
  261.     }
  262.  
  263.     /**
  264.      * Set the center of the gmap (an address)
  265.      *
  266.      * @param string $center GoogleMap  center (an address)
  267.      *
  268.      * @return void
  269.      */
  270.     public function setCenter($center) {
  271.         $this->center = $center;
  272.     }
  273.  
  274.     /**
  275.      * Set the center of the gmap (a lat and lng)
  276.      *
  277.      * @param string $lat
  278.      * @param string $lng
  279.      *
  280.      * @return void
  281.      */
  282.     public function setCenterLatLng($lat, $lng) {
  283.         $this->centerLatLng = 'new google.maps.LatLng(' . $lat . ', ' . $lng . ')';
  284.     }
  285.  
  286.     /**
  287.      * Set the center of the gmap
  288.      *
  289.      * @param boolean $displayDirectionFields display directions or not in the info window
  290.      *
  291.      * @return void
  292.      */
  293.     public function setDisplayDirectionFields($displayDirectionFields) {
  294.         $this->displayDirectionFields = $displayDirectionFields;
  295.     }
  296.  
  297.     /**
  298.      * Set the defaultHideMarker
  299.      *
  300.      * @param boolean $defaultHideMarker hide all the markers on the map by default
  301.      *
  302.      * @return void
  303.      */
  304.     public function setDefaultHideMarker($defaultHideMarker) {
  305.         $this->defaultHideMarker = $defaultHideMarker;
  306.     }
  307.  
  308.     /**
  309.      * Set the includeJs
  310.      *
  311.      * @param boolean $includeJs do not include JS
  312.      *
  313.      * @return void
  314.      */
  315.     public function setincludeJs($includeJs) {
  316.         $this->includeJs = $includeJs;
  317.     }
  318.  
  319.     /**
  320.      * Get the google map content
  321.      *
  322.      * @return string the google map html code
  323.      */
  324.     public function getGoogleMap() {
  325.         return $this->content;
  326.     }
  327.  
  328.     /**
  329.      * Get URL content using cURL.
  330.      *
  331.      * @param string $url the url
  332.      *
  333.      * @return string the html code
  334.      *
  335.      * @todo add proxy settings
  336.      */
  337.     public function getContent($url) {
  338.         $curl = curl_init();
  339.         curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  340.         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
  341.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  342.         curl_setopt($curl, CURLOPT_URL, $url);
  343.         $data = curl_exec($curl);
  344.         curl_close($curl);
  345.         return $data;
  346.     }
  347.  
  348.     /**
  349.      * Geocoding an address (address -> lat,lng)
  350.      *
  351.      * @param string $address an address
  352.      *
  353.      * @return array array with precision, lat & lng
  354.      */
  355.     public function geocoding($address) {
  356.         $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=true';
  357.  
  358.         if (function_exists('curl_init')) {
  359.             $data = $this->getContent($url);
  360.         } else {
  361.             $data = file_get_contents($url);
  362.         }
  363.  
  364.         $response = json_decode($data, TRUE);
  365.         $status = $response['status'];
  366.  
  367.         if ($status == 'OK') {
  368.             $return = array(
  369.                 $status,
  370.                 $response['results'][0]['types'],
  371.                 $response['results'][0]['geometry']['location']['lat'],
  372.                 $response['results'][0]['geometry']['location']['lng']
  373.             ); // successful geocode, $status-$precision-$lat-$lng
  374.         } else {
  375.             echo '<!-- geocoding : failure to geocode : ' . $status . " -->\n";
  376.             $return = NULL; // failure to geocode
  377.         }
  378.  
  379.         return $return;
  380.     }
  381.  
  382.     /**
  383.      * Add marker by his coord
  384.      *
  385.      * @param string $lat      lat
  386.      * @param string $lng      lngs
  387.      * @param string $title    title
  388.      * @param string $html     html code display in the info window
  389.      * @param string $category marker category
  390.      * @param string $icon     an icon url
  391.      *
  392.      * @return void
  393.      */
  394.     public function addMarkerByCoords($lat, $lng, $title, $html = '', $category = '', $icon = '') {
  395.         if ($icon == '') {
  396.             $icon = 'http://maps.gstatic.com/mapfiles/markers2/marker.png';
  397.         }
  398.  
  399.         // Save the lat/lon to enable the automatic center/zoom
  400.         $this->maxLng = (float)max((float)$lng, $this->maxLng);
  401.         $this->minLng = (float)min((float)$lng, $this->minLng);
  402.         $this->maxLat = (float)max((float)$lat, $this->maxLat);
  403.         $this->minLat = (float)min((float)$lat, $this->minLat);
  404.         $this->centerLng = (float)($this->minLng + $this->maxLng) / 2;
  405.         $this->centerLat = (float)($this->minLat + $this->maxLat) / 2;
  406.  
  407.         $this->contentMarker .= "\t" . 'addMarker(new google.maps.LatLng("' . $lat . '","' . $lng . '"),"' . $title . '","' . $html . '","' . $category . '","' . $icon . '",map' . $this->googleMapId . ');' . "\n";
  408.     }
  409.  
  410.     /**
  411.      * Add marker by his address
  412.      *
  413.      * @param string $address  an ddress
  414.      * @param string $title    title
  415.      * @param string $content  html code display in the info window
  416.      * @param string $category marker category
  417.      * @param string $icon     an icon url
  418.      *
  419.      * @return void
  420.      */
  421.     public function addMarkerByAddress($address, $title = '', $content = '', $category = '', $icon = '') {
  422.         $point = $this->geocoding($address);
  423.         if ($point !== NULL) {
  424.             $this->addMarkerByCoords($point[2], $point[3], $title, $content, $category, $icon);
  425.         } else {
  426.             echo '<!-- addMarkerByAddress : ADDRESS NOT FOUND ' . strip_tags($address) . " -->\n";
  427.         }
  428.     }
  429.  
  430.     /**
  431.      * Add marker by an array of coord
  432.      *
  433.      * @param string $coordtab an array of lat,lng,content
  434.      * @param string $category marker category
  435.      * @param string $icon     an icon url
  436.      *
  437.      * @return void
  438.      */
  439.     public function addArrayMarkerByCoords($coordtab, $category = '', $icon = '') {
  440.         foreach ($coordtab as $coord) {
  441.             $this->addMarkerByCoords($coord[0], $coord[1], $coord[2], $coord[3], $category, $icon);
  442.         }
  443.     }
  444.  
  445.     /**
  446.      * Add marker by an array of address
  447.      *
  448.      * @param string $coordtab an array of address
  449.      * @param string $category marker category
  450.      * @param string $icon     an icon url
  451.      *
  452.      * @return void
  453.      */
  454.     public function addArrayMarkerByAddress($coordtab, $category = '', $icon = '') {
  455.         foreach ($coordtab as $coord) {
  456.             $this->addMarkerByAddress($coord[0], $coord[1], $coord[2], $category, $icon);
  457.         }
  458.     }
  459.  
  460.     /**
  461.      * Set a direction between 2 addresss and set a text panel
  462.      *
  463.      * @param string $from an address
  464.      * @param string $to   an address
  465.      *
  466.      * @return void
  467.      */
  468.     public function addDirection($from, $to) {
  469.         $this->contentMarker .= 'addDirection("' . $from . '","' . $to . '");';
  470.     }
  471.  
  472.     /**
  473.      * Parse a KML file and add markers to a category
  474.      *
  475.      * @param string $url      url of the kml file compatible with gmap and gearth
  476.      * @param string $category marker category
  477.      * @param string $icon     an icon url
  478.      *
  479.      * @return void
  480.      */
  481.     public function addKML($url, $category = '', $icon = '') {
  482.         $xml = new SimpleXMLElement($url, NULL, TRUE);
  483.         foreach ($xml->Document->Folder->Placemark as $item) {
  484.             $coordinates = explode(',', (string)$item->Point->coordinates);
  485.             $name = (string)$item->name;
  486.             $this->addMarkerByCoords($coordinates[1], $coordinates[0], $name, $name, $category, $icon);
  487.         }
  488.     }
  489.  
  490.     /**
  491.      * Initialize the javascript code
  492.      *
  493.      * @return void
  494.      */
  495.     public function init() {
  496.         // Google map DIV
  497.         if (($this->width != '') && ($this->height != '')) {
  498.             $this->content .= "\t" . '<div id="' . $this->googleMapId . '" style="width:' . $this->width . ';height:' . $this->height . '"></div>' . "\n";
  499.         } else {
  500.             $this->content .= "\t" . '<div id="' . $this->googleMapId . '"></div>' . "\n";
  501.         }
  502.  
  503.         if ($this->includeJs === TRUE) {
  504.             // Google map JS
  505.             $this->content .= '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=' . $this->lang . '">';
  506.             $this->content .= '</script>' . "\n";
  507.  
  508.             // Clusterer JS
  509.             if ($this->useClusterer == TRUE) {
  510.                 $this->content .= '<script src="' . $this->clustererLibraryPath . '" type="text/javascript"></script>' . "\n";
  511.             }
  512.         }
  513.  
  514.         $this->content .= '<script type="text/javascript">' . "\n";
  515.  
  516.  
  517.         $this->content .= 'function addLoadEvent(func) { ' . "\n";
  518.         $this->content .= 'var oldonload = window.onload; ' . "\n";
  519.         $this->content .= 'if (typeof window.onload != \'function\') { ' . "\n";
  520.         $this->content .= 'window.onload = func; ' . "\n";
  521.         $this->content .= '} else { ' . "\n";
  522.         $this->content .= 'window.onload = function() { ' . "\n";
  523.         $this->content .= 'if (oldonload) { ' . "\n";
  524.         $this->content .= ' oldonload(); ' . "\n";
  525.         $this->content .= '} ' . "\n";
  526.         $this->content .= ' func(); ' . "\n";
  527.         $this->content .= '} ' . "\n";
  528.         $this->content .= '}' . "\n";
  529.         $this->content .= '} ' . "\n";
  530.  
  531.         //global variables
  532.         $this->content .= 'var geocoder = new google.maps.Geocoder();' . "\n";
  533.         $this->content .= 'var map' . $this->googleMapId . ';' . "\n";
  534.         $this->content .= 'var gmarkers = [];' . "\n";
  535.         $this->content .= 'var infowindow;' . "\n";
  536.         $this->content .= 'var directions = new google.maps.DirectionsRenderer();' . "\n";
  537.         $this->content .= 'var directionsService = new google.maps.DirectionsService();' . "\n";
  538.         $this->content .= 'var current_lat = 0;' . "\n";
  539.         $this->content .= 'var current_lng = 0;' . "\n";
  540.  
  541.         // JS public function to get current Lat & Lng
  542.         $this->content .= "\t" . 'function getCurrentLat() {' . "\n";
  543.         $this->content .= "\t\t" . 'return current_lat;' . "\n";
  544.         $this->content .= "\t" . '}' . "\n";
  545.         $this->content .= "\t" . 'function getCurrentLng() {' . "\n";
  546.         $this->content .= "\t\t" . 'return current_lng;' . "\n";
  547.         $this->content .= "\t" . '}' . "\n";
  548.  
  549.         // JS public function to add a  marker
  550.         $this->content .= "\t" . 'function addMarker(latlng,title,content,category,icon,currentmap) {' . "\n";
  551.         $this->content .= "\t\t" . 'var marker = new google.maps.Marker({' . "\n";
  552.         $this->content .= "\t\t\t" . 'map:  currentmap,' . "\n";
  553.         $this->content .= "\t\t\t" . 'title : title,' . "\n";
  554.         $this->content .= "\t\t\t" . 'icon:  new google.maps.MarkerImage(icon, new google.maps.Size(' . $this->iconWidth . ', ' . $this->iconHeight . ')),' . "\n";
  555.         $this->content .= "\t\t\t" . 'position: latlng' . "\n";
  556.         $this->content .= "\t\t" . '});' . "\n";
  557.  
  558.         // Display direction inputs in the info window
  559.         if ($this->displayDirectionFields == TRUE) {
  560.             $this->content .= "\t\t" . 'content += \'<div style="clear:both;height:20px;"></div>\';' . "\n";
  561.             $this->content .= "\t\t" . 'id_name = \'marker_\'+gmarkers.length;' . "\n";
  562.             $this->content .= "\t\t" . 'content += \'<input type="text" id="\'+id_name+\'"/>\';' . "\n";
  563.             $this->content .= "\t\t" . 'var from = ""+latlng.lat()+","+latlng.lng();' . "\n";
  564.             $this->content .= "\t\t" . 'content += \'<br /><input type="button" onClick="addDirection(to.value,document.getElementById(\\\'\'+id_name+\'\\\').value);" value="Arrivée"/>\';' . "\n";
  565.             $this->content .= "\t\t" . 'content += \'<input type="button" onClick="addDirection(document.getElementById(\\\'\'+id_name+\'\\\').value,to.value);" value="Départ"/>\';' . "\n";
  566.         }
  567.  
  568.         $this->content .= "\t\t" . 'var html = \'<div style="text-align:left;width:' . $this->infoWindowWidth . 'px;" class="infoGmaps">\'+content+\'</div>\'' . "\n";
  569.         $this->content .= "\t\t" . 'google.maps.event.addListener(marker, "click", function() {' . "\n";
  570.         $this->content .= "\t\t\t" . 'if (infowindow) infowindow.close();' . "\n";
  571.         $this->content .= "\t\t\t" . 'infowindow = new google.maps.InfoWindow({content: html});' . "\n";
  572.         $this->content .= "\t\t\t" . 'infowindow.open(currentmap,marker);' . "\n";
  573.  
  574.         // Enable the zoom when you click on a marker
  575.         if ($this->enableWindowZoom == TRUE) {
  576.             $this->content .= "\t\t" . 'currentmap.setCenter(new google.maps.LatLng(latlng.lat(),latlng.lng()),' . $this->infoWindowZoom . ');' . "\n";
  577.         }
  578.  
  579.         $this->content .= "\t\t" . '});' . "\n";
  580.         $this->content .= "\t\t" . 'marker.mycategory = category;' . "\n";
  581.         $this->content .= "\t\t" . 'gmarkers.push(marker);' . "\n";
  582.  
  583.         // Hide marker by default
  584.         if ($this->defaultHideMarker == TRUE) {
  585.             $this->content .= "\t\t" . 'marker.setVisible(false);' . "\n";
  586.         }
  587.         $this->content .= "\t" . '}' . "\n";
  588.  
  589.         // JS public function to add a geocode marker
  590.         $this->content .= "\t" . 'function geocodeMarker(address,title,content,category,icon) {' . "\n";
  591.         $this->content .= "\t\t" . 'if (geocoder) {' . "\n";
  592.         $this->content .= "\t\t\t" . 'geocoder.geocode( { "address" : address}, function(results, status) {' . "\n";
  593.         $this->content .= "\t\t\t\t" . 'if (status == google.maps.GeocoderStatus.OK) {' . "\n";
  594.         $this->content .= "\t\t\t\t\t" . 'var latlng =  results[0].geometry.location;' . "\n";
  595.         $this->content .= "\t\t\t\t\t" . 'addMarker(results[0].geometry.location,title,content,category,icon)' . "\n";
  596.         $this->content .= "\t\t\t\t" . '}' . "\n";
  597.         $this->content .= "\t\t\t" . '});' . "\n";
  598.         $this->content .= "\t\t" . '}' . "\n";
  599.         $this->content .= "\t" . '}' . "\n";
  600.  
  601.         // JS public function to center the gmaps dynamically
  602.         $this->content .= "\t" . 'function geocodeCenter(address) {' . "\n";
  603.         $this->content .= "\t\t" . 'if (geocoder) {' . "\n";
  604.         $this->content .= "\t\t\t" . 'geocoder.geocode( { "address": address}, function(results, status) {' . "\n";
  605.         $this->content .= "\t\t\t\t" . 'if (status == google.maps.GeocoderStatus.OK) {' . "\n";
  606.         $this->content .= "\t\t\t\t" . 'map' . $this->googleMapId . '.setCenter(results[0].geometry.location);' . "\n";
  607.         $this->content .= "\t\t\t\t" . '} else {' . "\n";
  608.         $this->content .= "\t\t\t\t" . 'alert("Geocode was not successful for the following reason: " + status);' . "\n";
  609.         $this->content .= "\t\t\t\t" . '}' . "\n";
  610.         $this->content .= "\t\t\t" . '});' . "\n";
  611.         $this->content .= "\t\t" . '}' . "\n";
  612.         $this->content .= "\t" . '}' . "\n";
  613.  
  614.         // JS public function to set direction
  615.         $this->content .= "\t" . 'function addDirection(from,to) {' . "\n";
  616.         $this->content .= "\t\t" . 'var request = {' . "\n";
  617.         $this->content .= "\t\t" . 'origin:from, ' . "\n";
  618.         $this->content .= "\t\t" . 'destination:to,' . "\n";
  619.         $this->content .= "\t\t" . 'travelMode: google.maps.DirectionsTravelMode.DRIVING' . "\n";
  620.         $this->content .= "\t\t" . '};' . "\n";
  621.         $this->content .= "\t\t" . 'directionsService.route(request, function(response, status) {' . "\n";
  622.         $this->content .= "\t\t" . 'if (status == google.maps.DirectionsStatus.OK) {' . "\n";
  623.         $this->content .= "\t\t" . 'directions.setDirections(response);' . "\n";
  624.         $this->content .= "\t\t" . '}' . "\n";
  625.         $this->content .= "\t\t" . '});' . "\n";
  626.  
  627.         $this->content .= "\t\t" . 'if(infowindow) { infowindow.close(); }' . "\n";
  628.         $this->content .= "\t" . '}' . "\n";
  629.  
  630.         // JS public function to show a category of marker
  631.         $this->content .= "\t" . 'function showCategory(category) {' . "\n";
  632.         $this->content .= "\t\t" . 'for (var i=0; i<gmarkers.length; i++) {' . "\n";
  633.         $this->content .= "\t\t\t" . 'if (gmarkers[i].mycategory == category) {' . "\n";
  634.         $this->content .= "\t\t\t\t" . 'gmarkers[i].setVisible(true);' . "\n";
  635.         $this->content .= "\t\t\t" . '}' . "\n";
  636.         $this->content .= "\t\t" . '}' . "\n";
  637.         $this->content .= "\t" . '}' . "\n";
  638.  
  639.         // JS public function to hide a category of marker
  640.         $this->content .= "\t" . 'function hideCategory(category) {' . "\n";
  641.         $this->content .= "\t\t" . 'for (var i=0; i<gmarkers.length; i++) {' . "\n";
  642.         $this->content .= "\t\t\t" . 'if (gmarkers[i].mycategory == category) {' . "\n";
  643.         $this->content .= "\t\t\t\t" . 'gmarkers[i].setVisible(false);' . "\n";
  644.         $this->content .= "\t\t\t" . '}' . "\n";
  645.         $this->content .= "\t\t" . '}' . "\n";
  646.         $this->content .= "\t\t" . 'if(infowindow) { infowindow.close(); }' . "\n";
  647.         $this->content .= "\t" . '}' . "\n";
  648.  
  649.         // JS public function to hide all the markers
  650.         $this->content .= "\t" . 'function hideAll() {' . "\n";
  651.         $this->content .= "\t\t" . 'for (var i=0; i<gmarkers.length; i++) {' . "\n";
  652.         $this->content .= "\t\t\t" . 'gmarkers[i].setVisible(false);' . "\n";
  653.         $this->content .= "\t\t" . '}' . "\n";
  654.         $this->content .= "\t\t" . 'if(infowindow) { infowindow.close(); }' . "\n";
  655.         $this->content .= "\t" . '}' . "\n";
  656.  
  657.         // JS public function to show all the markers
  658.         $this->content .= "\t" . 'function showAll() {' . "\n";
  659.         $this->content .= "\t\t" . 'for (var i=0; i<gmarkers.length; i++) {' . "\n";
  660.         $this->content .= "\t\t\t" . 'gmarkers[i].setVisible(true);' . "\n";
  661.         $this->content .= "\t\t" . '}' . "\n";
  662.         $this->content .= "\t\t" . 'if(infowindow) { infowindow.close(); }' . "\n";
  663.         $this->content .= "\t" . '}' . "\n";
  664.  
  665.         // JS public function to hide/show a category of marker - TODO BUG
  666.         $this->content .= "\t" . 'function toggleHideShow(category) {' . "\n";
  667.         $this->content .= "\t\t" . 'for (var i=0; i<gmarkers.length; i++) {' . "\n";
  668.         $this->content .= "\t\t\t" . 'if (gmarkers[i].mycategory === category) {' . "\n";
  669.         $this->content .= "\t\t\t\t" . 'if (gmarkers[i].getVisible()===true) { gmarkers[i].setVisible(false); }' . "\n";
  670.         $this->content .= "\t\t\t\t" . 'else gmarkers[i].setVisible(true);' . "\n";
  671.         $this->content .= "\t\t\t" . '}' . "\n";
  672.         $this->content .= "\t\t" . '}' . "\n";
  673.         $this->content .= "\t\t" . 'if(infowindow) { infowindow.close(); }' . "\n";
  674.         $this->content .= "\t" . '}' . "\n";
  675.  
  676.         // JS public function add a KML
  677.         $this->content .= "\t" . 'function addKML(file) {' . "\n";
  678.         $this->content .= "\t\t" . 'var ctaLayer = new google.maps.KmlLayer(file);' . "\n";
  679.         $this->content .= "\t\t" . 'ctaLayer.setMap(map' . $this->googleMapId . ');' . "\n";
  680.         $this->content .= "\t" . '}' . "\n";
  681.     }
  682.  
  683.     /**
  684.      * Generate the HTML code of the gmap
  685.      */
  686.     public function generate() {
  687.         $this->init();
  688.  
  689.         //Fonction init()
  690.         $this->content .= "\t" . 'function initialize' . $this->googleMapId . '() {' . "\n";
  691.         $this->content .= "\t" . 'var myLatlng = new google.maps.LatLng(48.8792,2.34778);' . "\n";
  692.         $this->content .= "\t" . 'var myOptions = {' . "\n";
  693.         $this->content .= "\t\t" . 'zoom: ' . $this->zoom . ',' . "\n";
  694.         $this->content .= "\t\t" . 'center: myLatlng,' . "\n";
  695.         $this->content .= "\t\t" . 'mapTypeId: google.maps.MapTypeId.' . $this->mapType . "\n";
  696.         $this->content .= "\t" . '}' . "\n";
  697.  
  698.         //Goole map Div Id
  699.         $this->content .= "\t" . 'map' . $this->googleMapId . ' = new google.maps.Map(document.getElementById("' . $this->googleMapId . '"), myOptions);' . "\n";
  700.  
  701.         // Center
  702.         if ($this->enableAutomaticCenterZoom == TRUE) {
  703.             $lenLng = $this->maxLng - $this->minLng;
  704.             $lenLat = $this->maxLat - $this->minLat;
  705.             $this->minLng -= $lenLng * $this->coordCoef;
  706.             $this->maxLng += $lenLng * $this->coordCoef;
  707.             $this->minLat -= $lenLat * $this->coordCoef;
  708.             $this->maxLat += $lenLat * $this->coordCoef;
  709.  
  710.             $minLat = number_format(floatval($this->minLat), 12, '.', '');
  711.             $minLng = number_format(floatval($this->minLng), 12, '.', '');
  712.             $maxLat = number_format(floatval($this->maxLat), 12, '.', '');
  713.             $maxLng = number_format(floatval($this->maxLng), 12, '.', '');
  714.             $this->content .= "\t\t\t" . 'var bds = new google.maps.LatLngBounds(new google.maps.LatLng(' . $minLat . ',' . $minLng . '),new google.maps.LatLng(' . $maxLat . ',' . $maxLng . '));' . "\n";
  715.             $this->content .= "\t\t\t" . 'map' . $this->googleMapId . '.fitBounds(bds);' . "\n";
  716.         } else {
  717.             if (isset($this->centerLatLng)) {
  718.                 $this->content .= "\t" . 'map' . $this->googleMapId . '.setCenter(' . $this->centerLatLng . ');' . "\n";
  719.             } else {
  720.                 $this->content .= "\t" . 'geocodeCenter("' . $this->center . '");' . "\n";
  721.             }
  722.         }
  723.  
  724.         $this->content .= "\t" . 'google.maps.event.addListener(map' . $this->googleMapId . ',"click",function(event) { if (event) { current_lat=event.latLng.lat();current_lng=event.latLng.lng(); }}) ;' . "\n";
  725.  
  726.         $this->content .= "\t" . 'directions.setMap(map' . $this->googleMapId . ');' . "\n";
  727.         $this->content .= "\t" . 'directions.setPanel(document.getElementById("' . $this->googleMapDirectionId . '"))' . "\n";
  728.  
  729.         // add all the markers
  730.         $this->content .= $this->contentMarker;
  731.  
  732.         // Clusterer JS
  733.         if ($this->useClusterer == TRUE) {
  734.             $this->content .= "\t" . 'var markerCluster = new MarkerClusterer(map' . $this->googleMapId . ', gmarkers,{gridSize: ' . $this->gridSize . ', maxZoom: ' . $this->maxZoom . '});' . "\n";
  735.         }
  736.  
  737.         $this->content .= '}' . "\n";
  738.  
  739.         // Chargement de la map a la fin du HTML
  740.         //$this->content.= "\t".'window.onload=initialize;'."\n";
  741.         $this->content .= "\t" . 'addLoadEvent(initialize' . $this->googleMapId . ');' . "\n";
  742.  
  743.         //Fermeture du javascript
  744.         $this->content .= '</script>' . "\n";
  745.  
  746.     }
  747.  
  748. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement