Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmaps4rails V2 - change default zoom

I'm a complete noob, building my first rails app. I successfully implemented Google-maps-for-rails V2, using apneadiving's tutorial on youtube: http://www.youtube.com/watch?v=R0l-7en3dUw&feature=youtu.be

My issue is that for each of my maps, I'm only showing one marker, and when the map loads, it adjusts to be fully zoomed.

Searching around, there seems to be a lot of solutions for earlier versions of Gmaps4rails, but with V2, and creating the map via javascript, I can't seem to find a solution that works.

For reference, my code is below:

View:

    <script type="text/javascript">
      handler = Gmaps.build('Google');
      handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
      markers = handler.addMarkers(<%=raw @hash.to_json %>);
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();
      }); </script> 

Controller:

def show
  @pins = Pin.find(params[:id])
  @hash = Gmaps4rails.build_markers(@pins) do |pin, marker|
    marker.lat pin.latitude
    marker.lng pin.longitude
  end
end

I tried making changes via the console using: gmap.setzoom(12), as suggested by some post, but haven't had any luck with that .

Any help is much appreciated

Answer that worked for me: change view to:

  <script type="text/javascript">
  handler = Gmaps.build('Google');
  handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers(<%=raw @hash.to_json %>);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
  handler.getMap().setZoom(12);
  }); </script> 

Thanks for the help!

like image 428
dmt2989 Avatar asked Nov 12 '13 00:11

dmt2989


1 Answers

remove:

handler.fitMapToBounds();

Replace with:

handler.getMap().setZoom(yourValue);
like image 88
apneadiving Avatar answered Oct 20 '22 20:10

apneadiving