Projet

Général

Profil

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

root / js / utils_osm.js @ ed5e83c5

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
        map.addControl(new OpenLayers.Control.LayerSwitcher());
29
        for (var i = 0; i < base_layers.length; i++) {
30
            map.addLayer(base_layers[i]);
31
            if (base_layers[i].type == google.maps.MapTypeId.SATELLITE) {
32
                base_layers[i].mapObject.setTilt(0);
33
            }
34
        }
35
    } else {
36
        map.addLayer(new OpenLayers.Layer.OSM());
37
    }
38

    
39
    if (typeof contour != 'undefined') contours = [contour];
40
    if (typeof contours == 'undefined') contours = new Array;
41
    for (var i = 0; i < contours.length; i++) {
42
        var ct = contours[i];
43
        var cntr = new OpenLayers.Layer.Vector("contour_"+i, {
44
            strategies: [new OpenLayers.Strategy.Fixed()],
45
            projection: new OpenLayers.Projection("EPSG:4326"),
46
            styleMap: new OpenLayers.StyleMap({
47
                strokeWidth:     ct.strokeWidth,
48
                strokeColor:     ct.strokeColor,
49
                strokeOpacity:   ct.strokeOpacity,
50
                fillOpacity:     ct.fillOpacity,
51
                fillColor:       ct.fillColor
52
            }),
53
            protocol: new OpenLayers.Protocol.HTTP({
54
                url: ct.url,
55
                format: new OpenLayers.Format.OSM(),
56
            }),
57
            eventListeners: {
58
                "featuresadded": function () {
59
                    if (typeof fit_contours == 'undefined' || fit_contours) this.map.zoomToExtent(this.getDataExtent());
60
                }
61
            }
62
        });
63
        map.addLayer(cntr);
64
    }
65

    
66
    if (typeof ref_line != 'undefined') ref_lines = [ref_line];
67
    if (typeof ref_lines != 'undefined') {
68
        if (typeof def_line_style == 'undefined') def_line_style = {};
69
        var def_ln = {
70
            width:       def_line_style.width? def_line_style.width:2,
71
            color:       def_line_style.color? def_line_style.color:'#00F',
72
            length:      def_line_style.length? def_line_style.length:20000,
73
            opacity:     def_line_style.opacity? def_line_style.opacity:1}
74
        
75
        var lineLayer = new OpenLayers.Layer.Vector("ref_lines"); 
76
        map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path));                                     
77
        for (var i = 0; i < ref_lines.length; i++) {
78
            var ln = ref_lines[i];
79
            if(isNaN(ln.cap)) {
80
                var pt = {lon: ln.lon2, lat: ln.lat2};
81
            } else {
82
                var LonLat = new OpenLayers.LonLat(ln.lon1, ln.lat1);
83
                var dist = ln.length? ln.length:def_ln.length;
84
                var pt = OpenLayers.Util.destinationVincenty(LonLat, ln.cap, dist);
85
            }
86
            var points = new Array(
87
                new OpenLayers.Geometry.Point(ln.lon1, ln.lat1),
88
                new OpenLayers.Geometry.Point(pt.lon, pt.lat)
89
            );
90
            points[0].transform("EPSG:4326", map.getProjectionObject());
91
            points[1].transform("EPSG:4326", map.getProjectionObject());
92
            var line = new OpenLayers.Geometry.LineString(points);
93

    
94
            var style = { 
95
                strokeColor:   ln.color? ln.color:def_ln.color, 
96
                strokeWidth:   ln.width? ln.width:def_ln.width,
97
                strokeOpacity: ln.width? ln.opacity:def_ln.opacity
98
            };
99

    
100
            var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
101
            lineLayer.addFeatures([lineFeature]);
102
        }
103
        map.addLayer(lineLayer);                    
104
    }
105

    
106
    if (typeof ref_point != 'undefined') ref_points = [ref_point];
107
    if (typeof ref_points != 'undefined') {
108
        refpts_layer = new OpenLayers.Layer.Vector("ref_points", {projection: "EPSG:4326"}); 
109
        var selectMarkerControl = new OpenLayers.Control.SelectFeature(refpts_layer, {
110
            onSelect: function(feature) {
111
                var le_popup = new OpenLayers.Popup.FramedCloud("Popup",
112
                                                                feature.attributes.lonlat,
113
                                                                null,
114
                                                                feature.attributes.description,
115
                                                                null,
116
                                                                true);
117
                                                                //function() {selectMarkerControl.unselect(feature)});
118
                feature.popup = le_popup;
119
                map.addPopup(le_popup);
120
            },
121
            onUnselect: function(feature) {
122
                //alert(feature.id);
123
                map.removePopup(feature.popup);
124
                feature.popup.destroy();
125
                feature.popup = null;
126
            },
127
            multiple: true,
128
            toggle: true,
129
        });
130
        map.addControl(selectMarkerControl);
131
        
132
        selectMarkerControl.activate();
133
        map.addLayer(refpts_layer);
134
        
135

    
136
        if (typeof def_points_style == 'undefined') def_points_style = {};
137
        var def_pt = {
138
            icon_url:    def_points_style.icon_url,
139
            icon_width:  def_points_style.icon_width,
140
            icon_height: def_points_style.icon_height,
141
            showPopup:   def_points_style.showPopup   ? def_points_style.showPopup:false,
142
            icon_shiftX: def_points_style.icon_shiftX ? def_points_style.icon_shiftX:0,
143
            icon_shiftY: def_points_style.icon_shiftY ? def_points_style.icon_shiftY:0,
144
            opacity:     def_points_style.opacity ? def_points_style.opacity:1}
145
        
146
        for (var i = 0; i < ref_points.length; i++) {
147
            var pt = ref_points[i];
148
            var ptGeo = new OpenLayers.Geometry.Point(pt.lon, pt.lat);
149
            ptGeo.transform("EPSG:4326", map.getProjectionObject());
150
            var LonLat = new OpenLayers.LonLat(pt.lon, pt.lat).transform("EPSG:4326", map.getProjectionObject());
151
            map.setCenter(LonLat);
152
            var laFeature = new OpenLayers.Feature.Vector(ptGeo,
153
                                                          {description:pt.descr, lonlat: LonLat},
154
                                                          {externalGraphic: pt.icon_url?    pt.icon_url:def_pt.icon_url,
155
                                                           graphicWidth:    pt.icon_width?  pt.icon_width:def_pt.icon_width,
156
                                                           graphicHeight:   pt.icon_height? pt.icon_height:def_pt.icon_height,
157
                                                           graphicXOffset:  pt.icon_shiftX? pt.icon_shiftX:def_pt.icon_shiftX,
158
                                                           graphicYOffset:  pt.icon_shiftY? pt.icon_shiftY:def_pt.icon_shiftY,
159
                                                           graphicOpacity:  pt.opacity?     pt.opacity:def_pt.opacity,
160
                                                           title:           pt.title?       pt.title :''});
161
            if (i == 0) elFeature = laFeature;
162
            refpts_layer.addFeatures(laFeature);
163
            if (pt.showPopup) selectMarkerControl.select(laFeature);
164
        }
165
        if (typeof zoom == 'undefined') map.zoomToExtent(refpts_layer.getDataExtent());
166
    }
167
    if (typeof get_lon_lat != 'undefined' && get_lon_lat) {
168
        map.events.register("click", map, function(e) {
169
            var position = map.getLonLatFromViewPortPx(e.xy);
170
            position.transform(map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326"));
171
            alert(position.lat.toFixed(5) + ', ' + position.lon.toFixed(5));
172
        });
173
    }
174
}
175

    
176
if (typeof addLoadEvent == 'function') addLoadEvent(draw_map);
177
else window.onload = draw_map;