Statistics
| Branch: | Tag: | Revision:

root / static / js / home-page-map.js @ 909263e2

History | View | Annotate | Download (9.8 kB)

1
        var lat;
2
        var lng;
3
        var zoomLevel = 6;
4
        var latlng;
5
        var map;
6
        var bounds;
7
        var image;
8
        var infoWindow;
9
        var cityImg;
10
        var countryImg;
11
        var styles;
12
        var markersUrl;
13
        var pinImg;
14

    
15
        function initialize() {
16
                image = new google.maps.MarkerImage(pinImg,
17
                // This marker is 29 pixels wide by 40 pixels tall.
18
                new google.maps.Size(29, 40),
19
                // The origin for this image is 0,0.
20
                new google.maps.Point(0, 0),
21
                // The anchor for this image is the base of the flagpole at 18,42.
22
                new google.maps.Point(14, 40));
23

    
24
                var styleArray = [ {
25
                        featureType : "all",
26
                        stylers : [ {
27
                                saturation : -60
28
                        }, {
29
                                gamma : 1.00
30
                        } ]
31
                }, {
32
                        featureType : "poi.business",
33
                        elementType : "labels",
34
                        stylers : [ {
35
                                visibility : "off"
36
                        } ]
37
                }, {
38
                        "featureType" : "transit.line",
39
                        "elementType" : "geometry",
40
                        "stylers" : [ {
41
                                "visibility" : "off"
42
                        } ]
43
                }, {
44
                        "featureType" : "poi",
45
                        "elementType" : "all",
46
                        "stylers" : [ {
47
                                "visibility" : "off"
48
                        } ]
49
                }, {
50
                        'featureType' : "administrative.country",
51
                        'elementType' : "labels",
52
                        'stylers' : [ {
53
                                'visibility' : "off"
54
                        } ]
55
                }
56

    
57
                ];
58

    
59
                geocoder = new google.maps.Geocoder();
60
                // disable scrolling/draging in maps
61
                if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
62
                        isDraggable = false;
63
                        isScrollable = true;
64
                } else {
65
                        isDraggable = true;
66
                        isScrollable = false;
67
                }
68

    
69
                var mapOptions = {
70
                        center : latlng,
71
                        zoom : zoomLevel,
72
                        mapTypeId : google.maps.MapTypeId.ROADMAP,
73
                        styles : styleArray,
74
                        mapTypeId : google.maps.MapTypeId.ROADMAP,
75
                        mapTypeControlOptions : {
76
                                style : google.maps.MapTypeControlStyle.DEFAULT
77
                        },
78
                        navigationControl : true,
79
                        mapTypeControl : false,
80
                        scrollwheel: isScrollable,
81
                        draggable: isDraggable
82
                };
83
                map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
84

    
85

    
86
                var homeControlDiv = document.createElement('div');
87
                homeControlDiv.className='roundButtonHolder';
88
                homeControlDiv.id="locationButton";
89
                var homeControl = new HomeControl(homeControlDiv, map);
90

    
91
                homeControlDiv.index = 1;
92
                map.controls[google.maps.ControlPosition.TOP_LEFT].push(homeControlDiv);
93

    
94
                bounds = new google.maps.LatLngBounds();
95
                infoWindow = new google.maps.InfoWindow();
96
                google.maps.event.addListener(map, 'idle', function() {
97
                        center = map.getCenter();
98
                        geocode(center);
99
                        zoom = map.getZoom();
100
                        if (zoom > 12){
101
                                $("#showCityBtn").show();
102
                                $("#showCountryBtn").hide();
103
                        }
104
                        else if ((zoom <= 12) && (zoom > 7)){
105
                                $("#showCityBtn").hide();
106
                                $("#showCountryBtn").show();
107
                        }
108
                        else {
109
                                $("#showCityBtn").hide();
110
                                $("#showCountryBtn").hide();
111
                        }
112
                });
113

    
114
        }
115

    
116
        var markers = new Array();
117
        function placeMarkers() {
118
                $.get(markersUrl,
119
                        function(data) {
120
                                $.each(
121
                                        data,
122
                                        function(index, jsonMarker) {
123
                                                var marker = createMarker(jsonMarker);
124
                                                if (marker) {
125
                                                        bounds.extend(marker.position);
126
                                                                markers.push(marker);
127
                                                                google.maps.event.addListener(
128
                                                                        marker,
129
                                                                        'click',
130
                                                                        function() {
131
                                                                                infoWindow.setContent ( "<div><h4>"
132
                                                                                                + jsonMarker.inst
133
                                                                                                + "</h4>"
134
                                                                                                +
135

    
136
                                                                                                "<div class='tabbable'>"
137
                                                                                                + "<ul class='nav nav-tabs'>"
138
                                                                                                + "<li class='active'><a href='#tab1' data-toggle='tab'>Info</a></li>"
139
                                                                                                + "<li><a href='#tab2' data-toggle='tab'>More...</a></li>"
140
                                                                                                + "</ul>"
141
                                                                                                + "<div class='tab-content'>"
142
                                                                                                + "<div class='tab-pane active' id='tab1'>"
143
                                                                                                + "<dl class='dl-horizontal'>"
144
                                                                                                + "<dt>Name</dt><dd>"
145
                                                                                                + jsonMarker.name
146
                                                                                                + "&nbsp;</dd>"
147
                                                                                                + "<dt>Address</dt><dd>"
148
                                                                                                + jsonMarker.address
149
                                                                                                + "&nbsp;</dd>"
150
                                                                                                + "<dt>Encryption</dt><dd>"
151
                                                                                                + jsonMarker.enc
152
                                                                                                + "&nbsp;</dd>"
153
                                                                                                + "<dt>SSID</dt><dd>"
154
                                                                                                + jsonMarker.SSID
155
                                                                                                + "&nbsp;</dd>"
156
                                                                                                + "<dt>Number of APs</dt><dd>"
157
                                                                                                + jsonMarker.AP_no
158
                                                                                                + "&nbsp;</dd></dl>"
159
                                                                                                + "</div>"
160
                                                                                                + "<div class='tab-pane' id='tab2'>"
161
                                                                                                + "<dl class='dl-horizontal'>"
162
                                                                                                + "<dt>Port Restrict</dt><dd>"
163
                                                                                                + jsonMarker.port_restrict
164
                                                                                                + "&nbsp;</dd>"
165
                                                                                                + "<dt>transp_proxy</dt><dd>"
166
                                                                                                + jsonMarker.transp_proxy
167
                                                                                                + "&nbsp;</dd>"
168
                                                                                                + "<dt>IPv6</dt><dd>"
169
                                                                                                + jsonMarker.IPv6
170
                                                                                                + "&nbsp;</dd>"
171
                                                                                                + "<dt>NAT</dt><dd>"
172
                                                                                                + jsonMarker.NAT
173
                                                                                                + "&nbsp;</dd>"
174
                                                                                                + "<dt>Wired</dt><dd>"
175
                                                                                                + jsonMarker.wired
176
                                                                                                + "&nbsp;</dd></dl>"
177
                                                                                                + "</div>"
178
                                                                                                + "</div>"
179
                                                                                                + "</div>"
180
                                                                                                + "</div>");
181
                                                                                infoWindow.open(
182
                                                                                        map,
183
                                                                                        marker
184
                                                                                );
185
                                                                        });
186
                                                                }
187
                                                        });
188
                                                        var mcOptions = {
189
                                                                gridSize : 50,
190
                                                                maxZoom : 15,
191
                                                                styles : styles
192
                                                        };
193
                                                        var markerCluster = new MarkerClusterer(map,
194
                                                                        markers, mcOptions);
195
                                                        map.fitBounds(bounds)
196
                                                });
197
        }
198

    
199
        function createMarker(markerObj) {
200
                var title = markerObj.name;
201
                var address = markerObj.address;
202
                var latLng = new google.maps.LatLng(markerObj.lat, markerObj.lng);
203
                var marker = new google.maps.Marker({
204
                        'position' : latLng,
205
                        'map' : map,
206
                        'title' : title,
207
                        'address' : address,
208
                        'icon' : image,
209
                });
210
                return marker
211
        }
212

    
213
        function clusterMarkers(markers) {
214
                var markerCluster = new MarkerClusterer(map, markers);
215
        }
216

    
217
        function rad(x) {
218
                return x * Math.PI / 180;
219
        }
220

    
221
        function find_closest_marker(event) {
222
                var lat = event.latLng.lat();
223
                var lng = event.latLng.lng();
224
                var R = 6371; // radius of earth in km
225
                var distances = [];
226
                var closest = -1;
227
                for (i = 0; i < markers.length; i++) {
228
                        var mlat = markers[i].position.lat();
229
                        var mlng = markers[i].position.lng();
230
                        var dLat = rad(mlat - lat);
231
                        var dLong = rad(mlng - lng);
232
                        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
233
                                        + Math.cos(rad(lat)) * Math.cos(rad(lat))
234
                                        * Math.sin(dLong / 2) * Math.sin(dLong / 2);
235
                        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
236
                        var d = R * c;
237
                        distances[i] = d;
238
                        if (closest == -1 || d < distances[closest]) {
239
                                closest = i;
240
                        }
241
                }
242

    
243
        }
244

    
245
        function geocode(position){
246
                addr = {};
247
                geocoder
248
                .geocode(
249
                                {
250
                                        'latLng' : position
251
                                },
252
                                function(results, status) {
253
                                        if (status == google.maps.GeocoderStatus.OK) {
254
                                                if (results.length >= 1) {
255
                                                        for ( var ii = 0; ii < results[0].address_components.length; ii++) {
256
                                                                var street_number = route = street = city = state = zipcode = country = formatted_address = '';
257
                                                                var types = results[0].address_components[ii].types
258
                                                                                .join(",");
259
                                                                if (types == "sublocality,political"
260
                                                                                || types == "locality,political"
261
                                                                                || types == "neighborhood,political"
262
                                                                                || types == "political") {
263
                                                                        addr.city = (city == '' || types == "locality,political") ? results[0].address_components[ii].long_name
264
                                                                                        : city;
265
                                                                }
266
                                                                if (types == "country,political") {
267
                                                                        addr.country = results[0].address_components[ii].long_name;
268
                                                                }
269
                                                        }
270
                                                }
271
                                        }
272
                                });
273
        }
274

    
275
        function codeAddress(address) {
276
            geocoder.geocode( { 'address': address}, function(results, status) {
277
              if (status == google.maps.GeocoderStatus.OK) {
278
                map.setCenter(results[0].geometry.location);
279
                    map.fitBounds(results[0].geometry.bounds)
280
              } else {
281
              }
282
            });
283
          }
284

    
285
        function HomeControl(controlDiv, map) {
286

    
287
                  // Set CSS styles for the DIV containing the control
288
                  // Setting padding to 5 px will offset the control
289
                  // from the edge of the map.
290
                  controlDiv.style.padding = '5px';
291

    
292
                  // Set CSS for the control border.
293
                  var controlUI = document.createElement('button');
294
                  controlUI.className='btn btn-warning roundButton';
295
                  controlUI.id = "showCityBtn";
296
                  extraCSS = 'background-image: url(' + cityImg + ');background-position: center center; background-repeat: no-repeat;';
297
                  controlUI.style.cssText='display:none; cursor:pointer; white-space:nowrap; position:absolute; '+extraCSS;
298
                  controlUI.title = "City View";
299

    
300
                  // Set CSS for the control border.
301
                  var controlUI2 = document.createElement('button');
302
                  controlUI2.className='btn btn-warning roundButton';
303
                  controlUI2.id = "showCountryBtn";
304
                  extraCSS2 = 'background-image: url(' + countryImg + ');background-position: center center; background-repeat: no-repeat;';
305
                  controlUI2.style.cssText='display:none; cursor:pointer; white-space:nowrap; position:absolute; '+extraCSS2;
306
                  controlUI2.title = "Country View";
307

    
308
                  controlDiv.appendChild(controlUI);
309
                  controlDiv.appendChild(controlUI2);
310

    
311
                  // Setup the click event listeners: simply set the map to Chicago.
312
                  google.maps.event.addDomListener(controlUI, 'click', function() {
313
                    codeAddress(addr.city+','+addr.country);
314
                  });
315
                  google.maps.event.addDomListener(controlUI2, 'click', function() {
316
                            codeAddress(addr.country);
317
                          });
318

    
319
                }
320

    
321
        $(document).ready(function() {
322
                mapDiv = $('#map_canvas')
323
                lat = mapDiv.data("map-center-lat");
324
                lat = parseFloat(lat.toString().replace(",","."));
325
                lng = mapDiv.data("map-center-lng");
326
                lng = parseFloat(lng.toString().replace(",","."));
327
                cityImg = mapDiv.data("city");
328
                countryImg = mapDiv.data("country");
329
                zoomLevel = 6;
330
                latlng = new google.maps.LatLng(lat,lng);
331
                map = '';
332
                bounds = '';
333
                image = '';
334
                infoWindow;
335
                pinImg = mapDiv.data("pin");
336
                addr = {};
337
                styles = [{
338
                        url : mapDiv.data("pin"),
339
                        height : 54,
340
                        width : 63,
341
                        textColor : '#ffffff',
342
                        textSize : 11
343
                }, {
344
                        url : mapDiv.data("pin"),
345
                        height : 54,
346
                        width : 63,
347
                        textColor : '#ffffff',
348
                        textSize : 11
349
                }, {
350
                        url : mapDiv.data("pin"),
351
                        height : 54,
352
                        width : 63,
353
                        textColor : '#ffffff',
354
                        textSize : 11
355
                }];
356

    
357
                markersUrl = mapDiv.data('markers');
358

    
359
                initialize();
360
                marks = placeMarkers();
361
                clusterMarkers(marks);
362
        });
363