Subversion Repositories eFlore/Applications.coel

Rev

Rev 1827 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1827 Rev 1834
1
var map = null,
1
var map = null,
2
optionsCoucheOSM = {
2
optionsCoucheOSM = {
3
	attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors,'
3
	attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors,'
4
	+ ' <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
4
	+ ' <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
5
	maxZoom: 18
5
	maxZoom: 18
6
},
6
},
7
optionsCoucheGoogle = {
7
optionsCoucheGoogle = {
8
	attribution: 'Map data &copy;'+new Date().getFullYear()+' <a href="http://maps.google.com">Google</a>',
8
	attribution: 'Map data &copy;'+new Date().getFullYear()+' <a href="http://maps.google.com">Google</a>',
9
	maxZoom: 18
9
	maxZoom: 18
10
},
10
},
11
coucheOSM = new L.TileLayer("http://osm.tela-botanica.org/tuiles/osmfr/{z}/{x}/{y}.png",
11
coucheOSM = new L.TileLayer("http://osm.tela-botanica.org/tuiles/osmfr/{z}/{x}/{y}.png",
12
		optionsCoucheOSM),
12
		optionsCoucheOSM),
13
coucheSatellite = new L.TileLayer("http://mt1.google.com/vt/lyrs=y@218131653&hl=fr&src=app&x={x}&y={y}&z={z}",
13
coucheSatellite = new L.TileLayer("http://mt1.google.com/vt/lyrs=y@218131653&hl=fr&src=app&x={x}&y={y}&z={z}",
14
	optionsCoucheGoogle),
14
	optionsCoucheGoogle),
15
optionsCarte = {
15
optionsCarte = {
16
	center : new L.LatLng(46, 2),
16
	center : new L.LatLng(46, 2),
17
	zoom : 6,
17
	zoom : 6,
18
	layers : [coucheOSM]
18
	layers : [coucheOSM]
19
};
19
};
20
 
20
 
21
var xmlHttpRequest = null,
21
var xmlHttpRequest = null;
22
	nombreCollections = 0,
22
	nombreCollections = 0;
23
	collections = new Array(),
23
	collections = new Array();
-
 
24
	structures = new Array();
24
	structures = new Array(),
25
	if(clustering) {
25
	coucheStructures = new L.MarkerClusterGroup({
26
		coucheStructures = new L.MarkerClusterGroup({
26
		disableClusteringAtZoom : 10
27
			disableClusteringAtZoom : 10
-
 
28
		});
-
 
29
	} else {
-
 
30
		coucheStructures = new L.FeatureGroup();
27
	}),
31
	}
28
	infoBulle = null,
32
	infoBulle = null;
29
	chargementEnCours = false;
33
	chargementEnCours = false;
30
 
34
 
31
 
35
 
32
$(document).ready(function() {
36
$(document).ready(function() {
33
	dimensionnerCarte();
37
	dimensionnerCarte();
34
	initialiserCarte();
38
	initialiserCarte();
35
	initialiserPanneauControle();
39
	initialiserPanneauControle();
36
 
40
 
37
	$.ajax({
41
	$.ajax({
38
		dataType: "json",
42
		dataType: "json",
39
		url: urlWebService + "CoelRecherche/Nombre/*/*/*/*/*/*/*/" + departement + "/*/",
43
		url: urlWebService + "CoelRecherche/Nombre/*/*/*/*/*/*/*/" + departement + "/*/",
40
		data: { formatRetour: "text/plain", pays: pays},
44
		data: { formatRetour: "text/plain", pays: pays},
41
		async: false
45
		async: false
42
	}).complete(function(msg) {
46
	}).complete(function(msg) {
43
		if (! estStatutRequeteOK(msg.status)) {
47
		if (! estStatutRequeteOK(msg.status)) {
44
			alert(msg.responseText);
48
			alert(msg.responseText);
45
			return;
49
			return;
46
		}
50
		}
47
		nombreCollections = parseInt(msg.responseText);
51
		nombreCollections = parseInt(msg.responseText);
48
	});
52
	});
49
	
53
	
50
	chargerStructures();
54
	chargerStructures();
51
});
55
});
52
 
56
 
53
$(window).resize(function() {
57
$(window).resize(function() {
54
	dimensionnerCarte();
58
	dimensionnerCarte();
55
});
59
});
56
 
60
 
57
function dimensionnerCarte() {
61
function dimensionnerCarte() {
58
	$("#map").width($(window).width());
62
	$("#map").width($(window).width());
59
	$("#map").height($(window).height());
63
	$("#map").height($(window).height());
60
}
64
}
61
 
65
 
62
function initialiserCarte() {
66
function initialiserCarte() {
63
	map = L.map('map', optionsCarte);
67
	map = L.map('map', optionsCarte);
64
	coucheOSM.addTo(map);
68
	coucheOSM.addTo(map);
65
	coucheStructures.addTo(map);
69
	coucheStructures.addTo(map);
66
	map.on('zoomend', function() {
70
	map.on('zoomend', function() {
67
		// controle sur le niveau de zoom uniquement a la fin du placement des structures sur la carte 
71
		// controle sur le niveau de zoom uniquement a la fin du placement des structures sur la carte 
68
		if (chargementEnCours) {
72
		if (chargementEnCours) {
69
			chargementEnCours = false;
73
			chargementEnCours = false;
70
			verifierZoom();
74
			verifierZoom();
71
		}
75
		}
72
	});
76
	});
73
}
77
}
74
 
78
 
75
function initialiserPanneauControle() {
79
function initialiserPanneauControle() {
76
	var baseMaps = {
80
	var baseMaps = {
77
		"Plan" : coucheOSM,
81
		"Plan" : coucheOSM,
78
		"Satellite" : coucheSatellite
82
		"Satellite" : coucheSatellite
79
	};
83
	};
80
	var overlayMaps = {
84
	var overlayMaps = {
81
		"Structures" : coucheStructures	
85
		"Structures" : coucheStructures	
82
	};
86
	};
83
	L.control.layers(baseMaps, overlayMaps).addTo(map);
87
	L.control.layers(baseMaps, overlayMaps).addTo(map);
84
}
88
}
85
 
89
 
86
 
90
 
87
function recupererValeurNombreCollections() {
91
function recupererValeurNombreCollections() {
88
}
92
}
89
 
93
 
90
function chargerStructures() {
94
function chargerStructures() {
91
	if (requeteEnCours()) {
95
	if (requeteEnCours()) {
92
		window.setTimeout('chargerStructures()', 400);
96
		window.setTimeout('chargerStructures()', 400);
93
		return;
97
		return;
94
	}
98
	}
95
 
99
 
96
	chargementEnCours = true;
100
	chargementEnCours = true;
97
	$.ajax({
101
	$.ajax({
98
		dataType: "json",
102
		dataType: "json",
99
		url: urlWebService + "CoelRecherche/ParDefaut/*/*/*/*/*/*/*/" + departement + "/*/",
103
		url: urlWebService + "CoelRecherche/ParDefaut/*/*/*/*/*/*/*/" + departement + "/*/",
100
		data: { limit: nombreCollections, pays: pays},
104
		data: { limit: nombreCollections, pays: pays},
101
		async: true
105
		async: true
102
	}).complete(function(msg) {
106
	}).complete(function(msg) {
103
		if (!estStatutRequeteOK(msg.status)) {
107
		if (!estStatutRequeteOK(msg.status)) {
104
			alert(msg.responseText);
108
			alert(msg.responseText);
105
			return;
109
			return;
106
		}
110
		}
107
		collections = eval("(function(){return " + msg.responseText + ";})()");
111
		collections = eval("(function(){return " + msg.responseText + ";})()");
108
		ordonnerCollectionsParStructures();
112
		ordonnerCollectionsParStructures();
109
		chargerLocalisations();
113
		chargerLocalisations();
110
	});
114
	});
111
}
115
}
112
 
116
 
113
function requeteEnCours() {
117
function requeteEnCours() {
114
	return (xmlHttpRequest != null && xmlHttpRequest.readyState != 4);
118
	return (xmlHttpRequest != null && xmlHttpRequest.readyState != 4);
115
}
119
}
116
 
120
 
117
function estStatutRequeteOK(x_status) {
121
function estStatutRequeteOK(x_status) {
118
	return (x_status == 200 || x_status == 304 || x_status == 0);
122
	return (x_status == 200 || x_status == 304 || x_status == 0);
119
}
123
}
120
 
124
 
121
function ordonnerCollectionsParStructures() {
125
function ordonnerCollectionsParStructures() {
122
	for (var index = 0; index < collections.length; index ++) {
126
	for (var index = 0; index < collections.length; index ++) {
123
		var indexStructure = 0;
127
		var indexStructure = 0;
124
		while (indexStructure < structures.length && structures[indexStructure].id != collections[index].cs_id_structure) {
128
		while (indexStructure < structures.length && structures[indexStructure].id != collections[index].cs_id_structure) {
125
			indexStructure ++;
129
			indexStructure ++;
126
		}
130
		}
127
		if (indexStructure == structures.length) {
131
		if (indexStructure == structures.length) {
128
			var structure = {
132
			var structure = {
129
				"id"    : collections[index].cs_id_structure,
133
				"id"    : collections[index].cs_id_structure,
130
				"nom"   : collections[index].cs_nom,
134
				"nom"   : collections[index].cs_nom,
131
				"ville" : collections[index].cs_ville,
135
				"ville" : collections[index].cs_ville,
132
				"code_postal" : collections[index].cs_code_postal,
136
				"code_postal" : collections[index].cs_code_postal,
133
				"longitude"   : collections[index].cs_longitude,
137
				"longitude"   : collections[index].cs_longitude,
134
				"latitude"    : collections[index].cs_latitude,
138
				"latitude"    : collections[index].cs_latitude,
135
				"collections" : new Array()
139
				"collections" : new Array()
136
			};
140
			};
137
			structures.push(structure);
141
			structures.push(structure);
138
		}
142
		}
139
		var collection = {
143
		var collection = {
140
			"id"  : collections[index].cc_id_collection,
144
			"id"  : collections[index].cc_id_collection,
141
			"nom" : collections[index].cc_nom
145
			"nom" : collections[index].cc_nom
142
		};
146
		};
143
		structures[indexStructure].collections.push(collection);
147
		structures[indexStructure].collections.push(collection);
144
	}
148
	}
145
}
149
}
146
 
150
 
147
function chargerLocalisations() {
151
function chargerLocalisations() {
148
	var nombreStructuresAffichees = 0;
152
	var nombreStructuresAffichees = 0;
149
	for (var index = 0; index < structures.length; index ++) {
153
	for (var index = 0; index < structures.length; index ++) {
150
		if ((structures[index].longitude != null && structures[index].longitude != "")
154
		if ((structures[index].longitude != null && structures[index].longitude != "")
151
			&& (structures[index].latitude != null && structures[index].latitude != "")) {
155
			&& (structures[index].latitude != null && structures[index].latitude != "")) {
152
			var existeMarqueur = rechercherExistenceMarqueur(structures[index].longitude, structures[index].latitude);
156
			var existeMarqueur = rechercherExistenceMarqueur(structures[index].longitude, structures[index].latitude);
153
			if (existeMarqueur == null) {
157
			if (existeMarqueur == null) {
154
				creerMarqueur(structures[index]);
158
				creerMarqueur(structures[index]);
155
				nombreStructuresAffichees ++;
159
				nombreStructuresAffichees ++;
156
			} else {
160
			} else {
157
				ajouterStructureAMarqueur(existeMarqueur, structures[index]);
161
				ajouterStructureAMarqueur(existeMarqueur, structures[index]);
158
			}
162
			}
159
		}
163
		}
160
	}
164
	}
161
	if (nombreStructuresAffichees > 0) {
165
	if (nombreStructuresAffichees > 0) {
162
		map.fitBounds(coucheStructures.getBounds());
166
		map.fitBounds(coucheStructures.getBounds());
163
	}
167
	}
164
}
168
}
165
 
169
 
166
function rechercherExistenceMarqueur(longitude, latitude) {
170
function rechercherExistenceMarqueur(longitude, latitude) {
167
	var existeMarqueur = null;
171
	var existeMarqueur = null;
168
	coucheStructures.eachLayer(function(layer) {
172
	coucheStructures.eachLayer(function(layer) {
169
		if (layer.getLatLng().lat == latitude && layer.getLatLng().lng == longitude) {
173
		if (layer.getLatLng().lat == latitude && layer.getLatLng().lng == longitude) {
170
			existeMarqueur = layer;
174
			existeMarqueur = layer;
171
		}
175
		}
172
	});
176
	});
173
	return existeMarqueur;
177
	return existeMarqueur;
174
}
178
}
175
 
179
 
176
function creerMarqueur(structure) {
180
function creerMarqueur(structure) {
177
	var latlng = new L.LatLng(structure.latitude, structure.longitude);
181
	var latlng = new L.LatLng(structure.latitude, structure.longitude);
178
	var marqueur = new L.Marker(latlng, {
182
	var marqueur = new L.Marker(latlng, {
179
		structures : [structure],
183
		structures : [structure],
180
		structuresNom : structure.nom
184
		structuresNom : structure.nom
181
	});
185
	});
182
	marqueur.on('click', surClickMarqueur);
186
	marqueur.on('click', surClickMarqueur);
183
	marqueur.on('mouseover', construireToolTipMarqueur);
187
	marqueur.on('mouseover', construireToolTipMarqueur);
184
	coucheStructures.addLayer(marqueur);
188
	coucheStructures.addLayer(marqueur);
185
}
189
}
186
 
190
 
187
function construireToolTipMarqueur(event) {
191
function construireToolTipMarqueur(event) {
188
	// changer la propriété title du marqueur ne fonctionne pas
192
	// changer la propriété title du marqueur ne fonctionne pas
189
	// en dehors du constructeur (mais cette méthode est-elle pérenne ?)
193
	// en dehors du constructeur (mais cette méthode est-elle pérenne ?)
190
	// le clustering n'affichant pas tous les marqueurs, on doit remplir le tooltip
194
	// le clustering n'affichant pas tous les marqueurs, on doit remplir le tooltip
191
	event.target._icon.title = event.target.options.structuresNom;
195
	event.target._icon.title = event.target.options.structuresNom;
192
}
196
}
193
 
197
 
194
function ajouterStructureAMarqueur(marqueur, structure) {
198
function ajouterStructureAMarqueur(marqueur, structure) {
195
	marqueur.options.structures.push(structure);
199
	marqueur.options.structures.push(structure);
196
	marqueur.options.structuresNom += "\n"+structure.nom;
200
	marqueur.options.structuresNom += "\n"+structure.nom;
197
}
201
}
198
 
202
 
199
function surClickMarqueur(event) {
203
function surClickMarqueur(event) {
200
	var latlng = event.target.getLatLng();
204
	var latlng = event.target.getLatLng();
201
	var structures = event.target.options.structures;
205
	var structures = event.target.options.structures;
202
	afficherCollections(structures, latlng);
206
	afficherCollections(structures, latlng);
203
}
207
}
204
 
208
 
205
function afficherCollections(structures, latlng) {
209
function afficherCollections(structures, latlng) {
206
	masquerInfoBulle();
210
	masquerInfoBulle();
207
	infoBulle = new L.Popup({maxWidth : 0.25*$(window).width(), maxHeight : 0.35*$(window).height()});
211
	infoBulle = new L.Popup({maxWidth : 0.25*$(window).width(), maxHeight : 0.35*$(window).height()});
208
	infoBulle.setLatLng(latlng);
212
	infoBulle.setLatLng(latlng);
209
	infoBulle.openOn(map);
213
	infoBulle.openOn(map);
210
	remplirContenuPopup(structures);
214
	remplirContenuPopup(structures);
211
	$("a").css("color", "#598000");
215
	$("a").css("color", "#598000");
212
	map.setView(latlng, map.getZoom());
216
	map.setView(latlng, map.getZoom());
213
}
217
}
214
 
218
 
215
function masquerInfoBulle() {
219
function masquerInfoBulle() {
216
	if (infoBulle != null && map.hasLayer(infoBulle)) {
220
	if (infoBulle != null && map.hasLayer(infoBulle)) {
217
		map.removeLayer(infoBulle);
221
		map.removeLayer(infoBulle);
218
	}
222
	}
219
	infoBulle = null;
223
	infoBulle = null;
220
}
224
}
221
 
225
 
222
function remplirContenuPopup(structures) {
226
function remplirContenuPopup(structures) {
223
	$("#structure").empty();
227
	$("#structure").empty();
224
	var entetePopup = {
228
	var entetePopup = {
225
		"ville" : structures[0].ville,
229
		"ville" : structures[0].ville,
226
		"code_postal" : structures[0].code_postal
230
		"code_postal" : structures[0].code_postal
227
	}
231
	}
228
	$("#tpl-structure-entete").tmpl(entetePopup).appendTo($("#structure"));
232
	$("#tpl-structure-entete").tmpl(entetePopup).appendTo($("#structure"));
229
	
233
	
230
	$.each(structures, function(index, structure) {
234
	$.each(structures, function(index, structure) {
231
		var structureAjout = {
235
		var structureAjout = {
232
			"id"    : structure.id,
236
			"id"    : structure.id,
233
			"nom"   : structure.nom,
237
			"nom"   : structure.nom,
234
			"ville" : structure.ville,
238
			"ville" : structure.ville,
235
			"code_postal" : structure.code_postal,
239
			"code_postal" : structure.code_postal,
236
			"collections" : structure.collections
240
			"collections" : structure.collections
237
		};
241
		};
238
		$("#tpl-structure").tmpl(structureAjout).appendTo($("#structure"));
242
		$("#tpl-structure").tmpl(structureAjout).appendTo($("#structure"));
239
	});
243
	});
240
	
244
	
241
	infoBulle.setContent($("#structure").html());
245
	infoBulle.setContent($("#structure").html());
242
}
246
}
243
 
247
 
244
function verifierZoom() {
248
function verifierZoom() {
245
	if(map.getZoom() > 13) {
249
	if(map.getZoom() > 13) {
246
		map.setZoom(13);
250
		map.setZoom(13);
247
	}
251
	}
248
}
252
}