23

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!

apneadiving
  • 114,565
  • 26
  • 219
  • 213
dmt2989
  • 1,610
  • 3
  • 17
  • 30

3 Answers3

35

remove:

handler.fitMapToBounds();

Replace with:

handler.getMap().setZoom(yourValue);
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • 11
    Thanks! I ended up needing both: handler.fitMapToBounds(); handler.getMap().setZoom(12); otherwise the map wasn't centering on the correct location. Appreciate the help. – dmt2989 Nov 13 '13 at 00:49
  • @apneadiving - I'd like to piggy back this question / answer. Is there a way to set min/max zoom for the default map options without having to edit the source code? – Dudo Dec 31 '13 at 06:13
  • @Mallanaga you can inject your own builder, so you dont change the source, its explained in the wiki – apneadiving Dec 31 '13 at 09:33
  • 1
    zoom 0 is fully zoomed out (world view), 21 is fully zoomed in (street view) – raddrick Nov 28 '14 at 00:34
  • 1
    You should NOT replace with, but add "handler.getMap().setZoom(yourValue);" – Evgeny Palguev Oct 01 '15 at 10:33
0
Just Try This,

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

OR

handler.getMap().setZoom(yourValue);

It Should work.....

Rajesh Gurbani
  • 211
  • 1
  • 6
  • 18
  • Thanks for the reply. Unfortunately that didn't solve it. Still reverts to max zoom. Any other suggestions? – dmt2989 Nov 12 '13 at 19:33
0

Another option is to use handler.map.centerOn(marker):

      handler = Gmaps.build('Google');
      handler.buildMap({
      provider: {
        draggable: false,
        Zoom: 15
      },
      internal: {
        id: 'map'
      }
    },
    function(){
      marker = handler.addMarker(
        {
          "lat": lat,
          "lng": lng,
          "picture": {
            "url": "https://addons.cdn.mozilla.net/img/uploads/addon_icons/13/13028-64.png",
            "width":  36,
            "height": 36
          },
          "infowindow": "hello!"
        }
      );
      handler.map.centerOn(marker);
    }
  );
TenJack
  • 1,594
  • 4
  • 21
  • 35