# Map center/zoom problems in ym4r

**URL:** <https://www.ruby-forum.com/t/map-center-zoom-problems-in-ym4r/226059>\
**Category:** Rails\
**Created:** [February 19, 2010, 8:22pm UTC](https://www.ruby-forum.com/t/map-center-zoom-problems-in-ym4r/226059 "2010-02-19T20:22:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [February 19, 2010, 8:22pm UTC](https://www.ruby-forum.com/t/map-center-zoom-problems-in-ym4r/226059/1 "2010-02-19T20:22:05Z")

</div>

I am trying to show a list of events and their locations in a map in  
my index action. I use will pagination for paginating through the  
events. The first call to the page is a regular “html” format and the  
subsequents calls are ajax calls. I initialize the map and markergroup  
that contains the event markersin the first call to index page, for  
subsequent ajax requests i bind the browser variables with the rubu  
variables and add/remove event markers from the group. It all works  
fine, but if lets say there are 2 markers 1 in new york the other in  
san fran, i want the map to automatically adjust zoon to show all  
markers in the map, but it just shows either one. if i call  
“centerandzoomonmarkwrs” on the group then the map centers on east  
coast of africa!

index action

* * *

def index  
@events = Event.paginate(:page =\> params[:page], :per\_page =\>  
2,  
:order =\> “updated\_at DESC”)  
unless request.xhr?  
build\_index\_map@events  
else  
@map = Variable.new(“map”)  
@group = Variable.new(“event\_marker\_group”)  
@markers = get\_markers(@events)  
end

```
    respond_to do |format|
        format.html # index.html.erb
        format.xml { render :xml => @events }
        format.js
    end
end

def build_index_map(events)
    @map = GMap.new("map_div_id")
    @map.control_init(:large_map => true, :map_type => true)
    ll = Address.get_lat_long(events.first.address)
    # to center on usa -

```

@map.center\_zoom\_init([38.134557,-95.537109],4)  
@map.center\_zoom\_init(ll, 4)

```
    @event_addresses = Event.get_addresses_of_events(events)
    @markers = Hash.new
    @event_addresses.each do |event, address|
        @markers[event.id] = GMarker.new(address,:title =>

```

event.title,  
:info\_window =\> event.description)  
end  
group = GMarkerGroup.new(true, @markers)  
@map.overlay\_global\_init(group, “event\_marker\_group”)  
@map.record\_init group.center\_and\_zoom\_on\_markers  
end

```
def get_markers(events)
    @event_addresses = Event.get_addresses_of_events(events)
    @markers = Hash.new
    @event_addresses.each do |event, address|
        @markers[event.id] = GMarker.new(address,:title =>

```

event.title,  
:info\_window =\> event.description)  
end  
return @markers  
end

* * *

index.rjs

* * *

page.replace “events\_list”, :partial =\> “events\_index”  
page \<\< @map.clear\_overlays  
page \<\< @group.clear  
page \<\< @group.activate  
@markers.each {|key, value| page \<\< @group.addMarker(value, key)}  
page \<\< @map.add\_overlay(@group)  
page \<\< @group.centerAndZoom

* * *
