0

I am trying to bind the center attribute of a leaflet to the model like so:

<!DOCTYPE html>
<html>
<title>Test Leaflet</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<script src="angular-leaflet-directive.min.js"></script>
<script src="map.js"></script>
<body ng-app="mapApp" ng-controller="mapCtrl">
<ul>
<li ng-repeat="m in maps | orderBy:'ident':false">
        <div>
        {{m.ident}}
        <leaflet center="{{m.center}}" width="300px" height="300px"></leaflet>
        </div>
</li>
</ul>
</body>
</html>

with the JavaScript:

var mapApp = angular.module('mapApp', ['leaflet-directive']);
mapApp.controller('mapCtrl', ['$scope', function($scope, $http)
{
    $scope.maps = [
        { ident: "MAP1", center: { lat: 45, lng: 90, zoom: 10 } },
        { ident: "MAP2", center: { lat: -45, lng: -90, zoom: 10 } },
        { ident: "MAP3", center: { lat: 45, lng: -90, zoom: 10 } },
        { ident: "MAP4", center: { lat: -45, lng: 90, zoom: 10 } }
      ];
}]);

however this does not work. Only once I remove the center attribute do I get the maps but then they're not centered i.e.:

<leaflet width="300px" height="300px"></leaflet>

Can anyone assist in getting this working without changing the overall structure entirely.

(Apology in advance: I could not get the example to work on either Plunker or Fiddle even though it works fine in all my browsers)

Waslap
  • 572
  • 3
  • 23

2 Answers2

0

I think you have a couple options...one of which is this...

Add this to your controller:

angular.extend($scope, {
    center: {
        lat: 51.505,
        lng: -0.09,
        zoom: 4
    }
});

You also might need to do this (per https://github.com/tombatossals/angular-leaflet-directive):

"If you want to have more than one map on the page and access their respective map objects, add an id attribute to your leaflet directive in HTML:"

<leaflet id="mymap" lf-center="center" height="480px" width="640px"></leaflet>

Let me know how far this gets you...and we'll go from there.

jbdev
  • 711
  • 3
  • 11
  • I think the crux of my problem is that although the leaflet directive purports to be an angular integration of leaflet, it really isn't and there is no real two-way data binding. Extending the controller with 'center' does not really solve my problem as I need the center to vary with each map instance. I've tried the route with accessing each instance on its own but it gets really hacking as timeouts on ng-repeat only updates once for the app lifecycle so you need to resort to trickery such as updating in filter handlers to get real data binding updating dynamically. – Waslap Feb 16 '16 at 07:25
0

So I decided to mock it up and test a few things. This works for me...

HTML:

<ul>
<li ng-repeat="m in maps | orderBy:'ident':false">
        <div>
        {{m.ident}}
        <leaflet id="{{m.ident}}" center="m.center" width="300px" height="300px"></leaflet>
        </div>
</li>
</ul>

JS:

$scope.center = {center: { lat: 45, lng: 90, zoom: 1 }};

$scope.maps = [
    { ident: "MAP1", center: { lat: -45, lng: -90, zoom: 18 } },
    { ident: "MAP2", center: { lat: -45, lng: -90, zoom: 14} },
    { ident: "MAP3", center: { lat: -45, lng: -90, zoom: 10} },
    { ident: "MAP4", center: { lat: -45, lng: -90, zoom: 6 } }
  ];

Let me know...

jbdev
  • 711
  • 3
  • 11