Projet

Général

Profil

Paste
Télécharger (10,3 ko) Statistiques
| Branche: | Révision:

root / js / utils_osm.js @ 3a832cbc

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
        zoom: typeof zoom == 'undefined' ? 10:zoom,
21
        controls:[zcontrol,
22
                  new OpenLayers.Control.KeyboardDefaults(),
23
                  new OpenLayers.Control.Navigation()],
24
    });
25

    
26
    if (typeof scale_line != 'undefined' && scale_line == true) {
27
        map.addControl(new OpenLayers.Control.ScaleLine({bottomOutUnits: ''}));
28
    }
29

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

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

    
51
        // autres tests
52
        function show_pos(e) {
53
            alert(formatLonlats(map.getLonLatFromViewPortPx(e.xy)));
54
        }
55
        function set_pos(e) {
56
            if(this.checked) {
57
                document.getElementById("position").style.display = 'none';
58
                map.events.register("click", map, show_pos);
59
            } else {
60
                document.getElementById("position").style.display = 'block';
61
                map.events.unregister("click", map, show_pos);
62
            }
63
        }
64
        var panel = new OpenLayers.Control.Panel({
65
            div: document.getElementById("panel")
66
        });
67

    
68
        function formatLonlats(lonLat) {
69
            lonLat.transform(map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326"));
70
            var lat = lonLat.lat;
71
            var lon = lonLat.lon;
72
            var dist = OpenLayers.Util.distVincenty(lonLat, new OpenLayers.LonLat(ln.lon1, ln.lat1))*1000;
73
            return lat.toFixed(5) + ', ' + lon.toFixed(5) + ' à ' + parseInt(dist) + ' mètres';
74
        }
75

    
76
        map.addControl (new OpenLayers.Control.MousePosition({
77
            div: document.getElementById("position"),
78
            formatOutput: formatLonlats
79
        }));
80

    
81
        var history = new OpenLayers.Control.NavigationHistory();
82
        map.addControl(history);
83
        panel.addControls([history.next, history.previous]);
84
        map.addControl(panel);
85

    
86
        document.getElementById("clic_pos").onchange = set_pos;
87
        layers.layersDiv.appendChild(document.getElementById("extra"));
88
        // fin des autres tests
89
    } else {
90
        map.addLayer(new OpenLayers.Layer.OSM());
91
    }
92

    
93
    if (typeof contour != 'undefined') contours = [contour];
94
    if (typeof contours == 'undefined') contours = new Array;
95
    for (var i = 0; i < contours.length; i++) {
96
        var ct = contours[i];
97
        var cntr = new OpenLayers.Layer.Vector("contour_"+i, {
98
            strategies: [new OpenLayers.Strategy.Fixed()],
99
            projection: new OpenLayers.Projection("EPSG:4326"),
100
            styleMap: new OpenLayers.StyleMap({
101
                strokeWidth:     ct.strokeWidth,
102
                strokeColor:     ct.strokeColor,
103
                strokeOpacity:   ct.strokeOpacity,
104
                fillOpacity:     ct.fillOpacity,
105
                fillColor:       ct.fillColor
106
            }),
107
            protocol: new OpenLayers.Protocol.HTTP({
108
                url: ct.url,
109
                format: new OpenLayers.Format.OSM(),
110
            }),
111
            eventListeners: {
112
                "featuresadded": function () {
113
                    if (typeof fit_contours == 'undefined' || fit_contours) this.map.zoomToExtent(this.getDataExtent());
114
                }
115
            }
116
        });
117
        map.addLayer(cntr);
118
    }
119

    
120
    if (typeof ref_line != 'undefined') ref_lines = [ref_line];
121
    if (typeof ref_lines != 'undefined') {
122
        if (typeof def_line_style == 'undefined') def_line_style = {};
123
        var def_ln = {
124
            width:       def_line_style.width? def_line_style.width:2,
125
            color:       def_line_style.color? def_line_style.color:'#00F',
126
            length:      def_line_style.length? def_line_style.length:20000,
127
            opacity:     def_line_style.opacity? def_line_style.opacity:1}
128

    
129
        var lineLayer = new OpenLayers.Layer.Vector("ref_lines");
130
        map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path));
131
        for (var i = 0; i < ref_lines.length; i++) {
132
            var ln = ref_lines[i];
133
            if(isNaN(ln.cap)) {
134
                var pt = {lon: ln.lon2, lat: ln.lat2};
135
            } else {
136
                var LonLat = new OpenLayers.LonLat(ln.lon1, ln.lat1);
137
                var dist = ln.length? ln.length:def_ln.length;
138
                var pt = OpenLayers.Util.destinationVincenty(LonLat, ln.cap, dist);
139
            }
140
            var points = new Array(
141
                new OpenLayers.Geometry.Point(ln.lon1, ln.lat1),
142
                new OpenLayers.Geometry.Point(pt.lon, pt.lat)
143
            );
144
            points[0].transform("EPSG:4326", map.getProjectionObject());
145
            points[1].transform("EPSG:4326", map.getProjectionObject());
146
            var line = new OpenLayers.Geometry.LineString(points);
147

    
148
            var style = {
149
                strokeColor:   ln.color? ln.color:def_ln.color,
150
                strokeWidth:   ln.width? ln.width:def_ln.width,
151
                strokeOpacity: ln.width? ln.opacity:def_ln.opacity
152
            };
153

    
154
            var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
155
            lineLayer.addFeatures([lineFeature]);
156
        }
157
        map.addLayer(lineLayer);
158
    }
159

    
160
    if (typeof ref_point != 'undefined') ref_points = [ref_point];
161
    if (typeof ref_points != 'undefined') {
162
        refpts_layer = new OpenLayers.Layer.Vector("ref_points", {projection: "EPSG:4326"});
163
        var selectMarkerControl = new OpenLayers.Control.SelectFeature(refpts_layer, {
164
            onSelect: function(feature) {
165
                var le_popup = new OpenLayers.Popup.FramedCloud("Popup",
166
                                                                feature.attributes.lonlat,
167
                                                                null,
168
                                                                feature.attributes.description,
169
                                                                null,
170
                                                                true);
171
                                                                //function() {selectMarkerControl.unselect(feature)});
172
                feature.popup = le_popup;
173
                map.addPopup(le_popup);
174
            },
175
            onUnselect: function(feature) {
176
                //alert(feature.id);
177
                map.removePopup(feature.popup);
178
                feature.popup.destroy();
179
                feature.popup = null;
180
            },
181
            multiple: true,
182
            toggle: true,
183
        });
184
        map.addControl(selectMarkerControl);
185

    
186
        selectMarkerControl.activate();
187
        map.addLayer(refpts_layer);
188

    
189

    
190
        if (typeof def_points_style == 'undefined') def_points_style = {};
191
        var def_pt = {
192
            icon_url:    def_points_style.icon_url,
193
            icon_width:  def_points_style.icon_width,
194
            icon_height: def_points_style.icon_height,
195
            showPopup:   def_points_style.showPopup   ? def_points_style.showPopup:false,
196
            icon_shiftX: def_points_style.icon_shiftX ? def_points_style.icon_shiftX:0,
197
            icon_shiftY: def_points_style.icon_shiftY ? def_points_style.icon_shiftY:0,
198
            opacity:     def_points_style.opacity ? def_points_style.opacity:1}
199

    
200
        for (var i = 0; i < ref_points.length; i++) {
201
            var pt = ref_points[i];
202
            var ptGeo = new OpenLayers.Geometry.Point(pt.lon, pt.lat);
203
            ptGeo.transform("EPSG:4326", map.getProjectionObject());
204
            var LonLat = new OpenLayers.LonLat(pt.lon, pt.lat).transform("EPSG:4326", map.getProjectionObject());
205
            map.setCenter(LonLat);
206
            var laFeature = new OpenLayers.Feature.Vector(ptGeo,
207
                                                          {description:pt.descr, lonlat: LonLat},
208
                                                          {externalGraphic: pt.icon_url?    pt.icon_url:def_pt.icon_url,
209
                                                           graphicWidth:    pt.icon_width?  pt.icon_width:def_pt.icon_width,
210
                                                           graphicHeight:   pt.icon_height? pt.icon_height:def_pt.icon_height,
211
                                                           graphicXOffset:  pt.icon_shiftX? pt.icon_shiftX:def_pt.icon_shiftX,
212
                                                           graphicYOffset:  pt.icon_shiftY? pt.icon_shiftY:def_pt.icon_shiftY,
213
                                                           graphicOpacity:  pt.opacity?     pt.opacity:def_pt.opacity,
214
                                                           title:           pt.title?       pt.title :''});
215
            if (i == 0) elFeature = laFeature;
216
            refpts_layer.addFeatures(laFeature);
217
            if (pt.showPopup) selectMarkerControl.select(laFeature);
218
        }
219
        if (typeof zoom == 'undefined') map.zoomToExtent(refpts_layer.getDataExtent());
220
    }
221
    if (typeof get_lon_lat != 'undefined' && get_lon_lat) {
222
        map.events.register("click", map, function(e) {
223
            var position = map.getLonLatFromViewPortPx(e.xy);
224
            position.transform(map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326"));
225
            alert(position.lat.toFixed(5) + ', ' + position.lon.toFixed(5));
226
        });
227
    }
228

    
229
        return map;
230
}
231

    
232
function list2css_color(vals) {
233
        return "rgb("+vals+")";
234
}
235

    
236
function mk_all_refpoints_layer() {
237
        // Put the same style as panorama view for points
238
        var points_style = new OpenLayers.StyleMap({
239
                pointRadius: 10,
240
                fillOpacity: 0.5,
241
        });
242
        
243
        var lookup = {};
244
        
245
        for (var k in  point_colors ) {
246
                var css_color = list2css_color(point_colors[k]);
247
                lookup[k] = {
248
                        fillColor: css_color,
249
                        strokeColor: css_color
250
                };
251
        }
252
        console.log(lookup);
253
        points_style.addUniqueValueRules("default", "type", lookup);        
254
        
255
        var layer = new OpenLayers.Layer.Vector(
256
                "Reference points",{
257
                        projection: new OpenLayers.Projection("EPSG:4326"),
258
                        strategies: [new OpenLayers.Strategy.Fixed()],
259
                        protocol: new OpenLayers.Protocol.HTTP({
260
                                url: 'ajax/all_points.php',
261
                                format: new OpenLayers.Format.GeoJSON(),
262
                        }),
263
                        styleMap: points_style
264
                });
265
        return layer;
266
}
267

    
268

    
269
function mk_point_cloud(feature) {
270
        /* Takes a ref_point or loc_point and makes a dom element to be used as map
271
         * popup
272
         */
273
        var div = document.createElement('div');
274
        var ul = document.createElement('ul');
275
        var title = feature.attributes.name;
276
        var desc, link;
277
        
278
        if (feature.attributes.type == 'pano_point') {
279
                desc = 'photo panoramique';
280
                link = feature.attributes.view_url;
281
        } else {
282
                desc = 'point de référence';
283
        }
284
        div.innerHTML = '<p><strong>'+title+'</strong></p>';
285
        ul.innerHTML = '<li>'+desc+'</li>';
286
        
287
        if (link) {
288
                ul.innerHTML += '<li><a href="'+
289
                        link+'" >Visualiser';
290
        }
291
        
292
        div.appendChild(ul);
293
        return div.innerHTML;
294
}
295

    
296
function add_refpoint_control(layer, map) {
297
        var selectControl ;
298
        selectControl = new OpenLayers.Control.SelectFeature(
299
                layer,{
300
                        onSelect:function(feature) {
301
                                var popup = new OpenLayers.Popup.FramedCloud(
302
                                        feature.attributes.name,
303
                                        feature.geometry.getBounds().getCenterLonLat(),
304
                                        null,
305
                                        mk_point_cloud(feature),
306
                                        null, true, function() {selectControl.unselect(feature);});
307
                                feature.popup = popup;
308
                                map.addPopup(popup);},
309

    
310
                        onUnselect:function(feature) {
311
                                map.removePopup(feature.popup);
312
                                feature.popup.destroy();
313
                                feature.popup = null;
314
                        }});
315
        
316
        map.addControl(selectControl);
317
        selectControl.activate();
318

    
319
}
320