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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/**
* Date : 24.08.2009
* But : Classe pour utiliser Google Map v3
*
*
* Paramètres: mapID: ID de la map dans le code HTML
* key: Clé google
* lat: Latitude
* lng: Longitude
* zoom: Niveau de zoom (0-19)
*/
function GMap(mapID, key, lat, lng, zoom) {
/**
* Variables de classes
*/
this.gmap;
this.request = null;
this.selectedMarker = null;
this.tabMarker = new Array();
this.geocoder = new google.maps.Geocoder();
/**
* Initialisation de l'objet
*/
var options = {
key: key,
zoom: zoom,
center: new google.maps.LatLng(lat, lng),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.gmap = new google.maps.Map(document.getElementById(mapID), options);
/**
* Méthode pour ajouter un écouteur sur la map
* Event name : click
* zoom_changed
* drag / dragstart / dragend
* bounds_changed
*
*
* return: MapsEventListener
*/
this.addMapListener = function(eventName, fonction) {
return google.maps.event.addListener(this.gmap, eventName, fonction);
}
/**
* Méthode pour ajouter un écouteur sur le marqueur
* return: MapsEventListener
*/
this.addMarkerListener = function(eventName, marker, fonction) {
return google.maps.event.addListener(marker, eventName, fonction);
}
/**
* Méthode pour supprimer un écouteur sur la map ou sur le marqueur
* return: none
*/
this.removeListener = function(mapsEventListener) {
return google.maps.event.removeListener(mapsEventListener);
}
/**
* Méthode pour savoir si la map est initialisée
* return: boolean
*/
this.isInit = function() {
return this.gmap.get_bounds() != null;
}
/**
* Méthode pour déplacer la carte
* return: none
*/
this.moveTo = function(lat, lng) {
this.gmap.panTo(new google.maps.LatLng(lat, lng));
}
/**
* Méthode pour attribuer le centre de la carte
* return: none
*/
this.setCenter = function(lat, lng) {
this.gmap.set_center(new google.maps.LatLng(lat, lng));
}
/**
* Méthode pour retourner le centre de la carte
* return: google.maps.LatLng
*/
this.getCenter = function() {
return this.gmap.get_center();
}
/**
* Méthode pour retourner le Nord-Est de la carte
* return: google.maps.LatLng
*/
this.getNorthEast = function() {
return this.gmap.get_bounds().getNorthEast();
}
/**
* Méthode pour retourner le Sud-Ouest de la carte
* return: google.maps.LatLng
*/
this.getSouthWest = function() {
return this.gmap.get_bounds().getSouthWest();
}
/**
* Méthode pour retourner le niveau du zoom de la carte
* return: int
*/
this.getZoom = function() {
return this.gmap.get_zoom();
}
/**
* Méthode pour attribuer le niveau du zoom de la carte
* return: none
*/
this.setZoom = function(zoom) {
this.gmap.set_zoom(zoom);
}
/**
* Méthode pour récupérer un marqueur
* return: google.maps.Marker
*/
this.getMarkerById = function(id) {
return this.tabMarker[id];
}
/**
* Méthode pour effacer les marqueurs
* return: none
*/
this.clearMarkers = function() {
for (var i in this.tabMarker) {
this.tabMarker[i].set_map(null);
delete (this.tabMarker[i]);
}
this.tabMarker.length = 0;
}
/**
* Méthode pour effacer les marqueurs en dehors de la vue actuelle
* return: none
*/
this.clearMarkersOutOfView = function() {
bounds = this.gmap.get_bounds();
for (var i in this.tabMarker) {
marker = this.tabMarker[i];
latLng = marker.get_position();
if (!bounds.contains(latLng)) {
this.tabMarker[i].set_map(null);
delete (this.tabMarker[i]);
}
}
}
/**
* Méthode pour déplacer le centre de la carte à l'adresse reçu en paramètre
* return: none
*/
this.moveCenterToAddress = function(address) {
var mymap = this.gmap;
this.geocoder.geocode({ 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
mymap.set_center(results[0].geometry.location);
type = results[0].geometry.location_type;
zoom = 13;
if (type == google.maps.GeocoderLocationType.ROOFTOP) {
zoom = 17;
}
else if (type == google.maps.GeocoderLocationType.INTERPOLATED) {
zoom = 17;
}
else zoom = 13;
//mymap.set_zoom(zoom);
}
//else alert("Erreur : " + status);
});
}
/**
* Méthode pour créer et insérer un marqueur sur la carte
* return: google.maps.Marker
*/
this.addMarker = function(lat, lng, id, title, img) {
// Création du marqueur
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: this.gmap,
title: title,
icon: img
});
// Efface l'ancien marker (s'il y en a un)
oldMarker = this.getMarkerById(id);
if (oldMarker != null) oldMarker.set_map(null);
// Sauvegarde du marqueur
this.tabMarker[id] = marker;
// Retourne le marqueur
return marker;
}
/**
* Annule la requête ajax en cours
* return: none
*/
this.cancelLoad = function() {
if (this.request != null) this.request.abort();
this.request = null;
}
/**
* Méthode pour charger des marqueurs provenant d'un fichier XML (Les coordonnées de la carte sont envoyées automatiquement en paramètre d'url: neLat, neLng, swLat, swLng)
*
* Format du fichier XML :
*
* <markers>
* <marker id="5434" lat="47.4910" lng="10.7690" title="Bistro 1"></marker>
* <marker id="7654" lat="46.8870" lng="10.1728" title="Bistro 2"></marker>
* <marker id="9832" lat="47.0354" lng="10.1823" title="Bistro 3"></marker>
* </markers>
*
*
* return: none
*/
this.loadFileMarker = function(xmlFile, params, fonctionEventClick) {
//window.document.getElementById("loading").style.display = "block";
var neLat = this.getNorthEast().lat();
var neLng = this.getNorthEast().lng();
var swLat = this.getSouthWest().lat();
var swLng = this.getSouthWest().lng();
var data = params + "&" +
"neLat=" + neLat + "&" +
"neLng=" + neLng + "&" +
"swLat=" + swLat + "&" +
"swLng=" + swLng;
this.clearMarkersOutOfView();
var mymap = this;
this.request = $.ajax({
type: "GET",
url: xmlFile,
data: data,
dataType: "xml",
success: function(xml) {
$(xml).find('marker').each(function() {
var id = $(this).attr('id');
var lat = $(this).attr('lat');
var lng = $(this).attr('lng');
var title = $(this).attr('title');
if (mymap.getMarkerById(id) == null) {
marker = mymap.addMarker(lat, lng, id, title, "Img/Marker.png");
if (fonctionEventClick != null) {
mymap.addMarkerListener("click", marker, function() {
fonctionEventClick(id);
});
}
}
});
},
error: function(result) {
alert("Error" + result);
},
complete: function(XMLHttpRequest, textStatus) {
//alert(textStatus);
}
});
}
} |
Partager