Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.

Commit

Permalink
Can embed location information for multiple locations in data- attrib…
Browse files Browse the repository at this point in the history
…utes.
  • Loading branch information
ratbeard committed Feb 25, 2011
1 parent b96e1da commit 45cba9e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
25 changes: 20 additions & 5 deletions coffeescripts/gmap.coffee
Expand Up @@ -61,10 +61,13 @@
#
# Set `data-longitude` and `data-latitude` on the map directly element
map.locationDataForMap = ($e) ->
longitude = getData($e, 'longitude')
if selector = getData($e, 'locations-selector')
locations = map.locationsDataFromElements(selector)
else if hasData($e, 'longitude')
else if longitude.indexOf(',') > -1 # multiple locations
locations = map.locationsDataFromDataAttributes($e)
else if longitude
locations = map.locationDataFromDataAttributes($e)
else
throw "dont have any map location data"
# Add gmap point object to each location
Expand All @@ -74,13 +77,25 @@

# Default strategy to fetch location data,
# Get a single location's data from element's `data-` attributes
map.locationDataFromDataAttributes = ($e) ->
[map.readDataAttributes($e)]

# Get Multiple locations embedded in `data-` attributes like:
# data-latitude="15,20.99" data-longitude="99.9,40.72"
map.locationsDataFromDataAttributes = ($e) ->
[
attrs = map.readDataAttributes($e)
lats = attrs.lat.split ', '
lngs = attrs.lng.split ', '
result = for lat, i in lats
{lat: lat, lng: lngs[i]}

map.readDataAttributes = ($e) ->
result =
id: $e.attr("id") || "#{map.autoIDPrefix}#{map.count++}"
lat: +getData($e, "latitude")
lng: +getData($e, "longitude")
lat: getData($e, "latitude")
lng: getData($e, "longitude")
title: getData($e, 'marker-title')
]


# Fetch location data from markup
# Kinda based on hcard/geo
Expand Down
41 changes: 32 additions & 9 deletions javascripts/gmap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions lib/bhm/google_maps/builder.rb
Expand Up @@ -37,6 +37,8 @@ def build_container(image)
container_options[:'data-locations-selector'] = selector
elsif @addresses.length == 1
embed_location_data_for_location(container_options)
else
embed_location_data_for_locations(container_options)
end

#Pass along users html options
Expand All @@ -53,6 +55,23 @@ def embed_location_data_for_location(container_options)
end
end

def embed_location_data_for_locations(container_options)
latitudes, longitudes = [], []
@addresses.each do |address|
latitudes << address.lat
longitudes << address.lng
end
container_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
#end
end


def alt_text
if (count = @addresses.length) > 1
@template.pluralize(count, "address") + " plotted on a map"
Expand Down
4 changes: 2 additions & 2 deletions lib/bhm/google_maps/helper.rb
Expand Up @@ -3,13 +3,13 @@ module GoogleMaps
module Helper

def include_gmap_js
@using_gmaps_js ||= begin
@included_map_js ||= begin
BHM::GoogleMaps.include_js_proc.call(self)
true
end
end

# Draw a dynamic map of 1 or more locations
# Draw a google map of 1 or more locations
def gmap(addresses, options={})
include_gmap_js unless options[:static]
BHM::GoogleMaps::Builder.new(self, addresses, options).to_html
Expand Down

0 comments on commit 45cba9e

Please sign in to comment.