summaryrefslogtreecommitdiff
path: root/static/main_map.js
blob: 637011799bc7f4c9bd54468d990aebff6b9d71fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* Copyright (C) 2008  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

See the file COPYING for details.
*/


/* main map */

/* check all the categories if clicked, unckeck if unclick */
function checkAll(item, ids){
    check = false;
    if(item.checked == true){
        check = true;
    }
    for (i in ids){
        document.getElementById('category_'+ids[i]).checked = check;
    }
}

/* show a block panel */
function show(id){
    document.getElementById(id).style.display = 'block';
}

/* hide a panel */
function hide(id){
    document.getElementById(id).style.display = 'None';
}

var map;

/* availaible map layers */
var layerMapnik = new OpenLayers.Layer.OSM.Mapnik('Classic');
var cyclemap = new OpenLayers.Layer.OSM.CycleMap("Cycle map", {
      displayOutsideMaxExtent: true,
      wrapDateLine: true});

/* default size and offset for icon */
var size = new OpenLayers.Size(21, 25);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);

/* define global variable */
var markers = new Array();
var layerMarkers;
var layerVectors;

var currentPopup;
var currentFeature;
var clicked = false;

/* show a popup */
function showPop(feature) {
    if (currentPopup != null) {
      currentPopup.hide();
    }
    if (feature.popup == null) {
        feature.popup = feature.createPopup();
        map.addPopup(feature.popup);
    } else {
        feature.popup.toggle();
    }
    currentPopup = feature.popup;
}

/* load geo objects with an AJAX request */
function loadGeoObjects(){
    /* get checked categories */
    inputs = window.document.forms["frm_categories"];
    var categories = '';
    var display_submited = false;
    for (var i = 0; i < inputs.length; i++) {
        input = inputs[i];
        if (input.checked
             && input.name.substring('category_'.length, 0) == 'category_'){
            id = input.name.substring('category_'.length, input.name.length);
            if(categories) categories += '_';
            categories += id;
        }
        if (input.checked && input.name == 'display_submited'){
            display_submited = true;
        }
    }
    /* 0 stand for all categories */
    if (!categories) categories = '0';
    var uri = "/chimere/getGeoObjects/" + categories;
    if (display_submited) uri += "/A_S";
    OpenLayers.loadURL(uri, '', this, setGeoObjects);
}

/* update the marker and vector layers from an http response GeoJSON */
function setGeoObjects(response){
    if(layerMarkers) layerMarkers.destroy();
    if(layerVectors) layerVectors.destroy();
    if (response.responseText.indexOf('no results') == -1) {
        /* clean the marker layer */
        if (currentPopup) {
            currentPopup.hide();
            hide('detail');
        }
        layerVectors = new OpenLayers.Layer.Vector("Vector Layer");
        map.addLayer(layerVectors);
        layerVectors.setOpacity(0.8);
        layerMarkers = new OpenLayers.Layer.Markers('POIs');
        map.addLayer(layerMarkers);
        layerMarkers.setOpacity(0.8);

        var json = new OpenLayers.Format.JSON();
        var geo_objects = json.read(response.responseText);
        /* load every geo object */
        for (var i = 0; i < geo_objects.features.length; i++) {
            var feature = geo_objects.features[i];
            if (feature.geometry.type == 'Point') putMarker(feature);
            if (feature.geometry.type == 'LineString') {
                putRoute(feature);
            }
        }
        /*
        var geojson = new OpenLayers.Format.GeoJSON();
        var markers_pt = geojson.read(response.responseText);
        for (var i = 0; i < markers_pt.length; i++) {
            putMarker2(markers_pt[i]);
        }*/
    }
}


/* put a route on the map */
function putRoute(route) {
    var polyline = route.geometry;
    var point_array = new Array();
    for (i=0; i<polyline.coordinates.length; i++){
        var point = new OpenLayers.Geometry.Point(polyline.coordinates[i][0],
                                                  polyline.coordinates[i][1]);
        point_array.push(point);
    }
    var linestring = new OpenLayers.Geometry.LineString(point_array);
    currentFeature = new OpenLayers.Feature.Vector();

    var style = OpenLayers.Util.extend({},
                                OpenLayers.Feature.Vector.style['default']);
    style.strokeColor = route.properties.color;
    style.strokeWidth = 3;
    currentFeature.style = style;
    currentFeature.geometry = linestring;
    layerVectors.addFeatures([currentFeature]);
}


/* put a marker on the map */
function putMarker(mark) {
    /* initialise a new marker with appropriate attribute for setting a
    marker */
    lat = mark.geometry.coordinates[1];
    lon = mark.geometry.coordinates[0];
    var size = new OpenLayers.Size(mark.properties.icon_width,
                                   mark.properties.icon_height);
    iconclone = new OpenLayers.Icon(media_path + mark.properties.icon_path,
                                    size, offset);
    var feature = new OpenLayers.Feature(markers,
              new OpenLayers.LonLat(lon, lat).transform(epsg_display_projection,
                                                        epsg_projection),
              {icon:iconclone});
    /*feature.closeBox = false;*/
    feature.pk = mark.properties.pk;
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.FramedCloud);
    feature.data.popupContentHTML = "<div class='cloud'>";
    feature.data.popupContentHTML += mark.properties.name;
    feature.data.popupContentHTML += "<br/>&nbsp;</div>";
    feature.data.overflow = 'hidden';
    var marker = feature.createMarker();
    /* manage markers events */
    var markerClick = function (evt) {
        currentFeature = this;
        if (clicked) {
            if (currentPopup == this.popup) {
                this.popup.hide();
                clicked = false;
                hide('detail');
            } else {
                currentPopup.hide();
                showPop(this);
                updateDetail(this.pk);
            }
        } else {
            showPop(this);
            clicked = true;
            updateDetail(this.pk);
        }
        OpenLayers.Event.stop(evt);
    };
    var markerOver = function (evt) {
        document.body.style.cursor='pointer';
        OpenLayers.Event.stop(evt);
    };
    var markerOut = function (evt) {
        document.body.style.cursor='auto';
        OpenLayers.Event.stop(evt);
    };
    marker.events.register('mousedown', feature, markerClick);
    marker.events.register('mouseover', feature, markerOver);
    marker.events.register('mouseout', feature, markerOut);
    layerMarkers.addMarker(marker);
    return feature;
}

/* update current detail panel with an AJAX request */
function updateDetail(pk){
    var uri = "/chimere/getDetail/" + pk;
    OpenLayers.loadURL(uri, '', this, setDetail);
}

/* update the detail panel from an http response */
function setDetail(response){
    if (response.responseText.indexOf('no results') == -1) {
        document.getElementById('detail').innerHTML = response.responseText;
        show('detail');
    }
}



/* main initialisation function */
function init(){
    /* set the main map */
    map = new OpenLayers.Map('map', {
        controls:[new OpenLayers.Control.Navigation(),
                  new OpenLayers.Control.PanZoomBar(),
                  new OpenLayers.Control.ScaleLine()],
        maxResolution: 156543.0399,
        units: 'm',
        projection: new OpenLayers.Projection('EPSG:4326')
    });
        /*projection: new OpenLayers.Projection('EPSG:900913'),
        displayProjection: new OpenLayers.Projection('EPSG:4326')*/
    map.addLayers([layerMapnik, cyclemap]);
    /* zoom to the appropriate extent */
    if (!zoomToCurrentExtent(map)){
        map.setCenter(centerLonLat, 12);
    }
    loadGeoObjects();
}