[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/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 <[(#VAL{li}|saisie_balise_structure_formulaire)] 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) {
54 data.lng = data.lng+360;
55 } else if (data.lng > 180) {
56 data.lng = data.lng-360;
57 }
58 $('#champ_#ENV{champ_zoom,zoom}').val(zoom);
59 if (action == 'click') {
60 $('#champ_#ENV{champ_lat,lat}').val(data.lat);
61 $('#champ_#ENV{champ_lon,lon}').val(data.lng);
62 annuler_geocoder = 1;
63 form_map.panTo(data);
64 marker.setLatLng(data);
65 }
66 else if (annuler_geocoder != 1) {
67 if (data.point == 'undefined') {
68 $('#champ_#ENV{champ_lat,lat}').val(data.lat);
69 $('#champ_#ENV{champ_lon,lon}').val(data.lng);
70 form_map.panTo(data);
71 marker.setLatLng(data);
72 } else {
73 $('#champ_#ENV{champ_lat,lat}').val(data.point.lat);
74 $('#champ_#ENV{champ_lon,lon}').val(data.point.lng);
75 form_map.panTo(data.point);
76 marker.setLatLng(data.point);
77 }
78 }
79 if (!marker._map) {
80 form_map.addLayer(marker);
81 }
82 }
83
84 [(#GET{geocoder}|oui)
85 function geocode(query) {
86 if (! query.error) {
87 $('#champ_#ENV{champ_adresse,adresse}').val(query.street).change();
88 $('#champ_#ENV{champ_code_postal,code_postal}').val(query.postcode).change();
89 $('#champ_#ENV{champ_ville,ville}').val(query.locality).change();
90 $('#champ_#ENV{champ_departement,departement}').val(query.departement).change();
91 $('#champ_#ENV{champ_region,region}').val(query.region).change();
92 $('#champ_#ENV{champ_pays,pays}').val(query.country).change();
93 $('#champ_#ENV{champ_code_pays,code_pays}').val(query.country_code).change();
94 maj_inputs(form_map,query,'geocoding');
95 } else {
96 alert('<:gis:erreur_geocoder:> '+query.search);
97 }
98 }]
99
100 var init_map = function(callback) {
101 // creer la carte
102 var map_container = 'map_[(#ENV{nom})]';
103 form_map = new L.Map(map_container);
104
105 // affecter sur l'objet du DOM
106 jQuery("#"+map_container).get(0).map=form_map;
107
108 // appeler l'éventuelle fonction de callback
109 if (callback && typeof(callback) === "function") {
110 form_map.on('load',function(e){
111 callback(e.target);
112 });
113 }
114
115 form_map.attributionControl.setPrefix('');
116
117 marker = new L.Marker(new L.LatLng(#ENV{lat,0}, #ENV{lon,0}), {draggable: true});
118
119 //default layer
120 #SET{layer_defaut,#REM|gis_layer_defaut} #SET{layers,#EVAL{$GLOBALS['gis_layers']}}
121 var [(#GET{layer_defaut})] = [new (#GET{layers}|table_valeur{#GET{layer_defaut}/layer})];
122 form_map.addLayer([(#GET{layer_defaut})]);
123
124 <B_layers>
125 var layers_control = new L.Control.Layers();
126 layers_control.addBaseLayer([(#GET{layer_defaut})],["(#GET{layers}|table_valeur{#GET{layer_defaut}/nom})"]);
127 <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}>[
128 (#CLE|!={#GET{layer_defaut}}|oui|et{#CLE|in_array{#CONFIG{gis/layers,#ARRAY}}|oui}|oui)
129 layers_control.addBaseLayer([new (#VALEUR|table_valeur{layer})],"[(#VALEUR|table_valeur{nom})]");]
130 </BOUCLE_layers>
131 form_map.addControl(layers_control);
132 // ajouter l'objet du controle de layers à la carte pour permettre d'y accéder depuis le callback
133 form_map.layersControl = layers_control;
134 // classe noajax sur le layer_control pour éviter l'ajout de hidden par SPIP
135 $(layers_control._form).addClass('noajax');
136 </B_layers>
137
138 [(#GET{utiliser_bb}|non)
139 form_map.setView(new L.LatLng([(#GET{init_lat})], [(#GET{init_lon})]), [(#GET{init_zoom})]);
140 ]
141 [(#GET{utiliser_bb}|oui)
142 form_map.fitBounds(
143 new L.LatLngBounds(
144 new L.LatLng([(#GET{init_sw_lat})], [(#GET{init_sw_lon})]),
145 new L.LatLng([(#GET{init_ne_lat})], [(#GET{init_ne_lon})])
146 )
147 );
148 // mettre à jour les champs de latitude et longitude quand la carte est chargée
149 // a voir si on le fait dans tous les cas, pas seulement pour la boundingbox, comme pour le zoom
150 form_map.on('load', function(e) {
151 $('#champ_#ENV{champ_lat,lat}').val(e.latlng.lat);
152 $('#champ_#ENV{champ_lon,lon}').val(e.latlng.lon);
153 });
154 ]
155
156 [(#GET{geocoder}|oui)
157 geocoder = new L.Geocoder(geocode,{acceptLanguage:'#ENV{lang}'});]
158
159 [(#REM) Ici on utilise #ENV{lat} et #ENV{lon}, et pas les valeurs
160 d'initialisation #GET{init_lat} et #GET{init_lon} qui sont toujours remplies]
161 [(#ENV{lat}|et{#ENV{lon}}|oui)
162 var data = {
163 "type": "FeatureCollection",
164 "features": \[
165 {"type": "Feature",
166 "geometry": {"type": "Point", "coordinates": \[#ENV{lon}, #ENV{lat}\]},
167 "id":"#ENV{id_gis,oui}",
168 "properties": {
169 "title":[(#ENV{titre,''}|supprimer_numero|json_encode)],
170 "description":[(#ENV{descriptif,''}|json_encode)][
171 (#LOGO_GIS|gis_icon_properties)]
172 }
173 }\]
174 }
175
176 var geojson = new L.geoJson('', {
177 onEachFeature: function (feature, layer) {
178 marker = layer;
179 layer.options.draggable = true;
180 if (feature.properties && feature.properties.icon){
181 layer.setIcon(new L.Icon({
182 iconUrl: feature.properties.icon,
183 iconSize: new L.Point( feature.properties.icon_size\[0\], feature.properties.icon_size\[1\] ),
184 iconAnchor: new L.Point( feature.properties.icon_anchor\[0\], feature.properties.icon_anchor\[1\] ),
185 popupAnchor: new L.Point( feature.properties.popup_anchor\[0\], feature.properties.popup_anchor\[1\] )
186 }));
187 }
188 if (feature.properties && feature.properties.title){
189 layer.bindPopup(feature.properties.title);
190 }
191 }
192 }).addTo(form_map);
193 geojson.addData(data);]
194
195 // mettre a jour les coordonnees quand on clique la carte
196 form_map.on('click', function(e) {
197 annuler_geocoder = 0;
198 maj_inputs(form_map,e.latlng,'click');
199 });
200
201 marker.on('dragend', function(e) {
202 maj_inputs(form_map,e.target._latlng,'click');
203 });
204
205 // mettre à jour le zoom quand on le modifie
206 form_map.on('zoomend', function(e) {
207 $('#champ_#ENV{champ_zoom,zoom}').val(e.target._zoom);
208 });
209
210 [(#GET{geocoder}|oui)
211 // geocoder si clic...
212 $('a##ENV{nom}_rechercher_geocodage').css("cursor","pointer").on('click',function(){
213 var address = $("#champ_#ENV{nom}_geocoder").val();
214 annuler_geocoder = 0;
215 geocoder.geocode(address);
216 });
217
218 // ne pas soumettre le formulaire si on presse Entree depuis le champ de recherche
219 $('#champ_#ENV{nom}_geocoder').on('keypress', function(e){
220 if (e.which == 13) {
221 $('a##ENV{nom}_rechercher_geocodage').trigger("click");
222 return false;
223 }
224 });]
225
226 [(#ENV{id_gis}|non|ou{#ENV{id_gis}|=={oui}}|et{#CONFIG{gis/geolocaliser_user_html5}|=={on}}|oui)
227 form_map.locate({setView: true, maxZoom: [(#GET{init_zoom})]});]
228
229 };
230
231 $(function(){
232 jQuery.getScript('[(#PRODUIRE{fond=javascript/gis.js})]',function(){
233 if (typeof(callback_form_map) === "function") {
234 init_map(callback_form_map);
235 } else {
236 init_map();
237 }
238 });
239 });
240
241 })(jQuery);
242 /*]]>*/
243 </script>
244 #ENV*{inserer_fin}
245 </[(#VAL{li}|saisie_balise_structure_formulaire)]>
246 [(#GET{geocoder}|oui)
247 <[(#VAL{li}|saisie_balise_structure_formulaire)] class="rechercher_adresse editer editer_[(#ENV{nom})]">
248 <label for="champ_#ENV{nom}_geocoder"><:gis:label_rechercher_address:></label>
249 <input type="text" class="text" name="champ_#ENV{nom}_geocoder" id="champ_#ENV{nom}_geocoder" placeholder="<:gis:placeholder_geocoder|attribut_html:>" value="" />
250 <a id="#ENV{nom}_rechercher_geocodage"><:info_rechercher:></a>
251 </[(#VAL{li}|saisie_balise_structure_formulaire)]>]