osm-directive.js 1.5 KB
angular.module('focaModalPuntoDescarga').directive('osmPuntoDescarga', function() {
    return {
        restrict: 'E',
        link: function(scope, el, attrs) {
            var contenedor = document.createElement('div');
            contenedor.className = 'w-100 mt-3';
            el.append(contenedor);
            scope.map = L.map(contenedor);
            L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(scope.map);
        },
        controller: ['$scope', '$timeout', function($scope, $timeout) {
            //resuelve bug mapa gris en modales
            $timeout(function() {
                $scope.map.invalidateSize();
            }, 100);

            $scope.markers = [];
            $scope.$watchGroup(['latitud', 'longitud'], function() {
                for(var i in $scope.markers) {
                    $scope.map.removeLayer($scope.markers[i]);
                }
                $scope.map.setView([$scope.latitud, $scope.longitud], 14);
                $scope.markers.push(
                    L.marker([$scope.latitud, $scope.longitud], {draggable: true})
                    .addTo($scope.map)
                    .on('dragend', function() {
                        $scope.latitud = this.getLatLng().lat;
                        $scope.longitud = this.getLatLng().lng;
                        $scope.$apply();
                    })
                );
            });
        }],
        scope: {
            latitud: '=',
            longitud: '=',
            zoom: '='
        }
    };
});