Matthew Berman
Matthew Berman

Reputation: 8631

Google Maps for Rails - Center on marker always max zoomed

I am using Google maps for rails and I have everything setup properly but I can't seem to figure out how to set the default zoom number. I tried setting it in gmaps4rails.base.js.coffee here:

@default_map_options = 
      id: 'map'
      draggable: true
      detect_location: false  # should the browser attempt to use geolocation detection features of HTML5?
      center_on_user: false   # centers map on the location detected through the browser
      center_latitude: 0
      center_longitude: 0 
      zoom: 7
      maxZoom: null
      minZoom: null
      auto_adjust : true      # adjust the map to the markers if set to true
      auto_zoom: true         # zoom given by auto-adjust
      bounds: []              # adjust map to these limits. Should be [{"lat": , "lng": }]    
      raw: {}                  # raw json to pass additional options

but i dont think that worked properly. Is there a way to add the zoom number when creating the map:

<%= gmaps(:markers => {:data => @json, 
                      :options => {:raw => '{ animation: google.maps.Animation }' } },
                      :map_options => { :raw => '{ disableDefaultUI: false, 
                                                   scrollwheel: true }' }) %>

nothing I do seems to do anything, it is always zoomed all the way in. thanks!

Upvotes: 3

Views: 1167

Answers (3)

Stuart C
Stuart C

Reputation: 237

Not sure exactly what you are trying to accomplish but my challenge was that with auto zoom it would zoom in so far that the perspective was all wrong. My solution was to change the max zoom value in the base coffee script. Snippet below. Note the auto remains true.

  center_longitude: 0
  zoom: 7
  maxZoom: 18
  minZoom: null
  auto_adjust : true      # adjust the map to the markers if set to true
  auto_zoom: true         # zoom given by auto-adjust
  bounds: []              # adjust map to these limits. Should be [{"lat": , "lng": }]
  raw: {}                  # raw json to pass additional options

The other haml solution mention in the answers also works for a single page.

Upvotes: 0

ExiRe
ExiRe

Reputation: 4767

Here is my solution, which works (note that i use haml. Use <%= %> for erb):

= gmaps("map_options" => { "detect_location" => true, "center_on_user" => true, "auto_zoom" => false, "zoom" => 16},"markers" => { "data" => @json })

Upvotes: 2

Mahmoud Khaled
Mahmoud Khaled

Reputation: 6276

You need to set :auto_zoom => false, :zoom => value

<%= gmaps(:markers => {:data => @json, 
                  :options => {:raw => '{ animation: google.maps.Animation }' } },
                  :map_options => { :raw => '{ disableDefaultUI: false, 
                                               scrollwheel: true,
                                               auto_zoom => false,
                                               zoom => 10 }' }) %>

Upvotes: 1

Related Questions