gmap.inc.php 5.87 KB
Newer Older
Feu's avatar
Feu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?php
/* Copyright 2007
 * - Julien Etelain < julien at pmad dot net >
 *
 * Ce fichier fait partie du site de l'Association des Étudiants de
 * l'UTBM, http://ae.utbm.fr.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */
 
/**
 * @file
 */
Ayolo's avatar
bleh  
Ayolo committed
27 28 29

require_once($topdir . "include/entities/pays.inc.php");
require_once($topdir . "include/entities/ville.inc.php");
30 31 32 33 34 35
/**
 * Permet d'afficher un carte (de goolge maps).
 *
 * @author Julien Etelain
 * @ingroup display_cts
 */
Feu's avatar
Feu committed
36 37
class gmap extends stdcontents
{
Ayolo's avatar
Ayolo committed
38
  var $name;
Feu's avatar
Feu committed
39

Ayolo's avatar
Ayolo committed
40
  /* google map api key */
Ayolo's avatar
Ayolo committed
41
  var $key = "__GMAP_KEY__";
Feu's avatar
Feu committed
42

Feu's avatar
Feu committed
43
  var $markers = array();
Ayolo's avatar
gmap  
Ayolo committed
44 45 46
  var $paths   = array();
  var $ville   = array();
  var $pays    = null;
Feu's avatar
Feu committed
47

Ayolo's avatar
Ayolo committed
48 49 50 51 52
  function gmap ( $name )
  {
    $this->name = $name;
    
  }
Feu's avatar
Feu committed
53

Feu's avatar
Feu committed
54
  function add_marker ( $name, $lat, $long, $draggable=false, $dragend=null )
Feu's avatar
Feu committed
55
  {
Feu's avatar
Feu committed
56
    $this->markers[] = array("name"=>$name,"lat"=>$lat, "long"=>$long, "draggable"=>$draggable, "dragend"=>$dragend );
Feu's avatar
Feu committed
57
  }
Feu's avatar
Feu committed
58 59 60
  
  function add_geopoint ( &$g )
  {
Ayolo's avatar
gmap  
Ayolo committed
61 62
    if( $g instanceof ville)
    {
Ayolo's avatar
gmap  
Ayolo committed
63
      global $site;
Ayolo's avatar
gmap  
Ayolo committed
64 65
      $pays = new pays($site->db);
      $pays->load_by_id($g->id_pays);
Ayolo's avatar
gmap  
Ayolo committed
66
      $this->ville[] = &$g;
Ayolo's avatar
gmap  
Ayolo committed
67 68
    }
    elseif( $g instanceof pays)
Ayolo's avatar
gmap  
Ayolo committed
69
      $this->pays=&$g;
Ayolo's avatar
gmap  
Ayolo committed
70 71
    else
      $this->add_marker($g->nom,$g->lat,$g->long );
Feu's avatar
Feu committed
72 73 74 75 76 77 78 79 80 81 82
  }  
  
  function add_path ( $name, $latlongs, $color="ff0000" )
  {
    $this->paths[] = array("name"=>$name,"latlongs"=>$latlongs, "color"=>$color );
  }
  
  function add_geopoint_path ( $name, $geopoints, $color="ff0000" )
  {
    $latlongs=array();
    foreach ($geopoints as $g)
Ayolo's avatar
bleh  
Ayolo committed
83
      $latlongs[]=$g;
Feu's avatar
Feu committed
84 85
    $this->add_path($name,$latlongs, $color);
  }  
Feu's avatar
Feu committed
86 87 88

  function html_render()
  {
Ayolo's avatar
bleh  
Ayolo committed
89
    global $site;
Feu's avatar
Feu committed
90 91 92 93
    $this->buffer .= "<div id=\"".$this->name."_canvas\" style=\"width: 500px; height: 300px\"></div>";
    
    
    $this->buffer .= "
Ayolo's avatar
fix  
Ayolo committed
94
    <script src=\"http://www.google.com/jsapi?key=".$this->key."\" type=\"text/javascript\"></script>
Feu's avatar
Feu committed
95
    <script type=\"text/javascript\">\n";
Ayolo's avatar
gmap  
Ayolo committed
96

Feu's avatar
fix?  
Feu committed
97
    //
Feu's avatar
fix?  
Feu committed
98
    $this->buffer .="google.load(\"maps\", \"2\");\n";
Feu's avatar
Feu committed
99 100
    $this->buffer .="var ".$this->name.";\n";

Ayolo's avatar
gmap  
Ayolo committed
101
    if(is_null($this->pays))
Ayolo's avatar
gmap  
Ayolo committed
102 103 104 105 106 107 108
    {
      foreach ( $this->markers as $marker )
        $this->buffer .= "var ".$marker["name"].";\n";

      foreach ( $this->paths as $path )
        $this->buffer .= "var ".$path["name"].";\n";
    } 
Ayolo's avatar
fix  
Ayolo committed
109 110

    $this->buffer .="function initialize() {\n";
Feu's avatar
fix?  
Feu committed
111
    $this->buffer .= $this->name." = new google.maps.Map2(document.getElementById(\"".$this->name."_canvas\"));\n";
Ayolo's avatar
gmap  
Ayolo committed
112

Feu's avatar
Feu committed
113
    
Ayolo's avatar
gmap  
Ayolo committed
114
    if(is_null($this->pays))
Feu's avatar
Feu committed
115
    {
Ayolo's avatar
gmap  
Ayolo committed
116 117 118 119 120
      $first = true;
    
      foreach ( $this->markers as $marker )
      {
        $this->buffer .= "var ".$marker["name"]."_point = new google.maps.LatLng(".sprintf("%.12F",$marker['lat']*360/2/M_PI).", ".sprintf("%.12F",$marker['long']*360/2/M_PI).");\n";
Feu's avatar
Feu committed
121 122
      
      
Ayolo's avatar
gmap  
Ayolo committed
123 124 125 126 127
        if ( $first )
        {
          $this->buffer .= $this->name.".setCenter(".$marker["name"]."_point, 15);\n";
          $first = false;
        }
Feu's avatar
Feu committed
128
      
Ayolo's avatar
gmap  
Ayolo committed
129 130 131 132 133 134 135 136
        if ( $marker["draggable"] )
        {
          $this->buffer .= "var ".$marker["name"]." = new google.maps.Marker(".$marker["name"]."_point, {draggable: true});\n";
          if ( !is_null($marker["dragend"]) )
            $this->buffer .= "google.maps.Event.addListener(marker, \"dragend\", ".$marker["dragend"]." );\n";
        }
        else
          $this->buffer .= $marker["name"]."= new google.maps.Marker(".$marker["name"]."_point);\n";
Ayolo's avatar
fix  
Ayolo committed
137
      
Ayolo's avatar
gmap  
Ayolo committed
138
        $this->buffer .= $this->name.".addOverlay(".$marker["name"].");\n";
Feu's avatar
Feu committed
139
      
Ayolo's avatar
gmap  
Ayolo committed
140
      }
Feu's avatar
Feu committed
141

Ayolo's avatar
gmap  
Ayolo committed
142
      foreach($this->ville as $ville)
Ayolo's avatar
bleh  
Ayolo committed
143
      {
Ayolo's avatar
gmap  
Ayolo committed
144 145
        $pays = new pays($site->db);
	$pays->load_by_id($ville->id_pays);
Ayolo's avatar
gmap  
Ayolo committed
146
        $this->buffer .= "var ".$ville->nom."_dec = new google.maps.ClientGeocoder();\n";
Ayolo's avatar
gmap  
Ayolo committed
147
	$this->buffer .= "
Ayolo's avatar
gmap  
Ayolo committed
148
".$ville->nom."_dec.getLatLng(\"".$ville->nom.", ".$ville->cpostal.", ".$pays->nom."\",
Ayolo's avatar
gmap  
Ayolo committed
149 150
function(point)
{
Ayolo's avatar
gmap  
Ayolo committed
151
  if(!point)
Ayolo's avatar
gmap  
Ayolo committed
152
    return;
Ayolo's avatar
gmap  
Ayolo committed
153 154
  else
  {
Ayolo's avatar
gmap  
Ayolo committed
155 156
    ".$this->name.".setCenter(point,16);
    ".$this->name.".addOverlay(new google.maps.Marker(point));
Ayolo's avatar
gmap  
Ayolo committed
157 158 159
  }
}
);\n";
Ayolo's avatar
bleh  
Ayolo committed
160
      }
Ayolo's avatar
fix  
Ayolo committed
161

Ayolo's avatar
gmap  
Ayolo committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      foreach ( $this->paths as $path )
      {
        $points=array();
        foreach( $path["latlongs"] as $point )
        {
          if($point instanceof ville)
          {
            $pays = new pays($site->db);
            $pays->load_by_id($point->id_pays);
            $points[] = $point->nom.", ".$point->cpostal.", ".$pays->nom;
          }
          else
            $points[] = "@".sprintf("%.12F",$point['lat']*360/2/M_PI).", ".sprintf("%.12F",$point['long']*360/2/M_PI);
        }

        $this->buffer .= "var ".$path["name"]."points = \"from: ".implode(" to: ",$points)."\";\n";
        $this->buffer .= $path["name"]."= new google.maps.Directions(map);\n";
        $this->buffer .= $path["name"].".load(".$path["name"]."points, {getSteps:true});\n";
      }
    }
    else
    {
Ayolo's avatar
gmap  
Ayolo committed
184
      $this->buffer .= 'var '.$this->pays->nom."= new google.maps.Directions(map);\n";
Ayolo's avatar
gmap  
Ayolo committed
185
      $this->buffer .= $this->pays->nom.".load(\"from: ".$this->pays->nom."\", {getSteps:true});\n";
Feu's avatar
Feu committed
186
    }
Feu's avatar
Feu committed
187

Ayolo's avatar
fix  
Ayolo committed
188
    $this->buffer .= $this->name.".addControl(new google.maps.SmallMapControl());\n";
Feu's avatar
Feu committed
189
    $this->buffer .= $this->name.".addControl(new google.maps.MapTypeControl());\n";
Ayolo's avatar
fix  
Ayolo committed
190 191

    $this->buffer .= "
Feu's avatar
Feu committed
192 193
    }
    
Feu's avatar
fix?  
Feu committed
194
    google.setOnLoadCallback(initialize);
Feu's avatar
Feu committed
195
    document.onunload=GUnload;
Ayolo's avatar
fix  
Ayolo committed
196
    
Feu's avatar
Feu committed
197 198 199 200 201 202 203 204 205 206 207
    </script>";  
    
    
    return $this->buffer;
  }


}



Ayolo's avatar
Ayolo committed
208
?>