[PLUGINS] ~maj gis v4.26.1-->4.26.10
[lhc/web/clavette_www.git] / www / plugins / gis / saisies / carte.html
1 [(#REM)
2
3 Saisie carte
4
5 Parametres optionnels:
6
7 - lat = 48.3 latitude du centre de la carte
8 - lon = -4.7 longitude du centre de la carte
9 - zoom = 5 zoom de la carte
10 - sw_lat = lat - 10° latitude du sud-ouest de la bounding box
11 - sw_lon = lon - 10° longitude du sud-ouest de la bounding box
12 - ne_lat = lat + 10° latitude du nord-est de la bounding box
13 - ne_lon = lon + 10° longitude du nord-est de la bounding box
14 - largeur = 100%
15 - hauteur = 350px
16
17 ]
18
19 [(#SET{init_lat,#ENV{lat,#CONFIG{gis/lat,0}}})]
20 [(#SET{init_lon,#ENV{lon,#CONFIG{gis/lon,0}}})]
21 [(#SET{init_zoom,#ENV{zoom,#CONFIG{gis/zoom,0}}})]
22 [(#REM) On utilise la bounding box seulement si le centre n'a pas été donné et si les quatre valeurs de la bounding box sont renseignées
23 Les valeurs par defaut sont "centre +/- 10°", ce qui est naze, mais c'est un cas normalement impossible
24 ]
25 [(#ENV{lat}|ou{#ENV{lon}}|non|et{#ENV{sw_lat}}|et{#ENV{sw_lon}}|et{#ENV{ne_lat}}|et{#ENV{ne_lon}})
26 #SET{utiliser_bb, oui}
27 #SET{init_sw_lat,#ENV{sw_lat,#GET{lat}|moins{10}}}
28 #SET{init_sw_lon,#ENV{sw_lon,#GET{lon}|moins{10}}}
29 #SET{init_ne_lat,#ENV{ne_lat,#GET{lat}|plus{10}}}
30 #SET{init_ne_lon,#ENV{ne_lon,#GET{lon}|plus{10}}}
31 ]
32
33 <li class="pleine_largeur editer editer_[(#ENV{nom})][ (#ENV{li_class})][ saisie_(#ENV{type_saisie})]"[ data-id="(#ENV{id_saisie})"]>
34 #ENV*{inserer_debut}
35 <div id="map_[(#ENV{nom})]" name="formMap" class="formMap" style="width: #ENV{largeur,100%}; height: #ENV{hauteur,350px}"></div>
36 <script type="text/javascript">
37 /*<![CDATA[*/
38 var form_map,
39 annuler_geocoder = 0;
40 [(#ENV{recherche}|!={non}|oui|et{#CONFIG{gis/geocoder}|oui})
41 [(#SET{geocoder,oui})]
42 var geocoder;]
43
44 (function($){
45 var marker;
46
47 var maj_inputs = function(map,data,action) {
48 [(#GET{geocoder}|oui)
49 if (action != 'geocoding') {
50 var f = geocoder.geocode(data);
51 }]
52 var zoom = map.getZoom();
53 if(data.lng <= -180) data.lng = data.lng+360
54 else if(data.lng > 180) data.lng = data.lng-360;
55 $('#champ_#ENV{champ_zoom,zoom}').val(zoom);
56 if(action == 'click'){
57 $('#champ_#ENV{champ_lat,lat}').val(data.lat);
58 $('#champ_#ENV{champ_lon,lon}').val(data.lng);
59 annuler_geocoder = 1;
60 form_map.panTo(data);
61 marker.setLatLng(data);
62 }
63 else if(annuler_geocoder != 1){
64 if(data.point == 'undefined'){
65 $('#champ_#ENV{champ_lat,lat}').val(data.lat);
66 $('#champ_#ENV{champ_lon,lon}').val(data.lng);
67 form_map.panTo(data);
68 marker.setLatLng(data);
69 }else{
70 $('#champ_#ENV{champ_lat,lat}').val(data.point.lat);
71 $('#champ_#ENV{champ_lon,lon}').val(data.point.lng);
72 form_map.panTo(data.point);
73 marker.setLatLng(data.point);
74 }
75 }
76 if (!marker._map)
77 form_map.addLayer(marker);
78 }
79
80 [(#GET{geocoder}|oui)
81 function geocode(query) {
82 if(! query.error){
83 $('#champ_#ENV{champ_adresse,adresse}').val(query.street);
84 $('#champ_#ENV{champ_code_postal,code_postal}').val(query.postcode);
85 $('#champ_#ENV{champ_ville,ville}').val(query.locality);
86 $('#champ_#ENV{champ_departement,departement}').val(query.departement);
87 $('#champ_#ENV{champ_region,region}').val(query.region);
88 $('#champ_#ENV{champ_pays,pays}').val(query.country);
89 $('#champ_#ENV{champ_code_pays,code_pays}').val(query.country_code);
90 maj_inputs(form_map,query,'geocoding');
91 }else{
92 alert('<:gis:erreur_geocoder:> '+query.search);
93 }
94 }]
95
96 var init_map = function(callback) {
97 // creer la carte
98 var map_container = 'map_[(#ENV{nom})]';
99 form_map = new L.Map(map_container);
100
101 // affecter sur l'objet du DOM
102 jQuery("#"+map_container).get(0).map=form_map;
103
104 // appeler l'éventuelle fonction de callback
105 if (callback && typeof(callback) === "function") {
106 form_map.on('load',function(e){
107 callback(e.target);
108 });
109 }
110
111 form_map.attributionControl.setPrefix('');
112
113 marker = new L.Marker(new L.LatLng(#ENV{lat,0}, #ENV{lon,0}), {draggable: true});
114
115 //default layer
116 #SET{layer_defaut,#REM|gis_layer_defaut} #SET{layers,#EVAL{$GLOBALS['gis_layers']}}
117 var [(#GET{layer_defaut})] = [new (#GET{layers}|table_valeur{#GET{layer_defaut}/layer})];
118 form_map.addLayer([(#GET{layer_defaut})]);
119
120 <B_layers>
121 var layers_control = new L.Control.Layers();
122 layers_control.addBaseLayer([(#GET{layer_defaut})],["(#GET{layers}|table_valeur{#GET{layer_defaut}/nom})"]);
123 <BOUCLE_layers(DATA){source table, #GET{layers}}{si #ENV{control_type,#ENV{controle_type}}|!={non}|et{#ENV{no_control,#ENV{aucun_controle}}|!={oui}}|et{#CONFIG{gis/layers,#ARRAY}|count|>{1}|oui}|oui}>[
124 (#CLE|!={#GET{layer_defaut}}|oui|et{#CLE|in_array{#CONFIG{gis/layers,#ARRAY}}|oui}|oui)
125 layers_control.addBaseLayer([new (#VALEUR|table_valeur{layer})],"[(#VALEUR|table_valeur{nom})]");]
126 </BOUCLE_layers>
127 form_map.addControl(layers_control);
128 // ajouter l'objet du controle de layers à la carte pour permettre d'y accéder depuis le callback
129 form_map.layersControl = layers_control;
130 // classe noajax sur le layer_control pour éviter l'ajout de hidden par SPIP
131 $(layers_control._form).addClass('noajax');
132 </B_layers>
133
134 [(#GET{utiliser_bb}|non)
135 form_map.setView(new L.LatLng([(#GET{init_lat})], [(#GET{init_lon})]), [(#GET{init_zoom})]);
136 ]
137 [(#GET{utiliser_bb}|oui)
138 form_map.fitBounds(
139 new L.LatLngBounds(
140 new L.LatLng([(#GET{init_sw_lat})], [(#GET{init_sw_lon})]),
141 new L.LatLng([(#GET{init_ne_lat})], [(#GET{init_ne_lon})])
142 )
143 );
144 // mettre à jour les champs de latitude et longitude quand la carte est chargée
145 // a voir si on le fait dans tous les cas, pas seulement pour la boundingbox, comme pour le zoom
146 form_map.on('load', function(e) {
147 $('#champ_#ENV{champ_lat,lat}').val(e.latlng.lat);
148 $('#champ_#ENV{champ_lon,lon}').val(e.latlng.lon);
149 });
150 ]
151
152 [(#GET{geocoder}|oui)
153 geocoder = new L.Geocoder(geocode,{acceptLanguage:'#ENV{lang}'});]
154
155 [(#REM) Ici on utilise #ENV{lat} et #ENV{lon}, et pas les valeurs
156 d'initialisation #GET{init_lat} et #GET{init_lon} qui sont toujours remplies]
157 [(#ENV{lat}|et{#ENV{lon}}|oui)
158 var data = {
159 "type": "FeatureCollection",
160 "features": \[
161 {"type": "Feature",
162 "geometry": {"type": "Point", "coordinates": \[#ENV{lon}, #ENV{lat}\]},
163 "id":"#ENV{id_gis,oui}",
164 "properties": {
165 "title":[(#ENV{titre,''}|supprimer_numero|json_encode)],
166 "description":[(#ENV{descriptif,''}|json_encode)][
167 (#LOGO_GIS|gis_icon_properties)]
168 }
169 }\]
170 }
171
172 var geojson = new L.geoJson('', {
173 onEachFeature: function (feature, layer) {
174 marker = layer;
175 layer.options.draggable = true;
176 if (feature.properties && feature.properties.icon){
177 layer.setIcon(new L.Icon({
178 iconUrl: feature.properties.icon,
179 iconSize: new L.Point( feature.properties.icon_size\[0\], feature.properties.icon_size\[1\] ),
180 iconAnchor: new L.Point( feature.properties.icon_anchor\[0\], feature.properties.icon_anchor\[1\] ),
181 popupAnchor: new L.Point( feature.properties.popup_anchor\[0\], feature.properties.popup_anchor\[1\] )
182 }));
183 }
184 if (feature.properties && feature.properties.title){
185 layer.bindPopup(feature.properties.title);
186 }
187 }
188 }).addTo(form_map);
189 geojson.addData(data);]
190
191 // mettre a jour les coordonnees quand on clique la carte
192 form_map.on('click', function(e) {
193 annuler_geocoder = 0;
194 maj_inputs(form_map,e.latlng,'click');
195 });
196
197 marker.on("dragend", function(e){
198 maj_inputs(form_map,e.target._latlng,'click');
199 });
200
201 // mettre à jour le zoom quand on le modifie
202 form_map.on('zoomend', function(e) {
203 $('#champ_#ENV{champ_zoom,zoom}').val(e.target._zoom);
204 });
205
206 [(#GET{geocoder}|oui)
207 // geocoder si clic...
208 $('a##ENV{nom}_rechercher_geocodage').css("cursor","pointer").click(function(){
209 var address = $("#champ_#ENV{nom}_geocoder").val();
210 annuler_geocoder = 0;
211 geocoder.geocode(address);
212 });
213
214 // ne pas soumettre le formulaire si on presse Entree depuis le champ de recherche
215 $('#champ_#ENV{nom}_geocoder').keypress(function(e){
216 if (e.which == 13) {
217 $('a##ENV{nom}_rechercher_geocodage').trigger("click");
218 return false;
219 }
220 });]
221
222 [(#ENV{id_gis}|non|ou{#ENV{id_gis}|=={oui}}|et{#CONFIG{gis/geolocaliser_user_html5}|=={on}}|oui)
223 form_map.locate({setView: true, maxZoom: [(#GET{init_zoom})]});]
224
225 };
226
227 $(function(){
228 jQuery.getScript('[(#PRODUIRE{fond=javascript/gis.js}|compacte)]',function(){
229 if (typeof(callback_form_map) === "function") {
230 init_map(callback_form_map);
231 } else {
232 init_map();
233 }
234 });
235 });
236
237 })(jQuery);
238 /*]]>*/
239 </script>
240 #ENV*{inserer_fin}
241 </li>
242 [(#GET{geocoder}|oui)
243 <li class="rechercher_adresse editer_[(#ENV{nom})]">
244 <label for="champ_#ENV{nom}_geocoder"><:gis:label_rechercher_address:></label>
245 <input type="text" class="text" name="champ_#ENV{nom}_geocoder" id="champ_#ENV{nom}_geocoder" value="" />
246 <a id="#ENV{nom}_rechercher_geocodage"><:info_rechercher:></a>
247 </li>]