Statistics
| Branch: | Tag: | Revision:

root / static / js / geolocate.js @ ea51bc9d

History | View | Annotate | Download (6.1 kB)

1
        var lat;
2
        var lng;
3
        var zoomLevel;
4
        var latlng;
5
        var map;
6
        var bounds;
7
        var image;
8
        var marker;
9
        var eduMarker;
10
        var infoWindow;
11
        var getOnce;
12
        var geocoder;
13
        var infoWindow;
14
        var directionDisplay;
15
    var directionsService;
16
    var closestURL;
17

    
18
        var styles;
19
        var pinImg;
20

    
21
        function initialize() {
22
                image = new google.maps.MarkerImage(pinImg,
23
                        new google.maps.Size(29, 40),
24
                        new google.maps.Point(0, 0),
25
                        new google.maps.Point(14, 40)
26
                );
27
                var styleArray = [{
28
                        featureType : "all",
29
                        stylers : [{
30
                                saturation : -60
31
                        }, {
32
                                gamma : 1.00
33
                        }]
34
                }, {
35
                        featureType : "poi.business",
36
                        elementType : "labels",
37
                        stylers : [{
38
                                visibility : "off"
39
                        }]
40
                }, {
41
                        "featureType" : "transit.line",
42
                        "elementType" : "geometry",
43
                        "stylers" : [{
44
                                "visibility" : "off"
45
                        }]
46
                }, {
47
                        "featureType" : "poi",
48
                        "elementType" : "all",
49
                        "stylers" : [{
50
                                "visibility" : "off"
51
                        }]
52
                }, {
53
                        'featureType' : "administrative.country",
54
                        'elementType' : "labels",
55
                        'stylers' : [{
56
                                'visibility' : "off"
57
                        }]
58
                }];
59
                var mapOptions = {
60
                        center : latlng,
61
                        zoom : zoomLevel,
62
                        mapTypeId : google.maps.MapTypeId.ROADMAP,
63
                        styles : styleArray,
64
                        mapTypeId : google.maps.MapTypeId.ROADMAP,
65
                        mapTypeControlOptions : {
66
                                style : google.maps.MapTypeControlStyle.DEFAULT
67
                        },
68
                        navigationControl : true,
69
                        mapTypeControl : false,
70
                };
71
                console.log(mapOptions);
72
                map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
73

    
74
                var input = document.getElementById('searchTextField');
75
            var autocomplete = new google.maps.places.Autocomplete(input);
76

    
77
            autocomplete.bindTo('bounds', map);
78

    
79
                infoWindow = new google.maps.InfoWindow();
80
                bounds = new google.maps.LatLngBounds();
81
                directionsDisplay = new google.maps.DirectionsRenderer();
82
                geocoder = new google.maps.Geocoder();
83
                directionsDisplay.setMap(map);
84

    
85
                if (getOnce == false) {
86
                                marker = new google.maps.Marker({
87
                                        position : latlng,
88
                                        draggable : true,
89
                                        animation : google.maps.Animation.DROP,
90

    
91
                                });
92
                                marker.setMap(map);
93
                        }
94
                google.maps.event.addListener(map, 'idle', function() {
95
                        if(navigator.geolocation && getOnce == false) {
96
                                navigator.geolocation.getCurrentPosition(getPosition);
97
                        }
98

    
99
                });
100
                google.maps.event.addListener(map, 'click', function(event) {
101
                        moveMarker(event.latLng);
102
                });
103
                google.maps.event.addListener(marker, 'dragend', function(event) {
104
                        moveMarker(event.latLng);
105
                });
106

    
107
                google.maps.event.addListener(autocomplete, 'place_changed', function() {
108
                        var place = autocomplete.getPlace();
109
                        if (place.geometry.viewport) {
110
                    map.fitBounds(place.geometry.viewport);
111
                  } else {
112
                    map.setCenter(place.geometry.location);
113
                    map.setZoom(17);  // Why 17? Because it looks good.
114
                  }
115
                        moveMarker(place.geometry.location);
116
                });
117
        }
118

    
119
        function calcRoute(start, end) {
120
                $("#distanceText").html();
121
        var request = {
122
            origin:start,
123
            destination:end,
124
            travelMode: google.maps.DirectionsTravelMode.WALKING
125
        };
126
        directionsService.route(request, function(response, status) {
127
          if (status == google.maps.DirectionsStatus.OK) {
128
            directionsDisplay.setDirections(response);
129
            var route = response.routes[0];
130
            var distText = route.legs[0].distance.text;
131
            $("#distanceText").html('Total distance: ' + distText);
132
          }
133
        });
134
      }
135

    
136
        function moveMarker(position) {
137
                marker.setPosition(position);
138
                getClosest(position);
139
        }
140

    
141
        function getPosition(position) {
142
                latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
143
                getOnce = true;
144
                map.setCenter(latlng);
145
                map.setZoom(12);
146
                marker.setPosition(latlng);
147
                getClosest(latlng);
148
        }
149

    
150
        function getClosest(coords){
151

    
152
                $.ajax({
153
                        url: closestURL,
154
                        data: {"lat": coords.lat(), "lng": coords.lng()},
155
                        type: "GET",
156
                        cache: false,
157
                        success:function(data){
158
                                bounds = new google.maps.LatLngBounds();
159
                                if (eduMarker){
160
                                        eduMarker.setMap(null);
161
                                }
162
                                bounds.extend(coords);
163
                                bounds.extend(new google.maps.LatLng(data.lat, data.lng));
164
                                eduMarker = new google.maps.Marker({
165
                                        position : new google.maps.LatLng(data.lat, data.lng),
166
                                        draggable : true,
167
                                        'icon': image,
168
                                        animation : google.maps.Animation.DROP,
169

    
170
                                });
171
                                eduMarker.setMap(map);
172
                                map.fitBounds(bounds);
173
                                calcRoute(coords, new google.maps.LatLng(data.lat, data.lng));
174
                                google.maps.event.addListener(
175
                                        eduMarker,
176
                                        'click',
177
                                        function() {
178
                                                infoWindow.setContent( "<div>"
179
                                                        + data.text
180
                                                        + "</div>"
181
                                                );
182
                                                infoWindow.open(
183
                                                        map,
184
                                                        eduMarker
185
                                                );
186
                                        });
187
                                }
188
                        });
189

    
190
        }
191

    
192
        function autocomplete() {
193
            var input = $('#searchTextField');
194
            var autocomplete = new google.maps.places.Autocomplete(input);
195
            autocomplete.bindTo('bounds', map);
196
            var marker = new google.maps.Marker({
197
              map: map
198
            });
199
        }
200

    
201
        function resizeMap()
202
        {
203

    
204
                if (map != undefined)
205
                {
206
                        google.maps.event.trigger(map, 'resize');
207
                }
208

    
209
                return false;
210
        }
211

    
212
        $(document).ready(function() {
213
                var mapDiv = $('#map_canvas');
214
                lat = mapDiv.data('center-lat');
215
                lng = mapDiv.data('center-lng');
216
                lat = parseFloat(lat.toString().replace(",","."));
217
                lng = parseFloat(lng.toString().replace(",","."));
218
                var group = mapDiv.data('group');
219
                pinImg = mapDiv.data('pin');
220
                styles = [{
221
                        url : group,
222
                        height : 54,
223
                        width : 63,
224
                        textColor : '#ffffff',
225
                        textSize : 11
226
                }, {
227
                        url : group,
228
                        height : 54,
229
                        width : 63,
230
                        textColor : '#ffffff',
231
                        textSize : 11
232
                }, {
233
                        url : group,
234
                        height : 54,
235
                        width : 63,
236
                        textColor : '#ffffff',
237
                        textSize : 11
238
                }];
239
                closestURL = mapDiv.data('closest'),
240
                zoomLevel = 6;
241
                latlng = new google.maps.LatLng(lat, lng);
242
                map = '';
243
                bounds = '';
244
                image = '';
245
                marker = '';
246
                eduMarker = false;
247
                infoWindow;
248
                getOnce = false;
249
                geocoder = null;
250
                infoWindow;
251
                directionDisplay;
252
                directionsService = new google.maps.DirectionsService();
253
                initialize();
254
                resizeMap();
255
                $("#mylocm").click(function(){
256
                        navigator.geolocation.getCurrentPosition(getPosition);
257
                        return false;
258
                });
259

    
260
        });
261

    
262
        $(window).resize(function(){
263
                resizeMap();
264
        });