2

I'm trying to add a marker to the Leaflet directive but somehow it doesn't accept markers created with the default L.marker() method.

The directive is used as follows:

<leaflet markers="markers" center="center" layers="layers" defaults="defaults"></leaflet>

I'm extending my $scope in the controller as prescribed:

angular.extend($scope, {
    markers: {},
    //markers: { { someName: {lat:52.163815,lng:5.365131} } //this does work
});

Adding a marker afterwards doesn't work somehow. First the .markers object is not an array, so I can't just add any elements using push(). But even adding an element as associative array doesn't work:

var marker = L.marker(L.latLng(52.163815, 5.365131));
$scope.markers[0] = marker;

The error is:

[AngularJS - Leaflet] The marker definition is not valid.

[AngularJS - Leaflet] Received invalid data on the marker 0.

I'm overlooking something very simple but I've got no idea what... Any lead would be greatly appreciated.

Community
  • 1
  • 1
Ropstah
  • 17,538
  • 24
  • 120
  • 194
  • 1
    Why do you need to create your marker with `L.marker`? Angular expects only [simple plain objects with a set of possible properties](https://github.com/tombatossals/angular-leaflet-directive/blob/master/doc/markers-attribute.md#marker-attributes), exactly like you did to initialize `$scope.markers`. Simply add later on simple plain objects to your `$scope.markers` object. You can use an incrementer to get unique keys and avoid overriding previous objects if needed (see https://stackoverflow.com/questions/33901333/angular-token-error) – ghybs Nov 25 '15 at 08:38

1 Answers1

2

$scope.markers is expecting to get an object with marker properties, not the marker itself. What would work in your example is just a LatLng object, before wrapping it as Marker.

$scope.markers[0] = L.latLng(52.163815, 5.365131);

Or, if you get Markers from the outside, you can get it back from the inside:

$scope.markers[0] = marker.getLatLng();

Obviously, that doesn't convey another markers' properties, just coordinates.

NOtherDev
  • 9,542
  • 2
  • 36
  • 47