2 // Plugin Leaflet L.Map.Gis
3 L
.Map
.Gis
= L
.Map
.extend({
5 includes
: L
.Mixin
.Events
,
14 gis_layers
: L
.gisConfig
.gis_layers
,
15 default_layer
: L
.gisConfig
.default_layer
,
16 affiche_layers
: L
.gisConfig
.affiche_layers
,
18 overviewControl
: false,
20 layersControlOptions
: {},
24 disableClusteringAtZoom
: 0,
25 showCoverageOnHover
: false,
27 spiderfyOnMaxZoom
: false
30 autocenterandzoom
: false,
42 localize_visitor
: false,
43 localize_visitor_zoom
: 0,
44 centrer_fichier
: true,
52 initialize: function (id
,options
) {
53 L
.Util
.setOptions(this, options
);
55 this.on('load', function () {
56 // Affecter sur l'objet du DOM
57 jQuery('#'+this._container
.id
).get(0).map
= this;
58 // Appeler l'éventuelle fonction de callback
59 if (this.options
.callback
&& typeof(this.options
.callback
) === 'function')
60 this.options
.callback(this);
61 // trigger load sur l'objet du DOM
62 jQuery('#'+this._container
.id
).trigger('load',this);
65 L
.Map
.prototype.initialize
.call(this, id
, options
);
67 if (this.options
.utiliser_bb
) {
70 [this.options
.sw_lat
, this.options
.sw_lon
],
71 [this.options
.ne_lat
, this.options
.ne_lon
]
76 this.populateTileLayers(this.options
.affiche_layers
);
84 if (this.options
.localize_visitor
) {
85 var maxZoom
= this.options
.localize_visitor_zoom
;
86 this.on('locationerror',function (e
) {
87 maxZoom
= this.options
.zoom
;
90 this.locate({setView
: true, maxZoom
: maxZoom
});
93 // Si pas de points affichés trigger ready ici
94 if (!this.options
.affiche_points
|| !Object
.keys(this.options
.json_points
).length
) {
95 jQuery('#'+this._container
.id
).trigger('ready', this);
99 populateTileLayers: function () {
100 // Fond de carte par défaut
101 if (this.options
.default_layer
!= 'none') {
102 var default_layer
= this.createTileLayer(this.options
.default_layer
);
103 this.addLayer(default_layer
);
105 // Fonds de carte supplémentaires
106 if (this.options
.layersControl
&& !this.options
.noControl
&& this.options
.affiche_layers
.length
>1) {
107 var layers_control
= L
.control
.layers('','',this.options
.layersControlOptions
);
108 if (this.options
.default_layer
!= 'none') {
109 layers_control
.addBaseLayer(default_layer
,this.options
.gis_layers
[this.options
.default_layer
].nom
);
111 for (var l
in this.options
.affiche_layers
) {
112 if (this.options
.affiche_layers
[l
]!==this.options
.default_layer
) {
113 var layer
= this.createTileLayer(this.options
.affiche_layers
[l
]);
114 if (typeof layer
!=='undefined')
115 layers_control
.addBaseLayer(layer
,this.options
.gis_layers
[this.options
.affiche_layers
[l
]].nom
);
118 this.addControl(layers_control
);
119 // Ajouter l'objet du controle de layers à la carte pour permettre d'y accéder depuis le callback
120 this.layersControl
= layers_control
;
121 // Classe noajax sur le layer_control pour éviter l'ajout de hidden par SPIP
122 L
.DomUtil
.addClass(layers_control
._form
,'noajax');
126 initControls: function () {
127 this.attributionControl
.setPrefix('');
128 if (this.options
.scaleControl
)
129 L
.control
.scale().addTo(this);
130 if (this.options
.overviewControl
&& this.options
.default_layer
!= 'none') {
131 // todo ajouter une option pour permettre de choisir la couche à afficher dans la minimap
132 var minimap_layer
= this.createTileLayer(this.options
.default_layer
);
133 L
.control
.minimap(minimap_layer
,{width
: 100,height
: 100, toggleDisplay
: true}).addTo(this);
137 createTileLayer: function (name
) {
139 if (typeof this.options
.gis_layers
[name
]!=='undefined')
140 eval('layer=new '+ this.options
.gis_layers
[name
].layer
+';');
144 // API setGeoJsonFeatureIcon : Pour Ajouter l'icone d'un point (feature = item d'un GeoJson)
145 setGeoJsonFeatureIcon: function (feature
, layer
) {
146 // Déclarer l'icone du points, si défini
147 if (feature
.properties
&& feature
.properties
.icon
) {
149 'iconUrl': feature
.properties
.icon
,
150 'iconSize': [feature
.properties
.icon_size
[0], feature
.properties
.icon_size
[1]],
151 'iconAnchor': [feature
.properties
.icon_anchor
[0], feature
.properties
.icon_anchor
[1]]
153 if (feature
.properties
.popup_anchor
)
154 icon_options
.popupAnchor
= [feature
.properties
.popup_anchor
[0], feature
.properties
.popup_anchor
[1]];
155 if (feature
.properties
.shadow
)
156 icon_options
.shadowUrl
= feature
.properties
.shadow
;
157 if (feature
.properties
.shadow_size
)
158 icon_options
.shadowSize
= [feature
.properties
.shadow_size
[0], feature
.properties
.shadow_size
[1]];
159 layer
.setIcon(L
.icon(icon_options
));
163 // API setGeoJsonFeaturePopup : Pour Ajouter le texte de popup d'un point (feature = item d'un GeoJson)
164 setGeoJsonFeaturePopup: function (feature
, layer
) {
165 // Déclarer le contenu de la popup s'il y en a
166 if (feature
.properties
167 && !feature
.properties
.noclick
168 && (feature
.properties
.title
|| feature
.properties
.description
||
169 (this.options
.langue
&& (feature
.properties
['title_'+this.options
.langue
] || feature
.properties
['description_'+this.options
.langue
])))) {
170 var popupContent
= '',
172 description_ok
= false;
173 if (this.options
.langue
) {
174 langue
= this.options
.langue
;
175 if (feature
.properties
['title_'+langue
]) {
176 popupContent
= '<strong class="title">' + feature
.properties
['title_'+langue
] + '</strong>';
177 } else if (feature
.properties
.title
)
178 popupContent
= '<strong class="title">' + feature
.properties
.title
+ '</strong>';
179 if (feature
.properties
['description_'+langue
]) {
180 popupContent
= popupContent
+ feature
.properties
['description_'+langue
];
181 description_ok
= true;
183 } else if(feature
.properties
.title
)
184 popupContent
= '<strong class="title">' + feature
.properties
.title
+ '</strong>';
185 if (!description_ok
&& feature
.properties
.description
)
186 popupContent
= popupContent
+ feature
.properties
.description
;
187 if (feature
.properties
.popup_options
)
188 popupOptions
= feature
.properties
.popup_options
;
189 layer
.bindPopup(popupContent
,popupOptions
);
194 parseGeoJson: function (data
) {
196 // Analyse des points et déclaration (sans regroupement des points en cluster)
197 if (!map
.options
.cluster
) {
198 if (data
.features
&& data
.features
.length
> 0) {
199 var geojson
= L
.geoJson('', {
200 style
: this.options
.pathStyles
? this.options
.pathStyles : function (feature
) {
201 if (feature
.properties
&& feature
.properties
.styles
)
202 return feature
.properties
.styles
;
206 onEachFeature: function (feature
, layer
) {
207 // Déclarer l'icone du point
208 if (feature
.geometry
.type
== 'Point') {
209 map
.setGeoJsonFeatureIcon(feature
, layer
);
211 // Déclarer le contenu de la popup s'il y en a
212 map
.setGeoJsonFeaturePopup(feature
, layer
);
214 }).addData(data
).addTo(map
);
216 if (map
.options
.autocenterandzoom
) {
217 if (data
.features
.length
== 1 && data
.features
[0].geometry
.type
== 'Point')
218 map
.setView(geojson
.getBounds().getCenter(), map
.options
.zoom
);
220 map
.fitBounds(geojson
.getBounds());
222 if (map
.options
.openId
)
223 gis_focus_marker(map
.options
.openId
,map
.options
.mapId
);
225 if (typeof map
.geojsons
=='undefined') map
.geojsons
= [];
226 map
.geojsons
.push(geojson
);
229 map
.markerCluster
= L
.markerClusterGroup(map
.options
.clusterOptions
).addTo(map
);
231 /* Pour chaque points présents, on crée un marqueur */
232 jQuery
.each(data
.features
, function (i
, feature
) {
233 if (feature
.geometry
.coordinates
[0]) {
234 var marker
= L
.marker([feature
.geometry
.coordinates
[1], feature
.geometry
.coordinates
[0]]);
236 // Déclarer l'icone du point
237 map
.setGeoJsonFeatureIcon(feature
, marker
);
238 // Déclarer le contenu de la popup s'il y en a
239 map
.setGeoJsonFeaturePopup(feature
, marker
);
241 marker
.id
= feature
.id
;
242 markers
.push(marker
);
246 map
.markerCluster
.addLayers(markers
);
248 if (map
.options
.autocenterandzoom
) {
249 if (data
.features
.length
> 1)
250 map
.fitBounds(map
.markerCluster
.getBounds());
252 map
.setView(map
.markerCluster
.getBounds().getCenter(), map
.options
.zoom
);
258 addJSON: function (data
) {
259 return this.parseGeoJson(data
);
263 removeAllMarkers: function () {
264 // virer les éléments du cluster s'il est utilisé
265 if (this.options
.cluster
) {
266 this.markerCluster
.clearLayers();
268 // virer les points de la carte
269 if (typeof this.geojsons
=='undefined') this.geojsons
= [];
270 for (var i
in this.geojsons
) {
271 this.geojsons
[i
].clearLayers();
272 this.removeLayer(this.geojsons
[i
]);
277 loadData: function () {
279 if (map
.options
.affiche_points
280 && typeof(map
.options
.json_points
) !== 'undefined'
281 && map
.options
.json_points
.url
.length
) {
282 // Récupération des points à mettre sur la carte, via json externe
284 jQuery
.extend(true, args
, map
.options
.json_points
.env
);
285 if (typeof map
.options
.json_points
.objets
!== 'undefined') {
286 args
.objets
= map
.options
.json_points
.objets
;
287 if (args
.objets
== 'point_libre') {
288 args
.lat
= map
.options
.center
[0];
289 args
.lon
= map
.options
.center
[1];
290 if (typeof map
.options
.json_points
.titre
!== 'undefined')
291 args
.titre
= map
.options
.json_points
.titre
;
292 if (typeof map
.options
.json_points
.description
!== 'undefined')
293 args
.description
= map
.options
.json_points
.description
;
294 if (typeof map
.options
.json_points
.icone
!== 'undefined')
295 args
.icone
= map
.options
.json_points
.icone
;
298 if (typeof map
.options
.json_points
.limit
!== 'undefined')
299 args
.limit
= map
.options
.json_points
.limit
;
300 jQuery
.getJSON(map
.options
.json_points
.url
,args
,
303 // Charger le json (data) et déclarer les points
304 map
.parseGeoJson(data
);
305 jQuery('#'+map
._container
.id
).trigger('ready',map
);
312 addOverlays: function () {
314 if (map
.options
.kml
&& map
.options
.kml
.length
) {
316 for (var i
in map
.options
.kml
) {
317 map
.kml
[i
] = new L
.KML(map
.options
.kml
[i
], {async
: true});
318 if (map
.options
.centrer_fichier
)
319 map
.kml
[i
].on('loaded', function (e
) { map
.fitBounds(e
.target
.getBounds()); });
320 map
.addLayer(map
.kml
[i
]);
323 if (map
.options
.gpx
&& map
.options
.gpx
.length
) {
325 for (var i
in map
.options
.gpx
) {
326 map
.gpx
[i
] = new L
.GPX(map
.options
.gpx
[i
], {async
: true});
327 if (map
.options
.centrer_fichier
)
328 map
.gpx
[i
].on('loaded', function (e
) { map
.fitBounds(e
.target
.getBounds()); });
329 map
.addLayer(map
.gpx
[i
]);
332 if (map
.options
.geojson
&& map
.options
.geojson
.length
) {
333 for (var i
in map
.options
.geojson
) {
334 jQuery
.getJSON(map
.options
.geojson
[i
], function (data
) {
336 map
.parseGeoJson(data
);
340 if (map
.options
.topojson
&& map
.options
.topojson
.length
) {
341 for (var i
in map
.options
.topojson
) {
342 map
.topojson
[i
] = new L
.TOPOJSON(map
.options
.topojson
[i
], {async
: true});
343 if (map
.options
.centrer_fichier
) {
344 map
.topojson
[i
].on('loaded', function (e
) { map
.fitBounds(e
.target
.getBounds()); });
346 map
.addLayer(map
.topojson
[i
]);
352 L
.map
.gis = function (id
, options
) {
353 return new L
.Map
.Gis(id
, options
);