Q:

Google-Maps-for-Rails center map via ajax

I'm setting a new marker via ajax and I was wondering how to center the map on the new point with a custom zoom level, I made in this way:
  def geocode
pos = Gmaps4rails.geocode(params[:address])
render :update do |page|
    unless pos.blank?
        page << "$('#poi_latitude').val(#{pos.first[:lat]});"
        page << "$('#poi_longitude').val(#{pos.first[:lng]});"
        page << "if (marker != null) { marker.setMap(null); }"
        page << "var myLatlng = new google.maps.LatLng(#{pos.first[:lat]},#{pos.first[:lng]});"
        page << "marker = new google.maps.Marker({position: myLatlng, map: Gmaps4Rails.map});"
                    page << "Gmaps4Rails.map.centerAndZoom(myLatlng, 0);"

    end
end

end
but I get an error because the method centerAndZoom is not defined into gmaps4rails.js, should i define it into the js or there is another way?
Thanks

A:

Gmaps4rails is a wrapper, so google objects keep all their properties.
Your line Gmaps4Rails.map.centerAndZoom(myLatlng, 0); doesn't work since centerAndZoom isn't a valid method (where did you find it? the reference is here: http://code.google.com/apis/maps/documentation/javascript/reference.html )
Otherwise, your question is already answered here: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Dynamic-%28or-Ajax%29-map-refresh
You can use either replace_markers or add_markers.
And you can adapt the settings of your map to center it the way you want: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Map
See auto_adjust in particular.

