Blame view

src/js/app.js 17.1 KB
856e1d58   Sean Wills   Search menu
1
var app = angular.module('batmanDelivers', ['ngSanitize', 'ui.select']);
b2145b07   Tarpit Grover   WIP
2

3ef35c23   Sean Wills   Commented feature...
3
4
5
6
app.controller('myCtrl', function($scope, $http, $rootScope, $sce) {


    //GLOBAL VARIABLES
856e1d58   Sean Wills   Search menu
7
8
    var key = 'AIzaSyAPYatPrCcmNcVdAO_MF0YhO3c1mgdLvuQ';
    var distance;
01ae2e35   Sean Wills   Falied smooth mov...
9
    var map;
3ef35c23   Sean Wills   Commented feature...
10
    var mapReset = false;
93483f6d   Sean Wills   Settings Working,...
11

3ef35c23   Sean Wills   Commented feature...
12
13
14
15
16
17
18
19
20
21

    //UI VARIABLES
    $scope.warning = '';
    $scope.timer = 0;
    $scope.predictedTime = 0;
    $scope.speed = 50; //Average MPH


    //UI ARRAYS
    // $scope.settings = [{
93483f6d   Sean Wills   Settings Working,...
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
    //         description: 'Enable Time Prediction',
    //         enabled: true,
    //         showOnRouteLoad: true,
    //         showOnPageLoad: false,
    //         template: $sce.trustAsHtml("<span>Duration: " + $scope.timer + " Prediction: " + $scope.predictedTime + " (Mins)</span>")
    //     },
    //     {
    //         description: 'Enable Speed Manipulation',
    //         enabled: true,
    //         showOnRouteLoad: true,
    //         showOnPageLoad: false,
    //         template: $sce.trustAsHtml("<span>Manipulate Speed:</span> <div id='slider'></div>")
    //     },
    //     {
    //         description: 'Enable Speed Display',
    //         enabled: true,
    //         showOnRouteLoad: true,
    //         showOnPageLoad: false,
    //         template: $sce.trustAsHtml("<p>View Speed: " + $scope.speed + " (MPH)</p>")
    //     },
    //     {
    //         description: 'Enable Additional Stops',
    //         enabled: true,
    //         showOnRouteLoad: false,
    //         showOnPageLoad: true,
    //         template: $sce.trustAsHtml("<p class='add-stop' ng-click='addStop()'>Additional Stop</p>")
    //     },
    //     {
    //         description: 'Reset Map',
    //         enabled: true,
    //         showOnRouteLoad: false,
    //         showOnPageLoad: true,
    //         template: $sce.trustAsHtml("<p ng-click='resetMap()'>Reset Map</p>")
    //     }
    // ]
    $scope.stops = [
3ef35c23   Sean Wills   Commented feature...
58
59
60
61
62
63
64
        {
            Description: "Start typing to search...",
            lbl: "Pickup",
            valid: false,
            id: 1

        }, 
de63be91   Sean Wills   No settings
65
        {
3ef35c23   Sean Wills   Commented feature...
66
67
68
69
70
            Description: "Start typing to search...",
            lbl: "Drop",
            valid: false,
            id: 2
    }]
484036e0   Tarpit Grover   WIP
71

b2145b07   Tarpit Grover   WIP
72

3ef35c23   Sean Wills   Commented feature...
73
74
    //INIT MAP
    var directionsService = new google.maps.DirectionsService;
01ae2e35   Sean Wills   Falied smooth mov...
75
76
    var directionsDisplay = new google.maps.DirectionsRenderer();
    initMap();
01ae2e35   Sean Wills   Falied smooth mov...
77

856e1d58   Sean Wills   Search menu
78

3ef35c23   Sean Wills   Commented feature...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    //FUNCTIONS
    $scope.$on('StopChanged', function() {
        var validstops = $scope.stops.filter(function(x) {
            return x.valid;
        });

        if (validstops.length == 1) {

            map.panTo(validstops[0].$marker.getPosition());
        } else if (validstops.length > 1) {
            var bounds = new google.maps.LatLngBounds();
            for (var i = 0; i < validstops.length; i++) {
                bounds.extend(validstops[i].$marker.getPosition());
            }

            map.fitBounds(bounds, 100);
        }
856e1d58   Sean Wills   Search menu
96

856e1d58   Sean Wills   Search menu
97

ec938bf0   Tarpit Grover   first commit
98
        if (validstops.length == $scope.stops.length) {
3ef35c23   Sean Wills   Commented feature...
99
100
101
102
103
104
105
106
107
            //Set Markers as null ****
            var waypoints = [];
            if ($scope.stops.length > 2) {
                waypoints = $scope.stops.slice(1, $scope.stops.length - 1);
            }
            calcRoute($scope.stops[0], $scope.stops[$scope.stops.length - 1], waypoints);
        }
    });
    function refreshSettings(completed){
93483f6d   Sean Wills   Settings Working,...
108
109
110
111
112
113
114
        newSettings = [];
        $scope.settings.forEach(function(s, i){
            if(completed && !s.showOnPageLoad){
                s.showOnRouteLoad = true;
            } else {
                s.showOnRouteLoad = false;
            }         
3ef35c23   Sean Wills   Commented feature...
115
            newSettings.push(s);
93483f6d   Sean Wills   Settings Working,...
116
117
118
119
120
            if (i == $scope.settings.length - 1){
                $scope.settings = newSettings;
            }
        })
    }
3ef35c23   Sean Wills   Commented feature...
121
122
123
124
    function slider() {
        $("#slider").slider({
            animate: "fast",
            animate: true,
93483f6d   Sean Wills   Settings Working,...
125
126
127
            value: 50,
            min: 20,
            max: 300,
3ef35c23   Sean Wills   Commented feature...
128
129
            step: 1,
            slide: function(event, ui) {
ec938bf0   Tarpit Grover   first commit
130
                $scope.$apply(function() {
ec938bf0   Tarpit Grover   first commit
131
                    $scope.speed = ui.value;
3ef35c23   Sean Wills   Commented feature...
132
                    getTime();
01ae2e35   Sean Wills   Falied smooth mov...
133
134
135
136
137
                });
            }
        });
    }
    function getTime() {
b2145b07   Tarpit Grover   WIP
138
        var speed = $scope.speed / distance; //Difference
ec938bf0   Tarpit Grover   first commit
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
        setTimeout(function() {
            $scope.predictedTime = 60 / speed; //Work out the minutes based on the difference
        }, 500)
    }
    function initMap() {
        map = new google.maps.Map(document.getElementById('map-div'), {
            scrollwheel: false,
            center: {
                lat: 51.491903,
                lng: -0.024640
            },
            mapTypeId: 'roadmap',
            zoom: 14,
            mapTypeControl: false,
            styles: [{
                    "featureType": "all",
                    "elementType": "labels.text.fill",
                    "stylers": [{
                            "saturation": 36
                        },
                        {
                            "color": "#000000"
                        },
                        {
                            "lightness": 40
                        }
                    ]
                },
                {
                    "featureType": "all",
                    "elementType": "labels.text.stroke",
                    "stylers": [{
                            "visibility": "on"
                        },
                        {
                            "color": "#000000"
                        },
                        {
                            "lightness": 16
                        }
                    ]
                },
                {
                    "featureType": "all",
                    "elementType": "labels.icon",
                    "stylers": [{
                        "visibility": "off"
                    }]
                },
                {
                    "featureType": "administrative",
                    "elementType": "geometry.fill",
                    "stylers": [{
                            "color": "#000000"
                        },
                        {
                            "lightness": 20
                        }
                    ]
                },
                {
                    "featureType": "administrative",
                    "elementType": "geometry.stroke",
                    "stylers": [{
                            "color": "#000000"
                        },
                        {
                            "lightness": 17
                        },
                        {
                            "weight": 1.2
                        }
                    ]
                },
                {
                    "featureType": "landscape",
                    "elementType": "geometry",
                    "stylers": [{
                            "color": "#000000"
                        },
                        {
                            "lightness": 20
                        }
                    ]
                },
                {
                    "featureType": "poi",
                    "elementType": "geometry",
                    "stylers": [{
                            "color": "#000000"
                        },
                        {
                            "lightness": 21
                        }
                    ]
                },
                {
                    "featureType": "road.highway",
                    "elementType": "geometry.fill",
                    "stylers": [{
                            "color": "#000000"
                        },
                        {
                            "lightness": 17
                        }
                    ]
                },
                {
                    "featureType": "road.highway",
                    "elementType": "geometry.stroke",
                    "stylers": [{
                            "color": "#000000"
                        },
                        {
                            "lightness": 29
                        },
                        {
                            "weight": 0.2
                        }
                    ]
                },
                {
                    "featureType": "road.arterial",
                    "elementType": "geometry",
                    "stylers": [{
                            "color": "#000000"
                        },
                        {
                            "lightness": 18
                        }
                    ]
                },
                {
                    "featureType": "road.local",
                    "elementType": "geometry",
                    "stylers": [{
                            "color": "#000000"
                        },
                        {
                            "lightness": 16
                        }
                    ]
                },
                {
                    "featureType": "transit",
                    "elementType": "geometry",
                    "stylers": [{
                            "color": "#000000"
                        },
                        {
                            "lightness": 19
                        }
b2145b07   Tarpit Grover   WIP
291
                    ]
ec938bf0   Tarpit Grover   first commit
292
                },
a0fba7c8   David Beech   david
293
                {
ec938bf0   Tarpit Grover   first commit
294
                    "featureType": "water",
01ae2e35   Sean Wills   Falied smooth mov...
295
                    "elementType": "geometry",
01ae2e35   Sean Wills   Falied smooth mov...
296
297
298
                    "stylers": [{
                            "color": "#000000"
                        },
b2145b07   Tarpit Grover   WIP
299
                        {
01ae2e35   Sean Wills   Falied smooth mov...
300
301
302
                            "lightness": 17
                        }
                    ]
b2145b07   Tarpit Grover   WIP
303
                }
01ae2e35   Sean Wills   Falied smooth mov...
304
305
306
307
308
            ]
        });
        $rootScope.map = map;
    }
    function calcRoute(start, end, waypoints) {
856e1d58   Sean Wills   Search menu
309
        directionsService.route({
3ef35c23   Sean Wills   Commented feature...
310
311
312
313
314
315
316
317
            origin: start.$marker.getPosition(),
            destination: end.$marker.getPosition(),
            waypoints: waypoints.map(function(x) {
                return {
                    location: x.Description,
                    stopover: true
                };
            }),
93483f6d   Sean Wills   Settings Working,...
318
            travelMode: 'DRIVING',
3ef35c23   Sean Wills   Commented feature...
319
320
321
322
323
324
325
            provideRouteAlternatives: false
        }, function(response, status) {
            if (status === 'OK') {
                distance = response.routes[0].legs[0].distance.value * 0.000621371;
                if (distance < 35) {
                    directionsDisplay.setMap(map);
                    directionsDisplay.setDirections(response);
01ae2e35   Sean Wills   Falied smooth mov...
326
327
328
329
330
                    directionsDisplay.setOptions({
                        suppressMarkers: true
                    });
                    slider();
                    getTime();
26ced867   Sean Wills   New commit
331
                    refreshSettings();
5b416886   Sean Wills   Move and rotate
332
                    follow(response.routes[0].overview_path);
01ae2e35   Sean Wills   Falied smooth mov...
333
                } else {
b2145b07   Tarpit Grover   WIP
334
335
336
337
                    $scope.warning = 'Destination Too Far Away!';
                    setTimeout(function() {
                        $scope.warning = " ";
                    }, 1000)
01ae2e35   Sean Wills   Falied smooth mov...
338
339
                }
            } else {
5b416886   Sean Wills   Move and rotate
340
341
342
                console.log("Need more Stops", start, end, waypoints)
            }
        });
01ae2e35   Sean Wills   Falied smooth mov...
343
    }
856e1d58   Sean Wills   Search menu
344
    function follow(overview_path) {
5b416886   Sean Wills   Move and rotate
345
        var marker = new SlidingMarker({
856e1d58   Sean Wills   Search menu
346
            position: overview_path[0],
26ced867   Sean Wills   New commit
347
            icon: {
856e1d58   Sean Wills   Search menu
348
349
350
                url: "../includes/images/car3x.png",
                scaledSize: new google.maps.Size(32, 32),
                anchor: new google.maps.Point(16, 16)
3ef35c23   Sean Wills   Commented feature...
351
            },
b2145b07   Tarpit Grover   WIP
352
            map: map,
93483f6d   Sean Wills   Settings Working,...
353
            optimized: false,
3ef35c23   Sean Wills   Commented feature...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
            duration: 0,
            easing: "linear"
        });

        //Distances and Measurements
        var pre = [0];
        var j = 0;
        var rotateTime = 0;
        var heading;
        var metres;

        function moveDriver() {
            var from = new google.maps.LatLng(overview_path[j].lat(), overview_path[j].lng());
            var to = new google.maps.LatLng(overview_path[j + 1].lat(), overview_path[j + 1].lng());

856e1d58   Sean Wills   Search menu
369
            $scope.stops.forEach(function(stop) {
3ef35c23   Sean Wills   Commented feature...
370
                if (stop.valid) {
856e1d58   Sean Wills   Search menu
371
                    var stopLatLng = new google.maps.LatLng(stop.$marker.position.lat(), stop.$marker.position.lng());
3ef35c23   Sean Wills   Commented feature...
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
                    var distance = google.maps.geometry.spherical.computeDistanceBetween(from, stopLatLng); //Metres
                    if (distance < 50) {
                        stop.$marker.setMap(null);
                        stop.valid = false;
                        //Delete Marker
                    } else {
                        //Already removed
                    }
                }

            })

            var heading = Math.round(google.maps.geometry.spherical.computeHeading(from, to));
            pre.push(heading);
            var turn = Math.abs(pre[0]) - Math.abs(pre[1]);
            if (Math.abs(turn) > 40 && Math.abs(turn) < 140) {
                if ($scope.speed < 100) {
                    rotateTime = 1500; //secs
                } else {
                    rotateTime = 500; //secs
                }
                if (heading < 0) {
                    //Turning left
856e1d58   Sean Wills   Search menu
395
                    setTimeout(function() {
3ef35c23   Sean Wills   Commented feature...
396
397
398
399
400
                        $('img[src="../includes/images/car3x.png"]').css({
                            'transform': 'rotate(' + heading + 'deg)'
                        });
                    }, rotateTime)
                } else {
856e1d58   Sean Wills   Search menu
401
                    setTimeout(function() {
3ef35c23   Sean Wills   Commented feature...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
                        $('img[src="../includes/images/car3x.png"]').css({
                            'transform': 'rotate(' + heading + 'deg)'
                        });
                    }, rotateTime)
                }
            } else {
                rotateTime = 0;
                $('img[src="../includes/images/car3x.png"]').css({
                    'transform': 'rotate(' + heading + 'deg)'
                });

            }
            //** Make batman do skid over roundabouts

            //Getting Distance to milliseconds
            var metres = google.maps.geometry.spherical.computeDistanceBetween(from, to); //Metres
            var miles = metres * 0.000621371; //Miles
            var speed = $scope.speed / miles; //Difference
            var speedPerHour = 60 / speed; //Work out the minutes based on the difference
            var speedPerMilli = speedPerHour * 60000; // Convert to milliseconds

            j++;
856e1d58   Sean Wills   Search menu
424

3ef35c23   Sean Wills   Commented feature...
425
            marker.duration = speedPerMilli + rotateTime;
93483f6d   Sean Wills   Settings Working,...
426
            setTimeout(function() {
3ef35c23   Sean Wills   Commented feature...
427
428
                marker.setPosition(overview_path[j]);
            }, rotateTime)
93483f6d   Sean Wills   Settings Working,...
429
430
            if (j < overview_path.length - 1 && mapReset != true) {
                map.setZoom(16);
856e1d58   Sean Wills   Search menu
431
                map.panTo(marker.getPosition());
b2145b07   Tarpit Grover   WIP
432
433
                setTimeout(function() {
                    pre.shift();
1131cb0d   Sean Wills   First commit
434
                    moveDriver();
b2145b07   Tarpit Grover   WIP
435
                }, speedPerMilli + rotateTime)
1131cb0d   Sean Wills   First commit
436
            } else {
3ef35c23   Sean Wills   Commented feature...
437
438
                refreshSettings(true);
                $scope.warning = 'Route Completed!';
93483f6d   Sean Wills   Settings Working,...
439
440
441
442
443
444
445
                setTimeout(function() {
                    $scope.warning = '';
                }, 500)
            }
        }
        moveDriver();

3ef35c23   Sean Wills   Commented feature...
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
    }


    //UI FUNCTIONS
    // $scope.checkRouteSetting = function(setting){
    //     if (setting.enabled == true && setting.showOnRouteLoad == false){
    //         return true;
    //     } else {
    //         return false;
    //     }
    // }
    $scope.resetMap = function() {
        initMap();
        mapReset = true;
        $scope.stops = [{
            Description: "Start typing to search...",
            lbl: "Pickup",
            valid: false,
            id: 1

        }, {
            Description: "Start typing to search...",
            lbl: "Drop",
93483f6d   Sean Wills   Settings Working,...
469
470
471
472
473
474
475
476
477
478
479
            valid: false,
            id: 2
        }]
        $scope.warning = 'Map Reset!';
        setTimeout(function() {
            $scope.warning = " ";
            mapReset = false;
        }, 1000)
    }
    $scope.addStop = function() {
        if ($scope.stops.length < 4) {
3ef35c23   Sean Wills   Commented feature...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
            $scope.stops.splice($scope.stops.length - 1, 0, {
                Description: "Start typing to search...",
                lbl: "Stop"
            })
        } else {
            $scope.warning = 'Reached Stop Limit!';
            setTimeout(function() {
                $scope.warning = '';
            }, 500)
        }

    }
    $scope.checkStop = function(i) {
        if (i == 0 || i == $scope.stops.length - 1) {
            return true;
93483f6d   Sean Wills   Settings Working,...
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
        } else {
            return false;
        }
    }
    // $scope.enableSettings = function() {
    //     if ($('.settings').css('display') == 'none') {
    //         $('.settings').slideDown("slow");
    //     } else {
    //         $('.settings').slideUp("slow");
    //     }
    // }
    // $scope.changeSetting = function(change, i) {
    //     if (change == 'enable') {
    //         $scope.settings[i].enabled = true;
    //         $scope.warning = 'Setting Enabled, Click X To Disable!';
    //         $(".setting" + i).css("text-decoration", "none");
    //         setTimeout(function() {
    //             $scope.warning = " ";
    //         }, 1000)
    //     } else {
    //         $scope.settings[i].enabled = false;
    //         $scope.warning = 'Setting Disabled, Click X To Enable!';
    //         $(".setting" + i).css("text-decoration", "line-through");
    //         setTimeout(function() {
3ef35c23   Sean Wills   Commented feature...
519
520
521
522
    //             $scope.warning = " ";
    //         }, 1000)
    //     }
    // }
93483f6d   Sean Wills   Settings Working,...
523
524
525
526
527
528
529
    $scope.removeStop = function(i) {
        //TODO: STOP REMOVES PICKUP
        $scope.stops.splice(stop, 1);
    }


});
3ef35c23   Sean Wills   Commented feature...

856e1d58   Sean Wills   Search menu