ui-leaflet icon indicating copy to clipboard operation
ui-leaflet copied to clipboard

leafletDataHelper.getLayers returning old data

Open nmccready opened this issue 8 years ago • 3 comments

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 avatar Nov 02 '15 17:11 nmccready

From @jessertaylor on March 31, 2015 12:0

have you tried dumping layer.getBounds()?

nmccready avatar Nov 02 '15 17:11 nmccready

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

chrismanderson avatar Mar 15 '16 15:03 chrismanderson

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

fletort avatar Jun 02 '16 10:06 fletort