Statistics
| Branch: | Tag: | Revision:

root / static / js / world-map.js @ 242932f3

History | View | Annotate | Download (7.3 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
        addr = {};
10
        var styles;
11
        var pinImg;
12
        var pinsUrl;
13
        var cityImg;
14
        var countryImg;
15

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

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

    
58
                ];
59

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

    
117
        var markers = new Array();
118
        function placeMarkers() {
119
                $.get(
120
                        pinsUrl,
121
                        function(data) {
122
                                $.each(
123
                                        data,
124
                                        function(index, jsonMarker) {
125
                                                var marker = createMarker(jsonMarker);
126
                                                if (marker) {
127
                                                        bounds.extend(marker.position);
128
                                                        markers.push(marker);
129
                                                        google.maps.event.addListener(
130
                                                                marker,
131
                                                                'click',
132
                                                                function() {
133
                                                                        infoWindow.setContent(jsonMarker.text);
134
                                                                        infoWindow.open(
135
                                                                                map,
136
                                                                                marker
137
                                                                        );
138
                                                                }
139
                                                        );
140
                                                }
141
                                        }
142
                                );
143
                                var mcOptions = {
144
                                        gridSize : 60,
145
                                        maxZoom : null,
146
                                        styles : styles
147
                                };
148

    
149
                                var markerCluster = new MarkerClusterer(
150
                                        map,
151
                                        markers,
152
                                        mcOptions
153
                                );
154
                                map.fitBounds(bounds);
155
                        }
156
                );
157
        }
158

    
159
        function createMarker(markerObj) {
160
                var latLng = new google.maps.LatLng(markerObj.lat, markerObj.lng);
161
                var marker = new google.maps.Marker({
162
                        'position' : latLng,
163
                        'map' : map,
164
                        'icon' : image,
165
                });
166
                return marker
167
        }
168

    
169
        function clusterMarkers(markers) {
170
                var markerCluster = new MarkerClusterer(map, markers);
171
        }
172

    
173

    
174
        function geocode(position){
175
                addr = {};
176
                geocoder
177
                .geocode(
178
                                {
179
                                        'latLng' : position
180
                                },
181
                                function(results, status) {
182
                                        if (status == google.maps.GeocoderStatus.OK) {
183
                                                if (results.length >= 1) {
184
                                                        for ( var ii = 0; ii < results[0].address_components.length; ii++) {
185
                                                                var street_number = route = street = city = state = zipcode = country = formatted_address = '';
186
                                                                var types = results[0].address_components[ii].types
187
                                                                                .join(",");
188
                                                                if (types == "sublocality,political"
189
                                                                                || types == "locality,political"
190
                                                                                || types == "neighborhood,political"
191
                                                                                || types == "political") {
192
                                                                        addr.city = (city == '' || types == "locality,political") ? results[0].address_components[ii].long_name
193
                                                                                        : city;
194
                                                                }
195
                                                                if (types == "country,political") {
196
                                                                        addr.country = results[0].address_components[ii].long_name;
197
                                                                }
198
                                                        }
199
                                                }
200
                                        }
201
                                });
202
        }
203

    
204
        function codeAddress(address) {
205
            geocoder.geocode( { 'address': address}, function(results, status) {
206
              if (status == google.maps.GeocoderStatus.OK) {
207
                map.setCenter(results[0].geometry.location);
208
                    map.fitBounds(results[0].geometry.bounds)
209
              } else {
210
                //alert("Geocode was not successful for the following reason: " + status);
211
              }
212
            });
213
          }
214

    
215
        function HomeControl(controlDiv, map) {
216

    
217
                  // Set CSS styles for the DIV containing the control
218
                  // Setting padding to 5 px will offset the control
219
                  // from the edge of the map.
220
                  controlDiv.style.padding = '5px';
221

    
222
                  // Set CSS for the control border.
223
                  var controlUI = document.createElement('button');
224
                  controlUI.className='btn btn-warning roundButton';
225
                  controlUI.id = "showCityBtn";
226
                  extraCSS = 'background-image: url(' + cityImg +');background-position: center center; background-repeat: no-repeat;';
227
                  controlUI.style.cssText='display:none; cursor:pointer; white-space:nowrap; position:absolute; '+extraCSS;
228
                  controlUI.title = "City View";
229

    
230
                  // Set CSS for the control border.
231
                  var controlUI2 = document.createElement('button');
232
                  controlUI2.className='btn btn-warning roundButton';
233
                  controlUI2.id = "showCountryBtn";
234
                  extraCSS2 = 'background-image: url('+ countryImg + ');background-position: center center; background-repeat: no-repeat;';
235
                  controlUI2.style.cssText='display:none; cursor:pointer; white-space:nowrap; position:absolute; '+extraCSS2;
236
                  controlUI2.title = "Country View";
237

    
238
                  controlDiv.appendChild(controlUI);
239
                  controlDiv.appendChild(controlUI2);
240

    
241
                  // Setup the click event listeners: simply set the map to Chicago.
242
                  google.maps.event.addDomListener(controlUI, 'click', function() {
243
                    codeAddress(addr.city+','+addr.country);
244
                  });
245
                  google.maps.event.addDomListener(controlUI2, 'click', function() {
246
                            codeAddress(addr.country);
247
                          });
248

    
249
                }
250

    
251
        $(document).ready(function() {
252
                mapDiv = $('#map_canvas');
253
                lat = mapDiv.data('center-lat');
254
                lng = mapDiv.data('center-lng');
255
                lat = parseFloat(lat.toString().replace(",","."));
256
                lng = parseFloat(lng.toString().replace(",","."));
257
                latlng = new google.maps.LatLng(lat,lng);
258
                group = mapDiv.data('group');
259
                pinImg = mapDiv.data('pin');
260
                pinsUrl = mapDiv.data('url');
261
                cityImg = mapDiv.data('city');
262
                countryImg = mapDiv.data('country');
263
                styles = [ {
264
                        url : group,
265
                        height : 54,
266
                        width : 63,
267
                        textColor : '#ffffff',
268
                        textSize : 11
269
                }, {
270
                        url : group,
271
                        height : 54,
272
                        width : 63,
273
                        textColor : '#ffffff',
274
                        textSize : 11
275
                }, {
276
                        url : group,
277
                        height : 54,
278
                        width : 63,
279
                        textColor : '#ffffff',
280
                        textSize : 11
281
                } ];
282
                initialize();
283
                marks = placeMarkers();
284
                clusterMarkers(marks);
285
        });