angular-ui / ui-leaflet

AngularJS directive to embed an interact with maps managed by Leaflet library
http://angular-ui.github.io/ui-leaflet
Other
314 stars 134 forks source link

leafletDataHelper.getLayers returning old data #150

Open nmccready opened 9 years ago

nmccready commented 9 years ago

From @rikbosch on March 31, 2015 11:52

I'm trying to adjust the map's bounds to the bounds of a featureGroup.

However, when retrieving the bounds of the group, it contains no marker layers.

I'm calling the following function after setting the markers in the $scope object:

 $scope.layers = {
    baselayers: {},
    overlays: {
        primary: {
            type: 'featureGroup',
            name: 'Primary Lines',
            visible: true
        },

        secundary: {
            type: 'featureGroup',
            name: 'secundary',
            visible: false
        }
    }
};

$scope.markers = {};

myApiService.getMarkerData()
    .then(function(data) {
        $scope.markers = mapMarkers(data.lines);
        fitToLayer('primary');
    });

function fitToLayer(name) {
    leafletData.getMap()
        .then(function(map) {
            leafletData.getLayers()
                .then(function(layers) {
                    var layer = layers.overlays[name]; //<== layer has no markers defined 
                    map.fitBounds(layer.getBounds()); //<==throws error 'lat' undefined
                });
        });
}

function mapMarkers(lines) {
    return lines.map(function(line) {
        return {
            layer: 'primary',
            lat: line.x,
            lng: line.y,
            key: line.id
        };
    });
}

the full error:

TypeError: Cannot read property 'lat' of undefined
    at Object.L.LatLngBounds.getNorth (leaflet-src.js:1336)
    at Object.L.LatLngBounds.getNorthWest (leaflet-src.js:1316)
    at L.Map.L.Class.extend.getBoundsZoom (leaflet-src.js:1887)
    at L.Map.L.Class.extend.fitBounds (leaflet-src.js:1635)
    at main.controller.js:95
    at processQueue (angular.js:13248)
    at angular.js:13264
    at Scope.$get.Scope.$eval (angular.js:14466)
    at Scope.$get.Scope.$digest (angular.js:14282)
    at Scope.$get.Scope.$apply (angular.js:14571)

Copied from original issue: tombatossals/angular-leaflet-directive#668

nmccready commented 9 years ago

From @jessertaylor on March 31, 2015 12:0

have you tried dumping layer.getBounds()?

chrismanderson commented 8 years ago

Was there ever a solution to this issue? Believe I'm seeing the same error.

fletort commented 8 years ago

I am interesting to can access also to layer data. How can i used the leafletData tool ? In fact i would like to can acces to 'eachLayer' method of my gesojson layer to can modify my marker on a external action. as here : http://gis.stackexchange.com/a/133325