Take the 2-minute tour ×

When user pans the map, I make and ajax request to the server and get all the markers that fall into the new bounds, something almost identical to what is described here: Dynamically load Google Maps Markers with gmaps4rails

My problem is that when I use replaceMarker all the markers on the map are recreated therefore each of them flickers onces. This really annoys me.

If I use addMarkers, I don't get the flicker effect, but my sidebar gets screwed because; 1) The markers which are left out of the bounds after pan, are not removed from my marker list. 2) Some duplicates are added to my marker list - markers that fall into intersection of old and new bounds.

I tried modifying the addmarkers function but nothing good came out of it.

share|improve this question
Interesting. Please create a ticket on github, I'll patch the gem next week. –  apneadiving Dec 28 '11 at 8:27
add comment

1 Answer

up vote 2 down vote accepted

This is how I changed the addMarkers function. Comparing the new_markers set to the old one to find out which ones to remove and leave the already existing ones alone.

addMarkers : (new_markers) ->
  #update the list of markers to take into account
  added_markers = (marker for marker in new_markers when ($.grep(@markers, (a) -> a.id == marker.id).length == 0))
  removed_markers = (marker for marker in @markers when ($.grep(new_markers, (a) -> a.id == marker.id).length == 0))
  for marker in removed_markers
  @markers = @markers.concat(added_markers)
    #put markers on the map
share|improve this answer
Great! +1. Didn't see any pull request though :) –  apneadiving Dec 30 '11 at 19:39
I wasn't sure if this was generic enough because I'm compering ids which I generate. –  cashmere Dec 31 '11 at 20:01
Oh, right. Didn't see that on first glance with iphone. I'll keep the question in mind. –  apneadiving Dec 31 '11 at 23:31
add comment

Your Answer


