Gmaps4Rails v2 - removeMarkers() not working after calling addMarkers()

283 views Asked by At

I have a function responsible for hiding/showing markers, so I decided to use removeMarkers() and addMarkers() with a variable which contains all markers displayed on map, preventing AJAX requests. However, removeMarkers() appears to not working when used after addMarkers() function:

#/assets/javascript/general.js.coffee
@buildMap = (markers)->
  provider = Gmaps.build(
    'Google',
    {
      builders: { Marker: RichMarkerBuilder},
      markers:
        clusterer:
          gridSize: 50
          styles: [
            url: "/assets/cluster.png"
            textSize: 15
            width: 56
            height: 56
          ]
    }
  )
  Gmaps.handler = @clustereredHandler()
  Gmaps.handler.buildMap {
    provider: provider,
    internal: {id: 'map'} }, ->
      Gmaps.markers = _.map(markers, (marker_json) ->
        marker = Gmaps.handler.addMarker(marker_json)
        _.extend marker, marker_json
        marker
      )
      Gmaps.map = Gmaps.handler.getMap()

      Gmaps.handler.bounds.extendWith(Gmaps.markers)
      Gmaps.handler.fitMapToBounds()

#app/views/stores/index.html.erb
buildMap(<%=raw @hash.to_json %>);

So, I have:

  • Handler on Gmaps.handler variable;
  • All markers on Gmaps.markers variable;
  • Map on Gmaps.map variable.

Steps to failure:

  1. Load map - OK (All markers loaded correctly);
  2. > Gmaps.handler.removeMarkers(Gmaps.markers) - OK (All markers hidden correctly);
  3. > Gmaps.handler.addMarkers(Gmaps.markers) - OK (All markers shown correctly);
  4. > Gmaps.handler.removeMarkers(Gmaps.markers) - FAILURE! (Markers still being displayed);

I'm using 2.1.2version. Is there any fix for it?

Thanks

2

There are 2 answers

0
apneadiving On

According to my plunkr here, there is no bug with gmaps4rails.

I feel like you have an issue with your own functions (maybe dont use extend?) and replace with:

marker.json  = marker_json

I cant tell much more since they are not included.

0
jhoanna On

I think the issue is in saving the markers. Whenever you add new markers, you overwrite the old ones. Try

Gmaps.markers.push.apply(Gmaps.markers, Gmaps.handler.addMarkers(Gmaps.markers))

to append the new markers instead.