Auto-zoom a map using a CoffeeScript class

suggest change

Given an App.GoogleMap CoffeeScript class that stores the google.maps.Map as @map and the google.maps.Markers as @markers, fit the map so every marker is visible:

# app/assets/javascripts/google_maps.js.coffee
# ...
class App.GoogleMap
  # ...
  bounds: {}

  constructor: (map_div)->
    # ...
    @auto_zoom()

  auto_zoom: ->
    @init_bounds()
    # TODO: Maybe, adjust the zoom to have a maximum or 
    # minimum zoom level, here.

  init_bounds: ->
    @bounds = new google.maps.LatLngBounds()
    for marker in @markers
      @bounds.extend marker.position
    @map.fitBounds @bounds

See Google’s LatLngBounds documentation.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents