Projet

Général

Profil

Paste
Télécharger (8,27 ko) Statistiques
| Branche: | Révision:

root / js / utils_osm.js @ 1b9439c2

1
function draw_map() {
2

    
3
    var zcontrol;
4
    if (typeof zoom_control != 'undefined') {
5
        switch (zoom_control) {
6
        case 'full':
7
            zcontrol = new OpenLayers.Control.PanZoomBar();
8
            break;
9
        case 'light':
10
        default:
11
            zcontrol = new OpenLayers.Control.Zoom();
12
        }
13
    } else zcontrol = new OpenLayers.Control.Zoom();
14
    
15
    var map = new OpenLayers.Map({
16
        div: "map",
17
        zoom: typeof zoom == 'undefined' ? 10:zoom,
18
        controls:[zcontrol,
19
                  new OpenLayers.Control.KeyboardDefaults(),
20
                  new OpenLayers.Control.Navigation()],
21
    });
22
    
23
    if (typeof scale_line != 'undefined' && scale_line == true) {
24
        map.addControl(new OpenLayers.Control.ScaleLine({bottomOutUnits: ''}));
25
    }
26
    
27
    if (typeof base_layers != 'undefined') {
28
        var layers = new OpenLayers.Control.LayerSwitcher();
29
        map.addControl(layers);
30
        for (var i = 0; i < base_layers.length; i++) {
31
            map.addLayer(base_layers[i]);
32
        }
33
        
34
        // gestion du 45° google //
35
        function update_tilt() {
36
            for (var i = 0; i < base_layers.length; i++) {
37
                if (base_layers[i].type == google.maps.MapTypeId.SATELLITE) {
38
                    base_layers[i].mapObject.setTilt(this.checked?1:0);
39
                    //alert((chkbx.checked?1:0)+'//'+i);
40
                    base_layers[i].removeMap;
41
                    base_layers[i].redraw;
42
                }
43
            }
44
        }
45
        document.getElementById("tilt").onchange = update_tilt;
46
        // fin de gestion du 45° google //
47
        
48
        // autres tests
49
        function show_pos(e) {
50
            alert(formatLonlats(map.getLonLatFromViewPortPx(e.xy)));
51
        }
52
        function set_pos(e) {
53
            if(this.checked) {
54
                document.getElementById("position").style.display = 'none';
55
                map.events.register("click", map, show_pos);
56
            } else {
57
                document.getElementById("position").style.display = 'block';
58
                map.events.unregister("click", map, show_pos);
59
            }
60
        }
61
        var panel = new OpenLayers.Control.Panel({
62
            div: document.getElementById("panel")
63
        });
64
        
65
        function formatLonlats(lonLat) {
66
            lonLat.transform(map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326"));
67
            var lat = lonLat.lat;
68
            var lon = lonLat.lon;
69
            var dist = OpenLayers.Util.distVincenty(lonLat, new OpenLayers.LonLat(ln.lon1, ln.lat1))*1000;
70
            return lat.toFixed(5) + ', ' + lon.toFixed(5) + ' à ' + parseInt(dist) + ' mètres';
71
        }
72
        
73
        map.addControl (new OpenLayers.Control.MousePosition({
74
            div: document.getElementById("position"),
75
            formatOutput: formatLonlats
76
        }));
77

    
78
        var history = new OpenLayers.Control.NavigationHistory();
79
        map.addControl(history);
80
        panel.addControls([history.next, history.previous]);
81
        map.addControl(panel);
82

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

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

    
117
    if (typeof ref_line != 'undefined') ref_lines = [ref_line];
118
    if (typeof ref_lines != 'undefined') {
119
        if (typeof def_line_style == 'undefined') def_line_style = {};
120
        var def_ln = {
121
            width:       def_line_style.width? def_line_style.width:2,
122
            color:       def_line_style.color? def_line_style.color:'#00F',
123
            length:      def_line_style.length? def_line_style.length:20000,
124
            opacity:     def_line_style.opacity? def_line_style.opacity:1}
125
        
126
        var lineLayer = new OpenLayers.Layer.Vector("ref_lines"); 
127
        map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path));                                     
128
        for (var i = 0; i < ref_lines.length; i++) {
129
            var ln = ref_lines[i];
130
            if(isNaN(ln.cap)) {
131
                var pt = {lon: ln.lon2, lat: ln.lat2};
132
            } else {
133
                var LonLat = new OpenLayers.LonLat(ln.lon1, ln.lat1);
134
                var dist = ln.length? ln.length:def_ln.length;
135
                var pt = OpenLayers.Util.destinationVincenty(LonLat, ln.cap, dist);
136
            }
137
            var points = new Array(
138
                new OpenLayers.Geometry.Point(ln.lon1, ln.lat1),
139
                new OpenLayers.Geometry.Point(pt.lon, pt.lat)
140
            );
141
            points[0].transform("EPSG:4326", map.getProjectionObject());
142
            points[1].transform("EPSG:4326", map.getProjectionObject());
143
            var line = new OpenLayers.Geometry.LineString(points);
144

    
145
            var style = { 
146
                strokeColor:   ln.color? ln.color:def_ln.color, 
147
                strokeWidth:   ln.width? ln.width:def_ln.width,
148
                strokeOpacity: ln.width? ln.opacity:def_ln.opacity
149
            };
150

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

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

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

    
227
if (typeof addLoadEvent == 'function') addLoadEvent(draw_map);
228
else window.onload = draw_map;