Source: directives/mapa-endereco/mapa-endereco.js

/**
 * @ngdoc directives
 * @name mapaEndereco
 * @module s4c.directives.mapa.MapaEndereco
 *
 * @description
 * `MapaEnderecoController` Controller do módulo do mapa endereço
 * 
 * @example   <mapa-endereco></mapa-endereco>
 * 
 */
(function () {
    'use strict';

    angular.module('s4c.directives.MapaEndereco', ['leaflet-directive'])
        .directive('mapaEndereco', MapaEndereco);

    MapaEnderecoController.$inject = ['$scope', 'leafletData', 'MapaEndereco', '$timeout', 'LocationService'];

    function MapaEnderecoController($scope, leafletData, MapaEndereco, $timeout, LocationService) {

        var mapa;

        setInterval(function () {
            if (mapa) {
                mapa.invalidateSize(true);
            }
        }, 1500);

        function invalidate() {
            leafletData
                .getMap('mapa-endereco')
                .then(function (map) {
                    mapa = map;
                    map.invalidateSize(true);
                });
        }

        $scope.center = {
            lat: -22.94249,
            lng: -43.48789,
            zoom: 16
        };

        $scope.$on('leafletDirectiveMap.moveend', function () {
            $timeout(function () {
                invalidate();
            }, 500);
        });

        LocationService.getLocation();

        $timeout(function () {
            leafletData
                .getMap('mapa-endereco')
                .then(function (map) {
                    var marker = L.marker({
                        lat: LocationService.getLat(),
                        lng: LocationService.getLon()
                    }, {
                        draggable: true
                    }).addTo(map);

                    MapaEndereco._marker = marker;
                    MapaEndereco.atualizarCoordenadas(LocationService.getLat(), LocationService.getLon());
                    marker.on('dragend', function () {
                        var latLng = marker.getLatLng();

                        MapaEndereco.atualizarCoordenadas(latLng.lat, latLng.lng);
                    });
                });
        }, 1500);

    }

    function MapaEndereco() {
        return {
            restrict: 'E',
            templateUrl: 'app/directives/mapa-endereco/mapa-endereco.html',
            replace: true,
            scope: {},
            controller: MapaEnderecoController
        }
    }
}());