Projekt

Obecné

Profil

Stáhnout (2.31 KB) Statistiky
| Větev: | Revize:
1
angular.module('pvpk')
2
  .controller('mapController', ['$rootScope', '$scope', 'config', 'Device', function ($rootScope, $scope, config, Device) {
3
    this.$onInit = function () {
4
      $scope.markers = []
5

    
6
      $scope.map = new google.maps.Map(document.getElementById('map'), {
7
        center: config.DEFAULT_POSITION,
8
        zoom: config.DEFAULT_ZOOM,
9
        minZoom: config.DEFAULT_ZOOM_MIN,
10
        zoomControl: true,
11
        mapTypeControl: false,
12
        scaleControl: false,
13
        streetViewControl: false,
14
        rotateControl: false,
15
        fullscreenControl: false,
16
        mapTypeId: google.maps.MapTypeId.ROADMAP
17
      })
18

    
19
      Device.query({ showDirection: 0 }).$promise.then(function (dataResponse) {
20
        for (var i = 0, lctn; lctn = dataResponse[i]; i++) {
21
          $scope.createMarker(lctn)
22
        }
23
      })
24
    }
25

    
26
    $scope.createMarker = function (lctn) {
27
      if (lctn.lat && lctn.lng) {
28
        var marker = new google.maps.Marker({
29
          map: $scope.map,
30
          position: { lat: lctn.lat, lng: lctn.lng },
31
          title: lctn.name,
32
          infoWindow: new google.maps.InfoWindow({
33
            content: '<h6 class="mb-1">' + lctn.name + '</h6>' +
34
                        '<address>' + lctn.street + ', ' + lctn.town + '</address>'
35
          }),
36
          id: lctn.id
37
        })
38

    
39
        marker.addListener('click', function () {
40
          $scope.closeInfoWindows()
41
          marker.infoWindow.open($scope.map, marker)
42
          $rootScope.$emit('infoLocation', { id: lctn.id })
43
        })
44

    
45
        $scope.markers.push(marker)
46
      }
47
    }
48

    
49
    $rootScope.$on('activeMarker', function (event, args) {
50
      for (var i = 0, marker; marker = $scope.markers[i]; i++) {
51
        if (marker.id && marker.id === args.id && marker.infoWindow) {
52
          $scope.map.setCenter(marker.getPosition())
53
          $scope.map.setZoom(12)
54
          marker.infoWindow.open($scope.map, marker)
55
        } else {
56
          marker.infoWindow.close()
57
        }
58
      }
59
    })
60

    
61
    $rootScope.$on('setDefaultMap', function (event, args) {
62
      $scope.map.setCenter(config.DEFAULT_POSITION)
63
      $scope.map.setZoom(config.DEFAULT_ZOOM)
64
      $scope.closeInfoWindows()
65
    })
66

    
67
    $scope.closeInfoWindows = function () {
68
      for (var i = 0, marker; marker = $scope.markers[i]; i++) {
69
        marker.infoWindow.close()
70
      }
71
    }
72
  }])
(3-3/4)