1 | (function () {
|
---|
2 | angular.module('mapModule')
|
---|
3 | .controller('MapCtrl', ['$scope', '$http', 'GoogleMapService', function ($scope, $http, googleMapService) {
|
---|
4 | $scope.googleMapObject = {}; // The Google map module. See googleMapModule.js
|
---|
5 | $scope.searchResults = []; // Holds search results
|
---|
6 | $scope.searchPerformed = false; // Flag to track if the user has searched for anything yet
|
---|
7 |
|
---|
8 | $scope.selectOptions = {
|
---|
9 | regionNames: [], // E.g. ["Auckland", "Wellington"];
|
---|
10 | placesOfInterestTags: [], // E.g. ["Public Toilet", "Cafe"];
|
---|
11 | allPlacesOfInterest: [],
|
---|
12 | getPlacesNames: []
|
---|
13 | };
|
---|
14 |
|
---|
15 | $scope.filter = {
|
---|
16 | keyword: [],
|
---|
17 | regionNames: [],
|
---|
18 | placesOfInterestTags: [],
|
---|
19 | placesOfInterestTagsFilter: "all" // Default to "all" so every facility chosen must exist in results
|
---|
20 | };
|
---|
21 |
|
---|
22 | $scope.select2 = {
|
---|
23 | keywordDefaults: {
|
---|
24 | multiple: true,
|
---|
25 | simple_tags: true,
|
---|
26 | maximumSelectionSize: 1,
|
---|
27 | tags: [],
|
---|
28 | formatSelectionTooBig: function (maxSize) {
|
---|
29 | return "Only " + maxSize + " search term is allowed";
|
---|
30 | },
|
---|
31 | formatResult: function (result, container, query, escapeMarkup) {
|
---|
32 | container.addClass('needsclick');
|
---|
33 | return result.text;
|
---|
34 | }
|
---|
35 | },
|
---|
36 | defaults: {
|
---|
37 | maximumSelectionSize: 10,
|
---|
38 | formatResult: function (result, container, query, escapeMarkup) {
|
---|
39 | container.addClass('needsclick');
|
---|
40 | return result.text;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | };
|
---|
44 |
|
---|
45 | $scope.updateMap = function () {
|
---|
46 | // Serialized the map filter values that a user has entered, into JSON & post it to our Google Map Web API
|
---|
47 | var mapFilterValuesAsJson = angular.toJson({
|
---|
48 | Keyword: angular.isDefined($scope.filter.keyword) ? $scope.filter.keyword[0] : "",
|
---|
49 | RegionNames: angular.isDefined($scope.filter.regionNames) ? $scope.filter.regionNames : [],
|
---|
50 | PlacesOfInterestTags: angular.isDefined($scope.filter.placesOfInterestTags) ? $scope.filter.placesOfInterestTags : [],
|
---|
51 | MatchAllPlacesOfInterestTags: $scope.filter.placesOfInterestTagsFilter === "all" ? true : false
|
---|
52 | });
|
---|
53 |
|
---|
54 | googleMapService.getPlaces(mapFilterValuesAsJson).then(function (results) {
|
---|
55 | $scope.searchResults = results;
|
---|
56 | $scope.numOfResults = $scope.searchResults.length;
|
---|
57 |
|
---|
58 | if ($scope.numOfResults > 0) {
|
---|
59 | // Update map with results from Web API request
|
---|
60 | $scope.googleMapObject.addNewLocationMarkers($scope.searchResults);
|
---|
61 | }
|
---|
62 |
|
---|
63 | if ($scope.searchPerformed === false) {
|
---|
64 | $scope.searchPerformed = true;
|
---|
65 | }
|
---|
66 | });
|
---|
67 | };
|
---|
68 |
|
---|
69 | populateSelectOptionsAsync();
|
---|
70 |
|
---|
71 | function populateSelectOptionsAsync() {
|
---|
72 | googleMapService.getRegionNames().then(function (data) {
|
---|
73 | $scope.selectOptions.regionNames = data;
|
---|
74 | });
|
---|
75 |
|
---|
76 | googleMapService.getTags().then(function (data) {
|
---|
77 | $scope.selectOptions.placesOfInterestTags = data;
|
---|
78 | });
|
---|
79 |
|
---|
80 | googleMapService.getAllPlaces().then(function (data) {
|
---|
81 | $scope.selectOptions.allPlacesOfInterest = data;
|
---|
82 | });
|
---|
83 |
|
---|
84 | googleMapService.getPlacesNames().then(function (data) {
|
---|
85 | angular.extend($scope.select2.keywordDefaults.tags, data);
|
---|
86 | });
|
---|
87 | }
|
---|
88 | }]);
|
---|
89 | }()); |
---|