angular-ui / ui-leaflet

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

'The "center" property is not defined in the main scope' #135

Open nmccready opened 8 years ago

nmccready commented 8 years ago

From @toutpt on October 19, 2015 8:44

I dont find a way to create a directive that embed leaflet directive.

I have created a plunker to isolate the problem: http://plnkr.co/edit/zgnzG3fqHxJ13DoiNGy8?p=preview

console shows many errors:

This seems different from #362

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

nmccready commented 8 years ago

From @yukk1 on October 30, 2015 3:21

Thank you for forking my plunk.

I have updated my plunk. Please check it.

brunofin commented 8 years ago

Any news on that? I'm facing the same problem.

app.directive('fqLivemap', ['FquestRoutes', function(FquestRoutes) {
    return {
      templateUrl: '/partials/fquest/livemap.tmpl.html',
      link: function($scope, el) {
        $scope.center = {
          lat: -27.644606381943312,
          lng: -48.47648620605469,
          zoom: 10
        };
        if ("geolocation" in navigator) {
          navigator.geolocation.getCurrentPosition(function(position) {
            $scope.$apply(function() {
              $scope.center.lat = position.coords.latitude;
              $scope.center.lng = position.coords.longitude;
              $scope.center.zoom = 12;
            });
          });
        }
      }
    }
  }]);
<leaflet center="center"
    width="400px"
    height="300px">
</leaflet>
<p>{{ center }}</p>

The p tag displays center correctly.

jeansymolanza commented 8 years ago

I'm having the same issue as well/ Has this been resolved yet?

 function ConsumerBusinessProfileCtrl($scope) {

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

 });

<leaflet lf-center="center"></leaflet>

illepic commented 8 years ago

@brunofin, move your leaflet initialization vars to a directive controller like this:

app.directive('fqLivemap', ['FquestRoutes', function(FquestRoutes) {
    return {
      templateUrl: '/partials/fquest/livemap.tmpl.html',
      /* New stuff below here */
      controller: function($scope) {
        $scope.center = {
          lat: -27.644606381943312,
          lng: -48.47648620605469,
          zoom: 10
        };
      },
      /* New stuff above here */
      link: function($scope, el) {
        if ("geolocation" in navigator) {
          navigator.geolocation.getCurrentPosition(function(position) {
            $scope.$apply(function() {
              $scope.center.lat = position.coords.latitude;
              $scope.center.lng = position.coords.longitude;
              $scope.center.zoom = 12;
            });
          });
        }
      }
    }
  }]);
toutpt commented 8 years ago

For my part I have created angular-leaflet-light because of this issue https://github.com/toutpt/angular-leaflet-light

rawbeans commented 7 years ago

I am also having the issue of being unable to nest the directive in another directive. If I remove the center property, I get the error:

TypeError: Cannot read property 'baselayers' of undefined from inside the link function of the leaflet directive.