2

I'm using Angular-leaflet-directive. I have a menu where the user taps an option. Each time the user clicks an option the map is updated with two new markers and the bounds should be adjusted so these markers are both visible. The markers get updated but the bounds seem to work differently. The first time the user clicks something the bounds are updated, but they don't update for each time after that. This is the code which is called each time the user selects an option:

    var updateMap = function() {
        setDirections();

        $scope.bounds = leafletBoundsHelpers.createBoundsFromArray([
            [$scope.thisPractice.end.k, $scope.thisPractice.end.D],
            [$scope.thisPractice.start.k, $scope.thisPractice.start.D]
        ]);

        $scope.markers = {
            end: {
                title: "Destination",
                lat: $scope.thisPractice.end.k,
                lng: $scope.thisPractice.end.D,
                focus: true,
                icon: local_icons.markerRed
            },
            start: {
                title: "Start",
                lat: $scope.thisPractice.start.k,
                lng: $scope.thisPractice.start.D,
            }
        }
    }

This is my map initialization code:

// Initialize the map
    angular.extend($scope, {
        center: {},
        bounds: {},
        paths: {},
        markers: {},
        scrollWheelZoom: false,
        layers: {
            baselayers: {
                googleRoadmap: {
                    name: 'Google Streets',
                    layerType: 'ROADMAP',
                    type: 'google'
                }
            }
        }
    });

This is the HTML where my map occurs:

<div id="map_canvas">
    <leaflet id="leaflet_map" bounds="bounds" center="center" markers="markers" paths="paths" height="300px" width="100%" layers="layers"></leaflet>
</div>

It's not an issue with my data since the markers are updated fine. Any ideas? It seems weird that the bounds wouldn't function in the same way as the markers do.

Edit:

So the following code utilises the standard Leafleat.js functions and works:

var bounds = L.latLngBounds([$scope.thisPractice.end.k, $scope.thisPractice.end.D], [$scope.thisPractice.start.k, $scope.thisPractice.start.D])
leafletData.getMap().then(function(map) {
        map.fitBounds(bounds);
});

It would be nice if the angular directive implemented fitbounds in the same way.

Pancakes
  • 149
  • 2
  • 7
  • Have you tried just manually calling the map.fitBounds() method? Also try removing the center attribute. – Cory Silva Jan 23 '15 at 04:10
  • Removing center throws up an error because apparently bounds need a center attribute (even if empty) to work. fitBounds does seem to fix my problem which is nice, but I was hoping I could do it all within the directive... – Pancakes Jan 23 '15 at 04:16
  • @Pancakes did you found a solution for this? I have the **exact** same problem. – distante Nov 07 '16 at 15:20
  • 1
    @distante Yeah, check out my edit at the bottom of the question. There's a function in the leaflet library called fitBounds() which does the job. – Pancakes Nov 07 '16 at 22:15

0 Answers0