From 45cba9ef22c0a7d7c842d382c12f52d570e8711b Mon Sep 17 00:00:00 2001 From: Mike Frawley Date: Fri, 25 Feb 2011 08:48:19 -0600 Subject: [PATCH] Can embed location information for multiple locations in data- attributes. --- coffeescripts/gmap.coffee | 25 ++++++++++++++++----- javascripts/gmap.js | 41 ++++++++++++++++++++++++++-------- lib/bhm/google_maps/builder.rb | 19 ++++++++++++++++ lib/bhm/google_maps/helper.rb | 4 ++-- 4 files changed, 73 insertions(+), 16 deletions(-) diff --git a/coffeescripts/gmap.coffee b/coffeescripts/gmap.coffee index ce22783..3831520 100644 --- a/coffeescripts/gmap.coffee +++ b/coffeescripts/gmap.coffee @@ -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 @@ -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 diff --git a/javascripts/gmap.js b/javascripts/gmap.js index 69ccb42..42b41d7 100644 --- a/javascripts/gmap.js +++ b/javascripts/gmap.js @@ -47,11 +47,14 @@ }); }; map.locationDataForMap = function($e) { - var loc, locations, selector, _i, _len; + var loc, locations, longitude, selector, _i, _len; + longitude = getData($e, 'longitude'); if (selector = getData($e, 'locations-selector')) { locations = map.locationsDataFromElements(selector); - } else if (hasData($e, 'longitude')) { + } else if (longitude.indexOf(',') > -1) { locations = map.locationsDataFromDataAttributes($e); + } else if (longitude) { + locations = map.locationDataFromDataAttributes($e); } else { throw "dont have any map location data"; } @@ -61,15 +64,35 @@ } return locations; }; + map.locationDataFromDataAttributes = function($e) { + return [map.readDataAttributes($e)]; + }; map.locationsDataFromDataAttributes = function($e) { - return [ - { - id: $e.attr("id") || ("" + map.autoIDPrefix + (map.count++)), - lat: +getData($e, "latitude"), - lng: +getData($e, "longitude"), - title: getData($e, 'marker-title') + var attrs, i, lat, lats, lngs, result; + attrs = map.readDataAttributes($e); + lats = attrs.lat.split(', '); + lngs = attrs.lng.split(', '); + return result = (function() { + var _len, _results; + _results = []; + for (i = 0, _len = lats.length; i < _len; i++) { + lat = lats[i]; + _results.push({ + lat: lat, + lng: lngs[i] + }); } - ]; + return _results; + })(); + }; + map.readDataAttributes = function($e) { + var result; + return result = { + id: $e.attr("id") || ("" + map.autoIDPrefix + (map.count++)), + lat: getData($e, "latitude"), + lng: getData($e, "longitude"), + title: getData($e, 'marker-title') + }; }; map.locationsDataFromElements = function(selector) { return $(selector).map(function() { diff --git a/lib/bhm/google_maps/builder.rb b/lib/bhm/google_maps/builder.rb index 836bfa3..61e6f6f 100644 --- a/lib/bhm/google_maps/builder.rb +++ b/lib/bhm/google_maps/builder.rb @@ -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 @@ -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" diff --git a/lib/bhm/google_maps/helper.rb b/lib/bhm/google_maps/helper.rb index c4d869f..9a3e4a2 100644 --- a/lib/bhm/google_maps/helper.rb +++ b/lib/bhm/google_maps/helper.rb @@ -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