angular-leaflet-directive icon indicating copy to clipboard operation
angular-leaflet-directive copied to clipboard

Markers disappear when I add one

Open thetom88 opened this issue 12 years ago • 6 comments

Hello everyone and thank you for your great work. I've just updated the directive to the latest stable version. I have a problem when I add a new marker on the map, they're unvisible until I move the map. In addition the map doesn't always fit the bounds specified. Here's my html and controller code: http://pastebin.com/Pft78J0e

thetom88 avatar Jan 07 '14 09:01 thetom88

Hi, it seems that you obtain the markers from a service, which (possibly) will do a remote call to obtain the markers from an http service. The markers objects isn't notified of the new objects inside after a new angular cycle is processed.

I think this could be solved with any of these two options.

  1. Emit a promise from the service, when the promise is resolved the markers object will be updated and everything will stay on sync. For example:
var stepsRemoteCall = myService.getItinerary().VMConfigurator.steps;
stepsRemoteCall.then(function(steps) {
  ...
  // The rest of your code here
  ...
}

You can find the angular documentation of the promise $q implementation here: http://docs.angularjs.org/api/ng.$q

  1. Another way could be calling $scope.$apply(); when the service has finished the remote call, so the angular cycle is processed.

tombatossals avatar Jan 08 '14 08:01 tombatossals

Thank you tombatossals for your amazing work and for this answer of course. I forgot to say that the marker data are already in page and aren't download via $http, so I shouldn't need the promise, correct? I've also tried with $scope.$apply without success. Unfortunately I don't know exactly what to do..

thetom88 avatar Jan 08 '14 14:01 thetom88

Ok great, I have created a jsfiddle with your code so we can have a demo of the error.

http://jsfiddle.net/tombatossals/yXfuC/

Could you send a simple example of the object that will be obtained with this?

            var steps = myService.getItinerary().VMConfigurator.steps;

tombatossals avatar Jan 08 '14 14:01 tombatossals

Hi tombatossals, I've updated the fiddle with the necessary data. I haven't used the google layer, but i think it won't change that much. Here's the link: http://jsfiddle.net/yXfuC/7/ During this operation I found out I forgot to tell you that I reload the markers via ajax when I add or remove one and I do some other operations, then when finished, I emit an event on rootscope that is listened on map-controller and it should update map markers. Do I need to use a promise in this this situation? I think no, but I'm far from an angular guru

thetom88 avatar Jan 08 '14 16:01 thetom88

Hi, in the situation you mention I suppose it wouldn't be necessary to use a promise, but it's better to take a look at the real situation to know it. Whenever you update the $scope.markers object (or a reference to it), you must notify angular to recalculate the new values. If you make ajax calls, it's better if you do it with the angular $http service.

I have taken a look at the jsfiddle but don't see the problem you mention of the hidden markers, could you tell me which is the supposed bad behaviour?

tombatossals avatar Jan 10 '14 15:01 tombatossals

Closed, please reopen if needed.

tombatossals avatar Jun 20 '15 16:06 tombatossals