Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Markers not showing in second (or more) maps #345

Open
santiquetzal opened this issue Jul 13, 2017 · 4 comments
Open

Markers not showing in second (or more) maps #345

santiquetzal opened this issue Jul 13, 2017 · 4 comments

Comments

@santiquetzal
Copy link

santiquetzal commented Jul 13, 2017

I make an angular component to display one map with a marker, the problem was that if the component was called more than one time (more than one map is displayed on the screen), the marker only appears in the first map instance, but, the map is centerd correctly, so... something strange is happening. (A screen capture is attached at the end of the issue, its labeled as 'Capture 1').

I don't know where the problem can be, so I started to simplify my code up to:

 <leaflet id="1" markers=" {marker1: {lat: 42.846262, lng: -7.938083}}" height="300px"></leaflet>
 <leaflet id="2" markers=" {marker2: {lat: 4.846262,  lng: -2.938083}}" height="300px"></leaflet>

The markers are shown only in the first map!.
A screen capture is attached at the end of the issue (Labeled as 'Minimal example'
(I know the markers should be declared in the controller, but I put them in the html to make the example easier).

I went to ui-leaflet examples to find something that can helps me, but... In the examples the markers are not working too!

  • Accesing the map object with two (or more) maps on screen.
    The same marker should appear in both maps (same markers variable in controller), but it appears only in the first one.
  • Two maps sharing center example.
    Each map has its own markers (markers1 and markers2 in the controller), but the coordinates of the markers are identical. The markers should appear in both maps, but they are shown only in the first one. I noticed that geojson data are displayed in both maps...
  • Two maps with markers and events.
    This example is not working in github by now, I implemented it in my project and the same problem happend.

I checked that this problem happens to me with branches leaflet-1.X (this is the branch I'm using) and master.

Screen captures:

  • My own maps (Capture 1):
    image
  • My own maps (Minimal example):
    image
  • Example: Two maps sharing center example
    image
  • Example: Two maps with markers and events
    image
@ablock
Copy link

ablock commented Sep 7, 2017

This is happening to me as well, though which map gets the marker seems to be random.

@ablock
Copy link

ablock commented Sep 7, 2017

I figured out some additional details about what is going on here.

On processing the markers for the first map, _addMarkers (markers.js:80) is calling modelChangeInDirective in leafletHelpers.js. This is setting the directive global watchTrap.changeFromDirective to true. modelChangeInDirective uses a timeout to change that value back to false 10ms later, but that is not fast enough to prevent this code:

maybeWatch(leafletScope,'markers', watchOptions, function(newMarkers, oldMarkers){
                        if(watchTrap.changeFromDirective)
                            return;
                        _create(newMarkers, oldMarkers);
                    });

(markers.js:267-271)

...from blocking _create() from being called on the markers for the second map.

I have no idea what modelChangeInDirective is trying to achieve. It seems like maybe it is intended to debounce the watchers or something. Perhaps @nmccready can weigh in.

In any case, for our simple maps that don't do much with events, simply commenting out trapObj[trapField] = true; in leafletHelpers.js:144 doesn't seem to cause any problems and allows the markers to show on multiple maps.

@dasiekjs
Copy link

dasiekjs commented Jun 26, 2018

i had similar problem, but in my project, markers don't show , when i display map second (or more) time.
the problem was solved, when i add unique "group" in parker definition.

let _timeStamp = +new Date();
    vm.markers = users.map((user) => {
        return {  
          lat: user.realLatitude,  
          lng: user.realLongitude,  
          draggable: false,  
          icon: vm.config.local_icons.funskan_icon,  
          group: 'marker_group_${_timeStamp}', // here  
        };  
      });

@nmccready
Copy link
Contributor

Sorry to not provide feedback for so long, but I have long moved on from angular unless working on legacy projects. The main problem boils down to how leafletData is designed. It is a main bottleneck that can have race conditions and lose track of the map and or markers state themselves.

This limitation would require an entire rewrite which at this point is not worth the effort. Main reason being is that I am not using this library for work purposes any longer.

For getting multiple maps to work correctly you're going to want to implement a manager that maps DOM id's to the specific map instance.

An example:

https://github.com/realtymaps/map/blob/master/frontend/map/scripts/services/service.currentMap.coffee

utilized here:

https://github.com/realtymaps/map/blob/57be175fa42dd076520bf6a9c28cf2f6b2953084/frontend/map/scripts/factories/factory.map.coffee

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants