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,
50 initialize: function (id
,options
) {
51 L
.Util
.setOptions(this, options
);
53 this.on('load',function(e
){
54 // Affecter sur l'objet du DOM
55 jQuery("#"+this._container
.id
).get(0).map
= this;
56 // Appeler l'éventuelle fonction de callback
57 if (this.options
.callback
&& typeof(this.options
.callback
) === "function")
58 this.options
.callback(this);
59 // trigger load sur l'objet du DOM
60 jQuery("#"+this._container
.id
).trigger('load',this);
63 L
.Map
.prototype.initialize
.call(this, id
, options
);
65 if (this.options
.utiliser_bb
){
68 [this.options
.sw_lat
, this.options
.sw_lon
],
69 [this.options
.ne_lat
, this.options
.ne_lon
]
74 this.populateTileLayers(this.options
.affiche_layers
);
82 if (this.options
.localize_visitor
){
83 var maxZoom
= this.options
.localize_visitor_zoom
;
84 this.on('locationerror',function(e
){
85 maxZoom
= this.options
.zoom
;
88 this.locate({setView
: true, maxZoom
: maxZoom
});
91 // Si pas de points affichés trigger ready ici
92 if (!this.options
.affiche_points
|| !this.options
.json_points
.length
)
93 jQuery("#"+this._container
.id
).trigger('ready',this);
96 populateTileLayers: function (tilelayers
) {
97 // Fond de carte par défaut
98 var default_layer
= this.createTileLayer(this.options
.default_layer
);
99 this.addLayer(default_layer
);
100 // Fonds de carte supplémentaires
101 if (this.options
.layersControl
&& !this.options
.noControl
&& this.options
.affiche_layers
.length
>1){
102 var layers_control
= L
.control
.layers('','',this.options
.layersControlOptions
);
103 layers_control
.addBaseLayer(default_layer
,this.options
.gis_layers
[this.options
.default_layer
].nom
);
104 for(var l
in this.options
.affiche_layers
){
105 if (this.options
.affiche_layers
[l
]!==this.options
.default_layer
){
106 var layer
= this.createTileLayer(this.options
.affiche_layers
[l
]);
107 if (typeof layer
!=="undefined")
108 layers_control
.addBaseLayer(layer
,this.options
.gis_layers
[this.options
.affiche_layers
[l
]].nom
);
111 this.addControl(layers_control
);
112 // Ajouter l'objet du controle de layers à la carte pour permettre d'y accéder depuis le callback
113 this.layersControl
= layers_control
;
114 // Classe noajax sur le layer_control pour éviter l'ajout de hidden par SPIP
115 L
.DomUtil
.addClass(layers_control
._form
,'noajax');
119 initControls: function () {
120 this.attributionControl
.setPrefix('');
121 if (this.options
.scaleControl
)
122 L
.control
.scale().addTo(this);
123 if (this.options
.overviewControl
){
124 // todo ajouter une option pour permettre de choisir la couche à afficher dans la minimap
125 var minimap_layer
= this.createTileLayer(this.options
.default_layer
);
126 L
.control
.minimap(minimap_layer
,{width
: 100,height
: 100, toggleDisplay
: true}).addTo(this);
130 createTileLayer: function (name
) {
132 if (typeof this.options
.gis_layers
[name
]!=="undefined")
133 eval("layer=new "+ this.options
.gis_layers
[name
].layer
+";");
137 // API setGeoJsonFeatureIcon : Pour Ajouter l'icone d'un point (feature = item d'un GeoJson)
138 setGeoJsonFeatureIcon: function (feature
, layer
) {
139 // Déclarer l'icone du points, si défini
140 if (feature
.properties
&& feature
.properties
.icon
){
142 'iconUrl': feature
.properties
.icon
,
143 'iconSize': [feature
.properties
.icon_size
[0], feature
.properties
.icon_size
[1]],
144 'iconAnchor': [feature
.properties
.icon_anchor
[0], feature
.properties
.icon_anchor
[1]]
146 if (feature
.properties
.popup_anchor
)
147 icon_options
.popupAnchor
= [feature
.properties
.popup_anchor
[0], feature
.properties
.popup_anchor
[1]];
148 if (feature
.properties
.shadow
)
149 icon_options
.shadowUrl
= feature
.properties
.shadow
;
150 if (feature
.properties
.shadow_size
)
151 icon_options
.shadowSize
= [feature
.properties
.shadow_size
[0], feature
.properties
.shadow_size
[1]];
152 layer
.setIcon(L
.icon(icon_options
));
156 // API setGeoJsonFeaturePopup : Pour Ajouter le texte de popup d'un point (feature = item d'un GeoJson)
157 setGeoJsonFeaturePopup: function (feature
, layer
) {
158 // Déclarer le contenu de la popup s'il y en a
159 if (feature
.properties
&& (feature
.properties
.title
|| feature
.properties
.description
)){
160 var popupContent
= '';
161 var popupOptions
= '';
162 if (feature
.properties
.title
)
163 popupContent
= '<strong class="title">' + feature
.properties
.title
+ '</strong>';
164 if (feature
.properties
.description
)
165 popupContent
= popupContent
+ feature
.properties
.description
;
166 if (feature
.properties
.popup_options
)
167 popupOptions
= feature
.properties
.popup_options
;
168 layer
.bindPopup(popupContent
,popupOptions
);
173 parseGeoJson: function(data
) {
175 // Analyse des points et déclaration (sans regroupement des points en cluster)
176 if (!map
.options
.cluster
){
177 if (data
.features
.length
> 0){
178 var geojson
= L
.geoJson('', {
179 style
: this.options
.pathStyles
,
180 onEachFeature: function (feature
, layer
) {
181 // Déclarer l'icone du point
182 map
.setGeoJsonFeatureIcon(feature
, layer
);
183 // Déclarer le contenu de la popup s'il y en a
184 map
.setGeoJsonFeaturePopup(feature
, layer
);
186 }).addData(data
).addTo(map
);
188 if (map
.options
.autocenterandzoom
){
189 if (data
.features
.length
== 1 && data
.features
[0].geometry
.type
== 'Point')
190 map
.setView(geojson
.getBounds().getCenter(), map
.options
.zoom
);
192 map
.fitBounds(geojson
.getBounds());
194 if (map
.options
.openId
)
195 gis_focus_marker(map
.options
.openId
,map
.options
.mapId
);
197 if (typeof map
.geojsons
=="undefined") map
.geojsons
= [];
198 map
.geojsons
.push(geojson
);
201 map
.markers
= L
.markerClusterGroup(map
.options
.clusterOptions
);
203 /* Pour chaque points présents, on crée un marqueur */
204 jQuery
.each(data
.features
, function(i
, feature
){
205 if (feature
.geometry
.coordinates
[0]){
206 var marker
= L
.marker([feature
.geometry
.coordinates
[1], feature
.geometry
.coordinates
[0]]);
208 // Déclarer l'icone du point
209 map
.setGeoJsonFeatureIcon(feature
, marker
);
210 // Déclarer le contenu de la popup s'il y en a
211 map
.setGeoJsonFeaturePopup(feature
, marker
);
213 marker
.id
= feature
.id
;
214 map
.markers
.addLayer(marker
);
218 map
.addLayer(map
.markers
);
220 if (map
.options
.autocenterandzoom
){
221 if (data
.features
.length
> 1)
222 map
.fitBounds(map
.markers
.getBounds());
224 map
.setView(map
.markers
.getBounds().getCenter(), map
.options
.zoom
);
230 addJSON: function(data
) {
231 return this.parseGeoJson(data
);
235 removeAllMarkers: function(){
236 if (typeof this.geojsons
=="undefined") this.geojsons
= [];
237 for(var i
in this.geojsons
){
238 this.geojsons
[i
].clearLayers();
239 this.removeLayer(this.geojsons
[i
]);
244 loadData: function () {
246 if (map
.options
.affiche_points
247 && typeof(map
.options
.json_points
) !== "undefined"
248 && map
.options
.json_points
.url
.length
){
249 // Récupération des points à mettre sur la carte, via json externe
251 jQuery
.extend(true, args
, map
.options
.json_points
.env
);
252 if (typeof map
.options
.json_points
.objets
!== "undefined"){
253 args
.objets
= map
.options
.json_points
.objets
;
254 if (args
.objets
== "point_libre"){
255 args
.lat
= map
.options
.center
[0];
256 args
.lon
= map
.options
.center
[1];
257 if (typeof map
.options
.json_points
.titre
!== "undefined")
258 args
.titre
= map
.options
.json_points
.titre
;
259 if (typeof map
.options
.json_points
.description
!== "undefined")
260 args
.description
= map
.options
.json_points
.description
;
261 if (typeof map
.options
.json_points
.icone
!== "undefined")
262 args
.icone
= map
.options
.json_points
.icone
;
265 if (typeof map
.options
.json_points
.limit
!== "undefined")
266 args
.limit
= map
.options
.json_points
.limit
;
267 jQuery
.getJSON(map
.options
.json_points
.url
,args
,
270 // Charger le json (data) et déclarer les points
271 map
.parseGeoJson(data
);
272 jQuery("#"+map
._container
.id
).trigger('ready',map
);
279 addOverlays: function () {
281 if (map
.options
.kml
&& map
.options
.kml
.length
){
283 for(var i
in map
.options
.kml
){
284 map
.kml
[i
] = new L
.KML(map
.options
.kml
[i
], {async
: true});
285 if (map
.options
.centrer_fichier
)
286 map
.kml
[i
].on("loaded", function(e
) { map
.fitBounds(e
.target
.getBounds()); });
287 map
.addLayer(map
.kml
[i
]);
290 if (map
.options
.gpx
&& map
.options
.gpx
.length
){
292 for(var i
in map
.options
.gpx
){
293 map
.gpx
[i
] = new L
.GPX(map
.options
.gpx
[i
], {async
: true});
294 if (map
.options
.centrer_fichier
)
295 map
.gpx
[i
].on("loaded", function(e
) { map
.fitBounds(e
.target
.getBounds()); });
296 map
.addLayer(map
.gpx
[i
]);
299 if (map
.options
.geojson
&& map
.options
.geojson
.length
){
300 for(var i
in map
.options
.geojson
){
301 jQuery
.getJSON(map
.options
.geojson
[i
], function(data
){
303 map
.parseGeoJson(data
);
310 L
.map
.gis = function (id
, options
) {
311 return new L
.Map
.Gis(id
, options
);