当用户平移地图时,我向服务器发出ajax请求,并获得所有落入新界限的标记,这些标记几乎与此处描述的相同:使用gmaps4rails动态加载Google Maps标记
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
我的问题是,当我使用replaceMarker时,将重新创建地图上的所有标记,因此每个标记都闪烁一次.这真的让我很烦.
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.
如果使用addMarkers,则不会获得闪烁效果,但侧边栏却被拧紧了,因为: 1)平移后未加在范围内的标记不会从我的标记列表中删除. 2)一些重复项添加到我的标记列表中-标记落入新旧边界的交点.
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.
我尝试修改addmarkers函数,但效果不佳.
I tried modifying the addmarkers function but nothing good came out of it.
推荐答案这就是我更改addMarkers函数的方式.将new_markers与旧标记进行比较,以找出要删除的标记并将其保留下来.
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 @resetSidebarContent() 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 @clearMarker(marker) @markers.remove(marker) @markers = @markers.concat(added_markers) #put markers on the map @create_markers() @adjustMapToBounds()更多推荐
gmaps4rails replaceMakers闪烁效果
发布评论