Permalink
Browse files

Auto-size the dynamic map

pass size info in like :size => '50x50', not as :width and :height.  breaks backwards compat, though could support both ways
move sizing logic from static map to builder
  • Loading branch information...
1 parent a419ea1 commit 6cd5abbb0010312bb8f86245d8002b3406451c50 @ratbeard ratbeard committed Feb 25, 2011
Showing with 37 additions and 26 deletions.
  1. +1 −2 coffeescripts/gmap.coffee
  2. +5 −3 javascripts/gmap.js
  3. +27 −13 lib/bhm/google_maps/builder.rb
  4. +4 −8 lib/bhm/google_maps/static_map.rb
@@ -115,7 +115,7 @@
# Is loaded
container = $(container).css 'position', 'relative'
staticMap = container.find('img:first').css({position: 'absolute', top: 0, 'z-index': 9999})
- dynamicMap = $('<div class="dynamic-google-map" />').appendTo container
+ dynamicMap = $('<div class="dynamic-google-map" />').css({height: '100%', width: '100%'}).appendTo container
# Get locations data
locations = map.locationDataForMap(container)
# Start setting up the map / create a map element.
@@ -128,7 +128,6 @@
google.maps.event.addListenerOnce currentMap, 'tilesloaded', () ->
container.removeClass('static-google-map')
staticMap.remove()
- dynamicMap.css 'z-index', 'auto'
# Store map reference
map.maps.push currentMap
currentMap
View
Oops, something went wrong.
@@ -1,6 +1,10 @@
module BHM
module GoogleMaps
+ DEFAULT_WIDTH = 540
+ DEFAULT_HEIGHT = 400
+ DEFAULT_SIZE = "#{DEFAULT_WIDTH}x#{DEFAULT_HEIGHT}"
+
class Location
def initialize(object)
@lat, @lng = BHM::GoogleMaps.address_to_lat_lng_proc.call(object)
@@ -17,7 +21,18 @@ def initialize(template, addresses, options)
@options = options.symbolize_keys
@marker_options = @options.delete(:marker) || {}
@static = @options.delete(:static)
- @css_class = "#{BHM::GoogleMaps.container_class} #{BHM::GoogleMaps.static_map_class} #{@options.delete(:class)}"
+ @width, @height, @size = determine_size(options)
+ css_class = "#{BHM::GoogleMaps.container_class} #{BHM::GoogleMaps.static_map_class} #{@options.delete(:class)}"
+ @container_html_options = {
+ :class => css_class,
+ :style => "width: #{@width}px; height: #{@height}px"
+ }
+ end
+
+ def determine_size(options)
+ size = options.delete(:size) || DEFAULT_SIZE
+ w, h = size.split('x')
+ return w, h, size
end
def to_html
@@ -32,43 +47,42 @@ def build_static_map
end
def build_container(image)
- container_options = { :class => @css_class }
if selector = @options.delete(:location_data_selector)
# Lat/Lng data is embedded elsewhere in the page
- container_options[:'data-locations-selector'] = selector
+ container_html_options[:'data-locations-selector'] = selector
elsif @addresses.length == 1
- embed_location_data_for_location(container_options)
+ embed_location_data_for_location
else
- embed_location_data_for_locations(container_options)
+ embed_location_data_for_locations
end
#Pass along users html options
- #container_options.reverse_merge!(@options)
- @template.content_tag(:div, image, container_options)
+ #container_html_options.reverse_merge!(@options)
+ @template.content_tag(:div, image, @container_html_options)
end
- def embed_location_data_for_location(container_options)
+ def embed_location_data_for_location
lat, lng = @addresses.first.lat, @addresses.first.lng
- container_options.merge! 'data-latitude' => lat, 'data-longitude' => lng
+ @container_html_options.merge! 'data-latitude' => lat, 'data-longitude' => lng
#@marker_options[:title] ||= self.address_as_string
@marker_options.each_pair do |k, v|
- container_options[:"data-marker-#{k.to_s.dasherize}"] = v
+ @container_html_options[:"data-marker-#{k.to_s.dasherize}"] = v
end
end
- def embed_location_data_for_locations(container_options)
+ def embed_location_data_for_locations
latitudes, longitudes = [], []
@addresses.each do |address|
latitudes << address.lat
longitudes << address.lng
end
- container_options.merge!(
+ @container_html_options.merge!(
'data-latitude' => latitudes.join(', '),
'data-longitude' => longitudes.join(', ')
)
#@marker_options[:title] ||= self.address_as_string
#@marker_options.each_pair do |k, v|
- #container_options[:"data-marker-#{k.to_s.dasherize}"] = v
+ #container_html_options[:"data-marker-#{k.to_s.dasherize}"] = v
#end
end
@@ -7,11 +7,9 @@ class StaticMap
def initialize(addresses, options = {})
@addresses = addresses
- @width = options.fetch(:width, 540)
- @height = options.fetch(:height, 400)
@params = {
:sensor => false,
- :size => "#{@width}x#{@height}",
+ :size => options[:size],
:maptype => options.fetch(:type, "roadmap")
}
zoom = options.fetch(:zoom, @addresses.length > 1 ? nil : 15)
@@ -32,14 +30,12 @@ def to_url
def build_marker_params
return "markers=#{to_ll @addresses.first}" if @addresses.size == 1
- params = []
- @addresses.each_with_index do |address, index|
+ @addresses.each_with_index.map do |address, index|
return "markers=#{to_ll @addresses.first}" if @addresses.size == 1
color = COLOURS[index % COLOURS.size] if @cycle_color
label = LABELS[index % LABELS.size] if @cycle_label
- params << "markers=color:#{color}|label:#{label}|#{to_ll(address)}"
- end
- params.join("&")
+ "markers=color:#{color}|label:#{label}|#{to_ll(address)}"
+ end.join("&")
end
def to_ll(address)

0 comments on commit 6cd5abb

Please sign in to comment.