gmap.inc.php 6.05 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
<?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.
 */
23

Feu's avatar
Feu committed
24 25 26
/**
 * @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
  function gmap ( $name )
  {
    $this->name = $name;
51

Ayolo's avatar
Ayolo committed
52
  }
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
  }
58

Feu's avatar
Feu committed
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 );
72 73
  }

Feu's avatar
Feu committed
74 75 76 77
  function add_path ( $name, $latlongs, $color="ff0000" )
  {
    $this->paths[] = array("name"=>$name,"latlongs"=>$latlongs, "color"=>$color );
  }
78

Feu's avatar
Feu committed
79 80 81 82
  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
    $this->add_path($name,$latlongs, $color);
85
  }
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
    $this->buffer .= "<div id=\"".$this->name."_canvas\" style=\"width: 500px; height: 300px\"></div>";
91 92


Feu's avatar
Feu committed
93
    $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
    {
      foreach ( $this->markers as $marker )
        $this->buffer .= "var ".$marker["name"].";\n";

      foreach ( $this->paths as $path )
        $this->buffer .= "var ".$path["name"].";\n";
108
    }
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

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
      $first = true;
117

Ayolo's avatar
gmap  
Ayolo committed
118 119 120
      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";
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;
        }
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";
137

Ayolo's avatar
gmap  
Ayolo committed
138
        $this->buffer .= $this->name.".addOverlay(".$marker["name"].");\n";
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 157
    ";
        if($first)
	{
Ayolo's avatar
gmap  
Ayolo committed
158
          $this->buffer.="    ".$this->name.".setCenter(point,12);";
Ayolo's avatar
gmap  
Ayolo committed
159 160 161
          $first=false;
        }
        $this->buffer.="
Ayolo's avatar
gmap  
Ayolo committed
162
    ".$this->name.".addOverlay(new google.maps.Marker(point));
Ayolo's avatar
gmap  
Ayolo committed
163 164 165
  }
}
);\n";
Ayolo's avatar
bleh  
Ayolo committed
166
      }
Ayolo's avatar
fix  
Ayolo committed
167

Ayolo's avatar
gmap  
Ayolo committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      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
190
      $this->buffer .= 'var '.$this->pays->nom."= new google.maps.ClientGeocoder();\n";
Ayolo's avatar
gmap  
Ayolo committed
191
      $this->buffer .= $this->pays->nom.".getLatLng(\"".$this->pays->nom."\",
Ayolo's avatar
gmap  
Ayolo committed
192 193 194 195 196 197
function(point)
{
  if(!point)
    return;
  else
  {
Ayolo's avatar
gmap  
Ayolo committed
198
    ".$this->name.".setCenter(point,5);
Ayolo's avatar
gmap  
Ayolo committed
199 200 201 202
    ".$this->name.".addOverlay(new google.maps.Marker(point));
  }
}
);\n";
Feu's avatar
Feu committed
203
    }
Feu's avatar
Feu committed
204

Ayolo's avatar
fix  
Ayolo committed
205
    $this->buffer .= $this->name.".addControl(new google.maps.SmallMapControl());\n";
Feu's avatar
Feu committed
206
    $this->buffer .= $this->name.".addControl(new google.maps.MapTypeControl());\n";
Ayolo's avatar
fix  
Ayolo committed
207 208

    $this->buffer .= "
Feu's avatar
Feu committed
209
    }
210

Feu's avatar
fix?  
Feu committed
211
    google.setOnLoadCallback(initialize);
Feu's avatar
Feu committed
212
    document.onunload=GUnload;
213 214 215 216

    </script>";


Feu's avatar
Feu committed
217 218 219 220 221 222 223 224
    return $this->buffer;
  }


}



Ayolo's avatar
Ayolo committed
225
?>