Projet

Général

Profil

Paste
Télécharger (12,7 ko) Statistiques
| Branche: | Révision:

root / js / utils_osm.js @ e2b1b9c1

1

    
2
/** Draws the map for the "view this cap" feature
3
 */
4
function draw_cap_map(zoom) {
5

    
6
    var zcontrol;
7
    if (typeof zoom_control != 'undefined') {
8
        switch (zoom_control) {
9
        case 'full':
10
            zcontrol = new OpenLayers.Control.PanZoomBar();
11
            break;
12
        case 'light':
13
        default:
14
            zcontrol = new OpenLayers.Control.Zoom();
15
        }
16
    } else zcontrol = new OpenLayers.Control.Zoom();
17

    
18
    var map = new OpenLayers.Map({
19
            div: "map",
20
            theme: null,
21
            zoom: typeof zoom == 'undefined' ? 10:zoom,
22
        controls:[zcontrol,
23
                  new OpenLayers.Control.KeyboardDefaults(),
24
                  new OpenLayers.Control.Navigation()],
25
    });
26
        
27
        
28
    if (typeof scale_line != 'undefined' && scale_line == true) {
29
        map.addControl(new OpenLayers.Control.ScaleLine({bottomOutUnits: ''}));
30
    }
31
        map.celutz_addnew = false;
32

    
33
    if (typeof base_layers != 'undefined') {
34
        var layers = new OpenLayers.Control.LayerSwitcher();
35
        map.addControl(layers);
36
        for (var i = 0; i < base_layers.length; i++) {
37
            map.addLayer(base_layers[i]);
38
        }
39

    
40
        // gestion du 45° google //
41
        function update_tilt() {
42
            for (var i = 0; i < base_layers.length; i++) {
43
                if (base_layers[i].type == google.maps.MapTypeId.SATELLITE) {
44
                    base_layers[i].mapObject.setTilt(this.checked?1:0);
45
                    //alert((chkbx.checked?1:0)+'//'+i);
46
                    base_layers[i].removeMap;
47
                    base_layers[i].redraw;
48
                }
49
            }
50
        }
51
        document.getElementById("tilt").onchange = update_tilt;
52
        // fin de gestion du 45° google //
53

    
54
        // autres tests
55
        function show_pos(e) {
56
            alert(formatLonlats(map.getLonLatFromViewPortPx(e.xy)));
57
        }
58
        function set_pos(e) {
59
            if(this.checked) {
60
                document.getElementById("position").style.display = 'none';
61
                map.events.register("click", map, show_pos);
62
            } else {
63
                document.getElementById("position").style.display = 'block';
64
                map.events.unregister("click", map, show_pos);
65
            }
66
        }
67
            
68
            
69
    var bt_pano = mk_new_pano_button(map);
70
        var panel = new OpenLayers.Control.Panel({
71
                div: document.getElementById("panel"),
72
            default: bt_pano,
73
        createControlMarkup: function(control) {
74
            var button = document.createElement('button'),
75
                iconSpan = document.createElement('span'),
76
                textSpan = document.createElement('span');
77
            iconSpan.innerHTML = '&nbsp;';
78
            button.appendChild(iconSpan);
79
            if (control.text) {
80
                textSpan.innerHTML = control.text;
81
            }
82
            button.appendChild(textSpan);
83
            return button;
84
        }
85
    });
86

    
87
        function formatLonlats(lonLat) {
88
            lonLat.transform(map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326"));
89
            var lat = lonLat.lat;
90
            var lon = lonLat.lon;
91
            var dist = OpenLayers.Util.distVincenty(lonLat, new OpenLayers.LonLat(ln.lon1, ln.lat1))*1000;
92
            return lat.toFixed(5) + ', ' + lon.toFixed(5) + ' à ' + parseInt(dist) + ' mètres';
93
        }
94

    
95
        map.addControl (new OpenLayers.Control.MousePosition({
96
            div: document.getElementById("position"),
97
            formatOutput: formatLonlats
98
        }));
99

    
100
    var history = new OpenLayers.Control.NavigationHistory({
101
            previousOptions: {
102
                    title: "Go to previous map position",
103
                    text: "Préc."
104
            },
105
            nextOptions: {
106
                    title: "Go to next map position",
107
                    text: "Suiv."
108
            },
109
    });
110
            
111
        map.addControl(history);
112
            panel.addControls([bt_pano, history.next, history.previous]);
113
        map.addControl(panel);
114

    
115
        document.getElementById("clic_pos").onchange = set_pos;
116
        layers.layersDiv.appendChild(document.getElementById("extra"));
117
        // fin des autres tests
118
    } else {
119
        map.addLayer(new OpenLayers.Layer.OSM());
120
    }
121

    
122
    if (typeof contour != 'undefined') contours = [contour];
123
    if (typeof contours == 'undefined') contours = new Array;
124
    for (var i = 0; i < contours.length; i++) {
125
        var ct = contours[i];
126
        var cntr = new OpenLayers.Layer.Vector("contour_"+i, {
127
            strategies: [new OpenLayers.Strategy.Fixed()],
128
            projection: new OpenLayers.Projection("EPSG:4326"),
129
            styleMap: new OpenLayers.StyleMap({
130
                strokeWidth:     ct.strokeWidth,
131
                strokeColor:     ct.strokeColor,
132
                strokeOpacity:   ct.strokeOpacity,
133
                fillOpacity:     ct.fillOpacity,
134
                fillColor:       ct.fillColor
135
            }),
136
            protocol: new OpenLayers.Protocol.HTTP({
137
                url: ct.url,
138
                format: new OpenLayers.Format.OSM(),
139
            }),
140
            eventListeners: {
141
                "featuresadded": function () {
142
                    if (typeof fit_contours == 'undefined' || fit_contours) this.map.zoomToExtent(this.getDataExtent());
143
                }
144
            }
145
        });
146
        map.addLayer(cntr);
147
    }
148

    
149
    if (typeof ref_line != 'undefined') ref_lines = [ref_line];
150
    if (typeof ref_lines != 'undefined') {
151
        if (typeof def_line_style == 'undefined') def_line_style = {};
152
        var def_ln = {
153
            width:       def_line_style.width? def_line_style.width:2,
154
            color:       def_line_style.color? def_line_style.color:'#00F',
155
            length:      def_line_style.length? def_line_style.length:20000,
156
            opacity:     def_line_style.opacity? def_line_style.opacity:1}
157

    
158
        var lineLayer = new OpenLayers.Layer.Vector("ref_lines");
159
        map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path));
160
        for (var i = 0; i < ref_lines.length; i++) {
161
            var ln = ref_lines[i];
162
            if(isNaN(ln.cap)) {
163
                var pt = {lon: ln.lon2, lat: ln.lat2};
164
            } else {
165
                var LonLat = new OpenLayers.LonLat(ln.lon1, ln.lat1);
166
                var dist = ln.length? ln.length:def_ln.length;
167
                var pt = OpenLayers.Util.destinationVincenty(LonLat, ln.cap, dist);
168
            }
169
            var points = new Array(
170
                new OpenLayers.Geometry.Point(ln.lon1, ln.lat1),
171
                new OpenLayers.Geometry.Point(pt.lon, pt.lat)
172
            );
173
            points[0].transform("EPSG:4326", map.getProjectionObject());
174
            points[1].transform("EPSG:4326", map.getProjectionObject());
175
            var line = new OpenLayers.Geometry.LineString(points);
176

    
177
            var style = {
178
                strokeColor:   ln.color? ln.color:def_ln.color,
179
                strokeWidth:   ln.width? ln.width:def_ln.width,
180
                strokeOpacity: ln.width? ln.opacity:def_ln.opacity
181
            };
182

    
183
            var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
184
            lineLayer.addFeatures([lineFeature]);
185
        }
186
        map.addLayer(lineLayer);
187
    }
188

    
189
    if (typeof ref_point != 'undefined') ref_points = [ref_point];
190
    if (typeof ref_points != 'undefined') {
191
        refpts_layer = new OpenLayers.Layer.Vector("ref_points", {projection: "EPSG:4326"});
192
        var selectMarkerControl = new OpenLayers.Control.SelectFeature(refpts_layer, {
193
            onSelect: function(feature) {
194
                var le_popup = new OpenLayers.Popup.FramedCloud("Popup",
195
                                                                feature.attributes.lonlat,
196
                                                                null,
197
                                                                feature.attributes.description,
198
                                                                null,
199
                                                                true);
200
                                                                //function() {selectMarkerControl.unselect(feature)});
201
                feature.popup = le_popup;
202
                map.addPopup(le_popup);
203
            },
204
            onUnselect: function(feature) {
205
                //alert(feature.id);
206
                map.removePopup(feature.popup);
207
                feature.popup.destroy();
208
                feature.popup = null;
209
            },
210
            multiple: true,
211
            toggle: true,
212
        });
213
        map.addControl(selectMarkerControl);
214

    
215
        selectMarkerControl.activate();
216
        map.addLayer(refpts_layer);
217

    
218

    
219
        if (typeof def_points_style == 'undefined') def_points_style = {};
220
        var def_pt = {
221
            icon_url:    def_points_style.icon_url,
222
            icon_width:  def_points_style.icon_width,
223
            icon_height: def_points_style.icon_height,
224
            showPopup:   def_points_style.showPopup   ? def_points_style.showPopup:false,
225
            icon_shiftX: def_points_style.icon_shiftX ? def_points_style.icon_shiftX:0,
226
            icon_shiftY: def_points_style.icon_shiftY ? def_points_style.icon_shiftY:0,
227
            opacity:     def_points_style.opacity ? def_points_style.opacity:1}
228

    
229
        for (var i = 0; i < ref_points.length; i++) {
230
            var pt = ref_points[i];
231
            var ptGeo = new OpenLayers.Geometry.Point(pt.lon, pt.lat);
232
            ptGeo.transform("EPSG:4326", map.getProjectionObject());
233
            var LonLat = new OpenLayers.LonLat(pt.lon, pt.lat).transform("EPSG:4326", map.getProjectionObject());
234
            map.setCenter(LonLat);
235
            var laFeature = new OpenLayers.Feature.Vector(ptGeo,
236
                                                          {description:pt.descr, lonlat: LonLat},
237
                                                          {externalGraphic: pt.icon_url?    pt.icon_url:def_pt.icon_url,
238
                                                           graphicWidth:    pt.icon_width?  pt.icon_width:def_pt.icon_width,
239
                                                           graphicHeight:   pt.icon_height? pt.icon_height:def_pt.icon_height,
240
                                                           graphicXOffset:  pt.icon_shiftX? pt.icon_shiftX:def_pt.icon_shiftX,
241
                                                           graphicYOffset:  pt.icon_shiftY? pt.icon_shiftY:def_pt.icon_shiftY,
242
                                                           graphicOpacity:  pt.opacity?     pt.opacity:def_pt.opacity,
243
                                                           title:           pt.title?       pt.title :''});
244
            if (i == 0) elFeature = laFeature;
245
            refpts_layer.addFeatures(laFeature);
246
            if (pt.showPopup) selectMarkerControl.select(laFeature);
247
        }
248
        if (typeof zoom == 'undefined') map.zoomToExtent(refpts_layer.getDataExtent());
249
    }
250
    if (typeof get_lon_lat != 'undefined' && get_lon_lat) {
251
        map.events.register("click", map, function(e) {
252
            var position = map.getLonLatFromViewPortPx(e.xy);
253
            position.transform(map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326"));
254
            alert(position.lat.toFixed(5) + ', ' + position.lon.toFixed(5));
255
        });
256
    }
257

    
258
        return map;
259
}
260

    
261
function list2css_color(vals) {
262
        return "rgb("+vals+")";
263
}
264

    
265
function mk_all_refpoints_layer() {
266
        // Put the same style as panorama view for points
267
        var points_style = new OpenLayers.StyleMap({
268
                pointRadius: 10,
269
                fillOpacity: 0.5,
270
        });
271
        
272
        var lookup = {};
273
        
274
        for (var k in  point_colors ) {
275
                var css_color = list2css_color(point_colors[k]);
276
                lookup[k] = {
277
                        fillColor: css_color,
278
                        strokeColor: css_color
279
                };
280
        }
281
        console.log(lookup);
282
        points_style.addUniqueValueRules("default", "type", lookup);        
283
        
284
        var layer = new OpenLayers.Layer.Vector(
285
                "Reference points",{
286
                        projection: new OpenLayers.Projection("EPSG:4326"),
287
                        strategies: [new OpenLayers.Strategy.Fixed()],
288
                        protocol: new OpenLayers.Protocol.HTTP({
289
                                url: 'ajax/all_points.php',
290
                                format: new OpenLayers.Format.GeoJSON(),
291
                        }),
292
                        styleMap: points_style
293
                });
294
        return layer;
295
}
296

    
297

    
298
function mk_point_cloud(feature) {
299
        /* Takes a ref_point or loc_point and makes a dom element to be used as map
300
         * popup
301
         */
302
        var div = document.createElement('div');
303
        var ul = document.createElement('ul');
304
        var title = feature.attributes.name;
305
        var desc, link;
306
        
307
        if (feature.attributes.type == 'pano_point') {
308
                desc = 'photo panoramique';
309
                link = feature.attributes.view_url;
310
        } else {
311
                desc = 'point de référence';
312
        }
313
        div.innerHTML = '<p><strong>'+title+'</strong></p>';
314
        ul.innerHTML = '<li>'+desc+'</li>';
315
        
316
        if (link) {
317
                ul.innerHTML += '<li><a href="'+
318
                        link+'" >Visualiser';
319
        }
320
        
321
        div.appendChild(ul);
322
        return div.innerHTML;
323
}
324

    
325
function add_refpoint_control(layer, map) {
326
        var selectControl ;
327
        selectControl = new OpenLayers.Control.SelectFeature(
328
                layer,{
329
                        onSelect:function(feature) {
330
                                var popup = new OpenLayers.Popup.FramedCloud(
331
                                        feature.attributes.name,
332
                                        feature.geometry.getBounds().getCenterLonLat(),
333
                                        null,
334
                                        mk_point_cloud(feature),
335
                                        null, true, function() {selectControl.unselect(feature);});
336
                                feature.popup = popup;
337
                                map.addPopup(popup);},
338

    
339
                        onUnselect:function(feature) {
340
                                map.removePopup(feature.popup);
341
                                feature.popup.destroy();
342
                                feature.popup = null;
343
                        }});
344
        
345
        map.addControl(selectControl);
346
        selectControl.activate();
347
}
348

    
349

    
350
function mk_new_pano_button(map) {
351
        var btn = new OpenLayers.Control.Button({
352
                displayClass: 'olControlBtnNewPano',
353
                title: "Ajouter un nouveau panorama",
354
                text: "Photo",
355
                id: 'btn-new-pano',
356
                autoActivate: true,
357
                trigger: function() {start_add_new_pano(map)},
358
        });        
359
        return btn;
360
}
361

    
362

    
363
function add_pano_click_handler(callback) {
364
        var ctrlClass = OpenLayers.Class(OpenLayers.Control, {                
365
                defaultHandlerOptions: {
366
                    'single': false,
367
                    'double': true,
368
                    'pixelTolerance': 0,
369
                    'stopSingle': false,
370
                    'stopDouble': false
371
                },
372

    
373
                initialize: function(options)
374
                {
375
                    this.handlerOptions = OpenLayers.Util.extend(
376
                        {}, this.defaultHandlerOptions
377
                    );
378
                    
379
                    OpenLayers.Control.prototype.initialize.apply(this, arguments);
380
                    
381
                    this.handler = new OpenLayers.Handler.Click(
382
                        this, {
383
                            'click': callback
384
                        }, this.handlerOptions
385
                    );
386
                },
387
        });
388
        
389
        var control = new ctrlClass({
390
                        handlerOptions: {
391
                            "single": true,
392
                            "double": false
393
                        },
394
                autoActivate: true});
395
        
396
        return control;
397
}
398

    
399
function start_add_new_pano(map) {
400
        map.celutz_addnew = true;
401
        alert("cliquer sur un point de la carte pour choisir l'emplacement");
402
        document.body.style.cursor = 'crosshair';
403
        
404
        var control = add_pano_click_handler(function(evt) {
405
                var coord = map.getLonLatFromPixel(evt.xy);
406
                coord.transform(new OpenLayers.Projection("EPSG:900913"), 
407
                                new OpenLayers.Projection("EPSG:4326"));
408
                window.location = 'envoyer.php?lat='+coord.lat+'&lon='+coord.lon;
409
        });
410
        map.addControl(control);
411
}
412