Projet

Général

Profil

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

root / js / utils_osm.js @ f524783f

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
    map.addLayer(new OpenLayers.Layer.OSM());
28

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

    
56
    if (typeof ref_point != 'undefined') ref_points = [ref_point];
57
    if (typeof ref_points != 'undefined') {
58
        refpts_layer = new OpenLayers.Layer.Vector("ref_points", {projection: "EPSG:4326"}); 
59
        var selectMarkerControl = new OpenLayers.Control.SelectFeature(refpts_layer, {
60
            onSelect: function(feature) {
61
                var le_popup = new OpenLayers.Popup.FramedCloud("Popup",
62
                                                                feature.attributes.lonlat,
63
                                                                null,
64
                                                                feature.attributes.description,
65
                                                                null,
66
                                                                true);
67
                                                                //function() {selectMarkerControl.unselect(feature)});
68
                feature.popup = le_popup;
69
                map.addPopup(le_popup);
70
            },
71
            onUnselect: function(feature) {
72
                //alert(feature.id);
73
                map.removePopup(feature.popup);
74
                feature.popup.destroy();
75
                feature.popup = null;
76
            },
77
            multiple: true,
78
            toggle: true,
79
        });
80
        map.addControl(selectMarkerControl);
81
        
82
        selectMarkerControl.activate();
83
        map.addLayer(refpts_layer);
84
        
85

    
86
        if (typeof def_points_style == 'undefined') def_points_style = {};
87
        var def_pt = {
88
            icon_url:    def_points_style.icon_url,
89
            icon_width:  def_points_style.icon_width,
90
            icon_height: def_points_style.icon_height,
91
            showPopup:   def_points_style.showPopup   ? def_points_style.showPopup:false,
92
            icon_shiftX: def_points_style.icon_shiftX ? def_points_style.icon_shiftX:0,
93
            icon_shiftY: def_points_style.icon_shiftY ? def_points_style.icon_shiftY:0,
94
            opacity:     def_points_style.opacity ? def_points_style.opacity:1}
95
        
96
        for (var i = 0; i < ref_points.length; i++) {
97
            var pt = ref_points[i];
98
            var ptGeo = new OpenLayers.Geometry.Point(pt.lon, pt.lat);
99
            ptGeo.transform("EPSG:4326", map.getProjectionObject());
100
            var LonLat = new OpenLayers.LonLat(pt.lon, pt.lat).transform("EPSG:4326", map.getProjectionObject());
101
            map.setCenter(LonLat);
102
            var laFeature = new OpenLayers.Feature.Vector(ptGeo,
103
                                                          {description:pt.descr, lonlat: LonLat},
104
                                                          {externalGraphic: pt.icon_url?    pt.icon_url:def_pt.icon_url,
105
                                                           graphicWidth:    pt.icon_width?  pt.icon_width:def_pt.icon_width,
106
                                                           graphicHeight:   pt.icon_height? pt.icon_height:def_pt.icon_height,
107
                                                           graphicXOffset:  pt.icon_shiftX? pt.icon_shiftX:def_pt.icon_shiftX,
108
                                                           graphicYOffset:  pt.icon_shiftY? pt.icon_shiftY:def_pt.icon_shiftY,
109
                                                           graphicOpacity:  pt.opacity?     pt.opacity:def_pt.opacity,
110
                                                           title:           pt.title?       pt.title :''});
111
            if (i == 0) elFeature = laFeature;
112
            refpts_layer.addFeatures(laFeature);
113
            if (pt.showPopup) selectMarkerControl.select(laFeature);
114
        }
115
        if (typeof zoom == 'undefined') map.zoomToExtent(refpts_layer.getDataExtent());
116
    }
117

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

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

    
152
            var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
153
            lineLayer.addFeatures([lineFeature]);
154
        }
155
        map.addLayer(lineLayer);                    
156
    }
157
    if (typeof get_lon_lat != 'undefined' && get_lon_lat) {
158
        map.events.register("click", map, function(e) {
159
            var position = map.getLonLatFromViewPortPx(e.xy);
160
            position.transform(map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326"));
161
            alert(position.lon.toFixed(3) + ', ' + position.lat.toFixed(3));
162
        });
163
    }
164
}
165

    
166
if (typeof addLoadEvent == 'function') addLoadEvent(draw_map);
167
else window.onload = draw_map;