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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
XXX Lots overlap with map-OpenLayers.js - refactor!
XXX Things still need to be changed for mobile use, probably won't work
there.
TODO Pin size on report page
*/
function PaddingControl(div) {
div.style.padding = '40px';
}
function fixmystreet_update_pin(lonlat) {
document.getElementById('fixmystreet.latitude').value = lonlat.lat();
document.getElementById('fixmystreet.longitude').value = lonlat.lng();
$.getJSON('/report/new/ajax', {
latitude: $('#fixmystreet\\.latitude').val(),
longitude: $('#fixmystreet\\.longitude').val()
}, function(data) {
if (data.error) {
if (!$('#side-form-error').length) {
$('<div id="side-form-error"/>').insertAfter($('#side-form'));
}
$('#side-form-error').html('<h1>' + translation_strings.reporting_a_problem + '</h1><p>' + data.error + '</p>').show();
$('#side-form').hide();
return;
}
$('#side-form, #site-logo').show();
$('#councils_text').html(data.councils_text);
$('#form_category_row').html(data.category);
if ( data.extra_name_info && !$('#form_fms_extra_title').length ) {
// there might be a first name field on some cobrands
var lb = $('#form_first_name').prev();
if ( lb.length === 0 ) { lb = $('#form_name').prev(); }
lb.before(data.extra_name_info);
}
});
if (!$('#side-form-error').is(':visible')) {
$('#side-form, #site-logo').show();
}
}
var infowindow = new google.maps.InfoWindow();
function make_infowindow(marker) {
return function() {
infowindow.setContent(marker.title + "<br><a href=/report/" + marker.id + ">" + translation_strings.more_details + "</a>");
infowindow.open(fixmystreet.map, marker);
};
}
function fms_markers_list(pins, transform) {
var markers = [];
if (fixmystreet.markers) {
for (var m=0; m<fixmystreet.markers.length; m++) {
fixmystreet.markers[m].setMap(null);
}
}
for (var i=0; i<pins.length; i++) {
var pin = pins[i];
var pin_args = {
position: new google.maps.LatLng( pin[0], pin[1] ),
//size: pin[5] || 'normal',
id: pin[3],
title: pin[4] || '',
map: fixmystreet.map
};
if (pin[2] == 'green') {
pin_args.icon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|87dd00";
}
if (pin[2] == 'yellow') {
pin_args.icon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|ffd600";
}
var marker = new google.maps.Marker(pin_args);
if (fixmystreet.page == 'around' || fixmystreet.page == 'reports' || fixmystreet.page == 'my') {
var l = new google.maps.event.addListener(marker, 'click', make_infowindow(marker));
}
markers.push( marker );
}
return markers;
}
function fms_map_clicked(e) {
var lonlat = e.latLng;
if (fixmystreet.page == 'new') {
/* Already have a pin */
fixmystreet.report_marker.setPosition(lonlat);
} else {
var marker = new google.maps.Marker({
position: lonlat,
draggable: true,
animation: google.maps.Animation.DROP,
map: fixmystreet.map
});
var l = google.maps.event.addListener(marker, 'dragend', function(){
fixmystreet_update_pin( marker.getPosition() );
});
fixmystreet.report_marker = marker;
google.maps.event.removeListener(fixmystreet.event_update_map);
for (m=0; m<fixmystreet.markers.length; m++) {
fixmystreet.markers[m].setMap(null);
}
}
// check to see if markers are visible. We click the
// link so that it updates the text in case they go
// back
if ( ! 1 ) { // XXX fixmystreet.markers.getVisibility() )
fixmystreet.state_pins_were_hidden = true;
$('#hide_pins_link').click();
}
// Store pin location in form fields, and check coverage of point
fixmystreet_update_pin(lonlat);
// Already did this first time map was clicked, so no need to do it again.
if (fixmystreet.page == 'new') {
return;
}
$('#side').hide();
if (typeof heightFix !== 'undefined') {
heightFix('#report-a-problem-sidebar', '.content', 26);
}
// If we clicked the map somewhere inconvenient
// TODO
$('#sub_map_links').hide();
fixmystreet.page = 'new';
location.hash = 'report';
}
/* Pan data handler */
function fms_read_pin_json(obj) {
var current, current_near;
if (typeof(obj.current) != 'undefined' && (current = document.getElementById('current'))) {
current.innerHTML = obj.current;
}
if (typeof(obj.current_near) != 'undefined' && (current_near = document.getElementById('current_near'))) {
current_near.innerHTML = obj.current_near;
}
fixmystreet.markers = fms_markers_list( obj.pins, false );
}
function fms_update_pins() {
var b = fixmystreet.map.getBounds(),
b_sw = b.getSouthWest(),
b_ne = b.getNorthEast(),
bbox = b_sw.lng() + ',' + b_sw.lat() + ',' + b_ne.lng() + ',' + b_ne.lat(),
params = {
bbox: bbox
};
if (fixmystreet.all_pins) {
params.all_pins = 1;
}
$.getJSON('/ajax', params, fms_read_pin_json);
}
function fms_map_initialize() {
var centre = new google.maps.LatLng( fixmystreet.latitude, fixmystreet.longitude );
var map_args = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: centre,
zoom: 13 + fixmystreet.zoom,
disableDefaultUI: true,
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.RIGHT_TOP
},
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.RIGHT_TOP
},
mapTypeControl: true,
mapTypeControlOptions: {
position: google.maps.ControlPosition.RIGHT_TOP,
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
}
};
if (!fixmystreet.zoomToBounds) {
map_args.minZoom = 13;
map_args.maxZoom = 18;
}
fixmystreet.map = new google.maps.Map(document.getElementById("map"), map_args);
/* Space above the top right controls */
var paddingDiv = document.createElement('div');
var paddingControl = new PaddingControl(paddingDiv);
paddingDiv.index = 0;
fixmystreet.map.controls[google.maps.ControlPosition.RIGHT_TOP].push(paddingDiv);
if (fixmystreet.state_map && fixmystreet.state_map == 'full') {
// TODO Work better with window resizing, this is pretty 'set up' only at present
var q = fixmystreet_midpoint();
// Need to try and fake the 'centre' being 75% from the edge
fixmystreet.map.panBy(-q, -25);
}
if (document.getElementById('mapForm')) {
var l = google.maps.event.addListener(fixmystreet.map, 'click', fms_map_clicked);
}
$(window).hashchange(function(){
if (location.hash == '#report' && $('.rap-notes').is(':visible')) {
$('.rap-notes-close').click();
return;
}
if (location.hash && location.hash != '#') {
return;
}
// Okay, back to around view.
fixmystreet.report_marker.setMap(null);
fixmystreet.event_update_map = google.maps.event.addListener(fixmystreet.map, 'idle', fms_update_pins);
google.maps.event.trigger(fixmystreet.map, 'idle');
if ( fixmystreet.state_pins_were_hidden ) {
// If we had pins hidden when we clicked map (which had to show the pin layer as I'm doing it in one layer), hide them again.
$('#hide_pins_link').click();
}
$('#side-form').hide();
$('#side').show();
$('#sub_map_links').show();
//only on mobile
$('#mob_sub_map_links').remove();
$('.mobile-map-banner').html('<a href="/">' + translation_strings.home + '</a> ' + translation_strings.place_pin_on_map);
fixmystreet.page = 'around';
});
if ( fixmystreet.area.length ) {
for (var i=0; i<fixmystreet.area.length; i++) {
var args = {
url: "http://mapit.mysociety.org/area/" + fixmystreet.area[i] + ".kml?simplify_tolerance=0.0001",
clickable: false,
preserveViewport: true,
map: fixmystreet.map
};
if ( fixmystreet.area.length == 1 ) {
args.preserveViewport = false;
}
var a = new google.maps.KmlLayer(args);
a.setMap(fixmystreet.map);
}
}
if (fixmystreet.page == 'around') {
fixmystreet.event_update_map = google.maps.event.addListener(fixmystreet.map, 'idle', fms_update_pins);
}
fixmystreet.markers = fms_markers_list( fixmystreet.pins, true );
/*
if ( fixmystreet.zoomToBounds ) {
var bounds = fixmystreet.markers.getDataExtent();
if (bounds) {
var center = bounds.getCenterLonLat();
var z = fixmystreet.map.getZoomForExtent(bounds);
if ( z < 13 && $('html').hasClass('mobile') ) {
z = 13;
}
fixmystreet.map.setCenter(center, z);
}
}
*/
$('#hide_pins_link').click(function(e) {
var i, m;
e.preventDefault();
var showhide = [
'Show pins', 'Hide pins',
'Dangos pinnau', 'Cuddio pinnau',
"Vis nåler", "Gjem nåler",
"Zeige Stecknadeln", "Stecknadeln ausblenden"
];
for (i=0; i<showhide.length; i+=2) {
if (this.innerHTML == showhide[i]) {
for (m=0; m<fixmystreet.markers.length; m++) {
fixmystreet.markers[m].setMap(fixmystreet.map);
}
this.innerHTML = showhide[i+1];
} else if (this.innerHTML == showhide[i+1]) {
for (m=0; m<fixmystreet.markers.length; m++) {
fixmystreet.markers[m].setMap(null);
}
this.innerHTML = showhide[i];
}
}
});
$('#all_pins_link').click(function(e) {
var i;
e.preventDefault();
for (i=0; i<fixmystreet.markers.length; i++) {
fixmystreet.markers[i].setMap(fixmystreet.map);
}
var texts = [
'en', 'Show old', 'Hide old',
'nb', 'Inkluder utdaterte problemer', 'Skjul utdaterte rapporter',
'cy', 'Cynnwys hen adroddiadau', 'Cuddio hen adroddiadau'
];
for (i=0; i<texts.length; i+=3) {
if (this.innerHTML == texts[i+1]) {
this.innerHTML = texts[i+2];
fixmystreet.markers.protocol.options.params = { all_pins: 1 };
fixmystreet.markers.refresh( { force: true } );
lang = texts[i];
} else if (this.innerHTML == texts[i+2]) {
this.innerHTML = texts[i+1];
fixmystreet.markers.protocol.options.params = { };
fixmystreet.markers.refresh( { force: true } );
lang = texts[i];
}
}
if (lang == 'cy') {
document.getElementById('hide_pins_link').innerHTML = 'Cuddio pinnau';
} else if (lang == 'nb') {
document.getElementById('hide_pins_link').innerHTML = 'Gjem nåler';
} else {
document.getElementById('hide_pins_link').innerHTML = 'Hide pins';
}
});
}
google.maps.visualRefresh = true;
google.maps.event.addDomListener(window, 'load', fms_map_initialize);
|