bhm-google-maps 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +11 -0
- data/LICENSE +27 -0
- data/README.md +93 -0
- data/Rakefile +66 -0
- data/bhm-google-maps.gemspec +62 -0
- data/coffeescripts/gmap.coffee +88 -0
- data/javascripts/gmap.js +105 -0
- data/lib/bhm/google_maps/builder.rb +57 -0
- data/lib/bhm/google_maps/helper.rb +37 -0
- data/lib/bhm/google_maps/static_map.rb +60 -0
- data/lib/bhm/google_maps/tasks/bhm_google_maps.rake +9 -0
- data/lib/bhm/google_maps/version.rb +5 -0
- data/lib/bhm/google_maps.rb +46 -0
- data/lib/bhm-google-maps.rb +1 -0
- data/test/builder_test.rb +4 -0
- data/test/helper_test.rb +48 -0
- data/test/support/misc_helpers.rb +21 -0
- data/test/test_helper.rb +21 -0
- metadata +84 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright (c) 2010, Youth Tree
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
3. All advertising materials mentioning features or use of this software
|
12
|
+
must display the following acknowledgement:
|
13
|
+
This product includes software developed by the Youth Tree.
|
14
|
+
4. Neither the name of the Youth Tree nor the
|
15
|
+
names of its contributors may be used to endorse or promote products
|
16
|
+
derived from this software without specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY YOUTH TREE ''AS IS'' AND ANY
|
19
|
+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL YOUTH TREE BE LIABLE FOR ANY
|
22
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
23
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
24
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
25
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
26
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
27
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# BHM Google Maps #
|
2
|
+
|
3
|
+
Helpers for Google Maps v3 in Rails - Using html 5, the google maps api v3 and the static maps api
|
4
|
+
Supports unobtrusive maps in Rails 3 (and 2.3, but preferably 3) using the new google maps v3 api.
|
5
|
+
|
6
|
+
## Installation ##
|
7
|
+
|
8
|
+
Installing bhm-google-maps is simple. To get started with a rails 3 app:
|
9
|
+
|
10
|
+
1. Add the gem to your Gemfile: Add `gem "bhm-google-maps"`
|
11
|
+
2. Generate the js into your app: `rake bhm-google-maps:install`
|
12
|
+
3. Configure as needed
|
13
|
+
|
14
|
+
Some parts of bhm-google-maps may need to be configured - e.g. out of the box, it will append the google
|
15
|
+
maps js url to the page and the gmap.js from where it was called, as an example of changing this, we suggest
|
16
|
+
creating `config/initializers/bhm_google_maps_config.rb` and configuring from there. e.g:
|
17
|
+
|
18
|
+
# t is the scope of the view / helpers.
|
19
|
+
# This will append it to a content_for section
|
20
|
+
# instead of directly in the page.
|
21
|
+
BHM::GoogleMaps.include_js_proc = proc do |t|
|
22
|
+
t.content_for :extra_head, t.javascript_include_tag(t.google_maps_url(false), "gmap.js")
|
23
|
+
end
|
24
|
+
|
25
|
+
For a fuller / more in depth reference, see configuration below.
|
26
|
+
|
27
|
+
## Usage ##
|
28
|
+
|
29
|
+
Currently, bhm-google-maps is designed to show a single map w/ static fallback. To do this,
|
30
|
+
you use the `draw_map_of` helper. You pass it an object you wish to plot, typically following the
|
31
|
+
convention that:
|
32
|
+
|
33
|
+
1. `object.lat` and `object.lng` return latitude / longitude respectively
|
34
|
+
2. `object.to_s` returns the string representing the address / the address
|
35
|
+
|
36
|
+
Of course, this can be changed via other conventions. For this example, we'll use the following
|
37
|
+
class demo:
|
38
|
+
|
39
|
+
class Location
|
40
|
+
attr_accessor :address, :lat, :lng
|
41
|
+
|
42
|
+
def initialize(address, lat, lng)
|
43
|
+
@address = address
|
44
|
+
@lat = lat
|
45
|
+
@lng = lng
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_s; address.to_s; end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
Then, in your view, you could simple call:
|
53
|
+
|
54
|
+
<%= draw_map_of Location.new("My House", 12.345, 56.789) %>
|
55
|
+
|
56
|
+
Optionally, `draw_map_of` accepts a hash of options for:
|
57
|
+
|
58
|
+
* `:static_map_html` - options to pass to the image\_tag for the static map.
|
59
|
+
* `:static_map` - options to pass to the BHM::GoogleMaps::StaticMap constructor. These include `:type`, `:width` and `:height`
|
60
|
+
* `:marker` - options to pass to the `google.maps.Marker` in js.
|
61
|
+
|
62
|
+
You also get the following helpers:
|
63
|
+
|
64
|
+
* `using_gmaps_js?` - whether or not the embed method has been called
|
65
|
+
* `use_gmaps_js` - calls your embed method for the js - see configuration.
|
66
|
+
* `google_maps_url(sensor = false)` - returns the url for the google maps api v3.
|
67
|
+
* `static_map_of_addresses(addresses)` - returns an image tag for an array of addresses.
|
68
|
+
* `static_map_of_address(address)` - returns an image tag for a given address
|
69
|
+
|
70
|
+
## Configuration ##
|
71
|
+
|
72
|
+
For configuration purposes, there are a set of options on `BHM::GoogleMaps`. You can
|
73
|
+
set them via `BHM::GoogleMaps.option_name = value`
|
74
|
+
|
75
|
+
* `container_class` - the map class for the container div, defaults to gmap. If changed, you must update the js.
|
76
|
+
* `static_map_class` - the class of the container div, removed via js when made dynamic.
|
77
|
+
* `include_js_proc` - how to embed js in the page (e.g. using content\_for), defaults to using concat. Is called as a proc w/ the template passed as the only argument.
|
78
|
+
* `address_to_s_proc` - passed an address, converts it to a string (defaults to to_s)
|
79
|
+
* `address_to_lat_lng_proc` - passed an address, returns an array w/ lat and lng.
|
80
|
+
|
81
|
+
Ideally these options should be set in an initializer.
|
82
|
+
|
83
|
+
## Note on Patches/Pull Requests ##
|
84
|
+
|
85
|
+
1. Fork the project.
|
86
|
+
2. Make your feature addition or bug fix.
|
87
|
+
3. Add tests for it. This is important so I don't break it in a future version unintentionally.
|
88
|
+
4. Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
89
|
+
5. Send me a pull request. Bonus points for topic branches.
|
90
|
+
|
91
|
+
## Copyright ##
|
92
|
+
|
93
|
+
Copyright (c) 2010 Youth Tree. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
require File.expand_path("./lib/bhm/google_maps/version", File.dirname(__FILE__))
|
6
|
+
|
7
|
+
desc "Default: run unit tests."
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'jeweler'
|
12
|
+
Jeweler::Tasks.new do |gem|
|
13
|
+
gem.name = "bhm-google-maps"
|
14
|
+
gem.summary = "Helpers for Google Maps v3 in Rails - Using html 5, the google maps api v3 and the static maps api"
|
15
|
+
gem.description = "A set of helpers and javascript files that makes it trivial to implement google maps unobtrusively in an application."
|
16
|
+
gem.email = "darcy.laycock@youthtree.org.au"
|
17
|
+
gem.homepage = "http://github.com/YouthTree/bhm-google-maps"
|
18
|
+
gem.authors = ["Darcy Laycock"]
|
19
|
+
gem.version = BHM::GoogleMaps::VERSION
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
24
|
+
end
|
25
|
+
|
26
|
+
desc "Compiles the javascript from Coffeescript to Javascript"
|
27
|
+
task :compile_scripts do
|
28
|
+
system "coffee --no-wrap -c coffeescripts/*.coffee -o javascripts/"
|
29
|
+
end
|
30
|
+
|
31
|
+
desc "Interactively compiles coffeescripts as they're changed"
|
32
|
+
task :watch_scripts do
|
33
|
+
system "coffee --no-wrap -w -c coffeescripts/*.coffee -o javascripts/"
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Compile scripts, and produce a minified version."
|
37
|
+
task :build_scripts => [:compile_scripts] do
|
38
|
+
require 'closure-compiler'
|
39
|
+
build_prefix = "build/#{BHM::GoogleMaps::VERSION}"
|
40
|
+
FileUtils.mkdir_p build_prefix
|
41
|
+
Dir["javascripts/*.js"].each do |js|
|
42
|
+
new_name = js.gsub(/^javascripts\//, build_prefix + "/").gsub(/\.js$/, "-#{BHM::GoogleMaps::VERSION}.js")
|
43
|
+
FileUtils.cp js, new_name
|
44
|
+
min_name = new_name.gsub(/\.js$/, '.min.js')
|
45
|
+
File.open(min_name, "w+") do |f|
|
46
|
+
f.write Closure::Compiler.new.compile(File.read(js))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
desc "Generate docs for the bhm-google-maps plugin"
|
52
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "bhm-google-maps"
|
55
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
56
|
+
rdoc.rdoc_files.include('README.md')
|
57
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
58
|
+
end
|
59
|
+
|
60
|
+
desc "Test the bhm-google-maps plugin"
|
61
|
+
Rake::TestTask.new(:test) do |test|
|
62
|
+
test.libs << 'lib'
|
63
|
+
test.libs << 'test'
|
64
|
+
test.pattern = 'test/**/*_test.rb'
|
65
|
+
test.verbose = true
|
66
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{bhm-google-maps}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Darcy Laycock"]
|
12
|
+
s.date = %q{2010-04-26}
|
13
|
+
s.description = %q{A set of helpers and javascript files that makes it trivial to implement google maps unobtrusively in an application.}
|
14
|
+
s.email = %q{darcy.laycock@youthtree.org.au}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.md",
|
24
|
+
"Rakefile",
|
25
|
+
"bhm-google-maps.gemspec",
|
26
|
+
"coffeescripts/gmap.coffee",
|
27
|
+
"javascripts/gmap.js",
|
28
|
+
"lib/bhm-google-maps.rb",
|
29
|
+
"lib/bhm/google_maps.rb",
|
30
|
+
"lib/bhm/google_maps/builder.rb",
|
31
|
+
"lib/bhm/google_maps/helper.rb",
|
32
|
+
"lib/bhm/google_maps/static_map.rb",
|
33
|
+
"lib/bhm/google_maps/tasks/bhm_google_maps.rake",
|
34
|
+
"lib/bhm/google_maps/version.rb",
|
35
|
+
"test/builder_test.rb",
|
36
|
+
"test/helper_test.rb",
|
37
|
+
"test/support/misc_helpers.rb",
|
38
|
+
"test/test_helper.rb"
|
39
|
+
]
|
40
|
+
s.homepage = %q{http://github.com/YouthTree/bhm-google-maps}
|
41
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
42
|
+
s.require_paths = ["lib"]
|
43
|
+
s.rubygems_version = %q{1.3.6}
|
44
|
+
s.summary = %q{Helpers for Google Maps v3 in Rails - Using html 5, the google maps api v3 and the static maps api}
|
45
|
+
s.test_files = [
|
46
|
+
"test/builder_test.rb",
|
47
|
+
"test/helper_test.rb",
|
48
|
+
"test/support/misc_helpers.rb",
|
49
|
+
"test/test_helper.rb"
|
50
|
+
]
|
51
|
+
|
52
|
+
if s.respond_to? :specification_version then
|
53
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
54
|
+
s.specification_version = 3
|
55
|
+
|
56
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
57
|
+
else
|
58
|
+
end
|
59
|
+
else
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# GMap is a simply, unobtrusive google map adder for jQuery + HTML5.
|
2
|
+
# It uses data attributes to store map locations, works with a rails
|
3
|
+
# plugin to generate static alternatives and most importantly is simple.
|
4
|
+
GMap: (($) ->
|
5
|
+
|
6
|
+
# The actual object we'll be exporting.
|
7
|
+
map: {}
|
8
|
+
|
9
|
+
# Map and marker options to autoextract.
|
10
|
+
mapOptionKeys: ["zoom", "title"]
|
11
|
+
markerOptionKeys: ["title"]
|
12
|
+
|
13
|
+
# Exposed options / data.
|
14
|
+
map.count: 0
|
15
|
+
map.autoIDPrefix: "gmap-"
|
16
|
+
map.maps: []
|
17
|
+
|
18
|
+
# Use a function for the roadmap value to avoid load order issues.
|
19
|
+
# e.g. if the google maps library is loaded after this one.
|
20
|
+
map.defaultOptions: {
|
21
|
+
zoom: 15
|
22
|
+
mapTypeId: -> google.maps.MapTypeId.ROADMAP
|
23
|
+
scroolwheel: false
|
24
|
+
}
|
25
|
+
|
26
|
+
# Very, very simple wrapper for generating a data key - tbh possibly un-needed.
|
27
|
+
dataKey: (key, spacer) ->
|
28
|
+
spacer?= ""
|
29
|
+
"data-$spacer$key"
|
30
|
+
|
31
|
+
# Returns true iff the given element has the set data element, false otherwise.
|
32
|
+
hasData: (e, key, spacer) ->
|
33
|
+
e.is "[${dataKey key, spacer}]"
|
34
|
+
|
35
|
+
# Return the value of a given data attribute.
|
36
|
+
getData: (e, key, spacer) ->
|
37
|
+
e.attr dataKey(key, spacer)
|
38
|
+
|
39
|
+
# Merges a given object w/ values retrieved using data element.
|
40
|
+
# automatically namespaces keys etc.
|
41
|
+
mergeDataOptions: (element, options, keys, spacer) ->
|
42
|
+
for key in keys
|
43
|
+
options[key] = getData(element, key, spacer) if hasData element, key, spacer
|
44
|
+
|
45
|
+
# Return the map options for a given element.
|
46
|
+
mapOptionsForElement: (element) ->
|
47
|
+
options: $.extend {}, map.defaultOptions
|
48
|
+
# Set the mapTypeId if it's a function
|
49
|
+
options.mapTypeId: options.mapTypeId() if $.isFunction options.mapTypeId
|
50
|
+
mergeDataOptions element, options, mapOptionKeys
|
51
|
+
options
|
52
|
+
|
53
|
+
# Install the google map onto each option with the .gmap key.
|
54
|
+
map.install: ->
|
55
|
+
$('.gmap').each -> map.setupElement this
|
56
|
+
|
57
|
+
# Called with a single html dom element as an argument, will
|
58
|
+
# automatically set it up as a google map.
|
59
|
+
map.setupElement: (e) ->
|
60
|
+
$e: $ e
|
61
|
+
id: $e.attr "id"
|
62
|
+
$e.attr "id", "${map.autoIDPrefix}${map.count++}" unless id?
|
63
|
+
# Get the position of the current marker.
|
64
|
+
lat: Number getData($e, "latitude")
|
65
|
+
lng: Number getData($e, "longitude")
|
66
|
+
point: new google.maps.LatLng lat, lng
|
67
|
+
# Start setting up the map / create a map element.
|
68
|
+
mapOptions: mapOptionsForElement $e
|
69
|
+
mapOptions.center: point
|
70
|
+
$e.empty().addClass('dynamic-google-map').removeClass('static-google-map')
|
71
|
+
currentMap: new google.maps.Map e, mapOptions
|
72
|
+
map.maps.push currentMap
|
73
|
+
# Now, we need to finally add the marker to the map.
|
74
|
+
markerOptions: {
|
75
|
+
position: point
|
76
|
+
map: currentMap
|
77
|
+
}
|
78
|
+
mergeDataOptions $e, markerOptions, markerOptionKeys, "marker-"
|
79
|
+
marker: new google.maps.Marker markerOptions
|
80
|
+
currentMap
|
81
|
+
|
82
|
+
# On load, we'll install the maps.
|
83
|
+
$(document).ready -> map.install()
|
84
|
+
|
85
|
+
# Return map to set it up.
|
86
|
+
map
|
87
|
+
|
88
|
+
)(jQuery)
|
data/javascripts/gmap.js
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
var GMap;
|
2
|
+
// GMap is a simply, unobtrusive google map adder for jQuery + HTML5.
|
3
|
+
// It uses data attributes to store map locations, works with a rails
|
4
|
+
// plugin to generate static alternatives and most importantly is simple.
|
5
|
+
GMap = (function($) {
|
6
|
+
var dataKey, getData, hasData, map, mapOptionKeys, mapOptionsForElement, markerOptionKeys, mergeDataOptions;
|
7
|
+
// The actual object we'll be exporting.
|
8
|
+
map = {};
|
9
|
+
// Map and marker options to autoextract.
|
10
|
+
mapOptionKeys = ["zoom", "title"];
|
11
|
+
markerOptionKeys = ["title"];
|
12
|
+
// Exposed options / data.
|
13
|
+
map.count = 0;
|
14
|
+
map.autoIDPrefix = "gmap-";
|
15
|
+
map.maps = [];
|
16
|
+
// Use a function for the roadmap value to avoid load order issues.
|
17
|
+
// e.g. if the google maps library is loaded after this one.
|
18
|
+
map.defaultOptions = {
|
19
|
+
zoom: 15,
|
20
|
+
mapTypeId: function mapTypeId() {
|
21
|
+
return google.maps.MapTypeId.ROADMAP;
|
22
|
+
},
|
23
|
+
scroolwheel: false
|
24
|
+
};
|
25
|
+
// Very, very simple wrapper for generating a data key - tbh possibly un-needed.
|
26
|
+
dataKey = function dataKey(key, spacer) {
|
27
|
+
spacer = (typeof spacer !== "undefined" && spacer !== null) ? spacer : "";
|
28
|
+
return "data-" + spacer + key;
|
29
|
+
};
|
30
|
+
// Returns true iff the given element has the set data element, false otherwise.
|
31
|
+
hasData = function hasData(e, key, spacer) {
|
32
|
+
return e.is(("[" + (dataKey(key, spacer)) + "]"));
|
33
|
+
};
|
34
|
+
// Return the value of a given data attribute.
|
35
|
+
getData = function getData(e, key, spacer) {
|
36
|
+
return e.attr(dataKey(key, spacer));
|
37
|
+
};
|
38
|
+
// Merges a given object w/ values retrieved using data element.
|
39
|
+
// automatically namespaces keys etc.
|
40
|
+
mergeDataOptions = function mergeDataOptions(element, options, keys, spacer) {
|
41
|
+
var _a, _b, _c, _d, key;
|
42
|
+
_a = []; _c = keys;
|
43
|
+
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
44
|
+
key = _c[_b];
|
45
|
+
_a.push((function() {
|
46
|
+
if (hasData(element, key, spacer)) {
|
47
|
+
options[key] = getData(element, key, spacer);
|
48
|
+
return options[key];
|
49
|
+
}
|
50
|
+
})());
|
51
|
+
}
|
52
|
+
return _a;
|
53
|
+
};
|
54
|
+
// Return the map options for a given element.
|
55
|
+
mapOptionsForElement = function mapOptionsForElement(element) {
|
56
|
+
var options;
|
57
|
+
options = $.extend({}, map.defaultOptions);
|
58
|
+
// Set the mapTypeId if it's a function
|
59
|
+
if ($.isFunction(options.mapTypeId)) {
|
60
|
+
options.mapTypeId = options.mapTypeId();
|
61
|
+
}
|
62
|
+
mergeDataOptions(element, options, mapOptionKeys);
|
63
|
+
return options;
|
64
|
+
};
|
65
|
+
// Install the google map onto each option with the .gmap key.
|
66
|
+
map.install = function install() {
|
67
|
+
return $('.gmap').each(function() {
|
68
|
+
return map.setupElement(this);
|
69
|
+
});
|
70
|
+
};
|
71
|
+
// Called with a single html dom element as an argument, will
|
72
|
+
// automatically set it up as a google map.
|
73
|
+
map.setupElement = function setupElement(e) {
|
74
|
+
var $e, currentMap, id, lat, lng, mapOptions, marker, markerOptions, point;
|
75
|
+
$e = $(e);
|
76
|
+
id = $e.attr("id");
|
77
|
+
if (!((typeof id !== "undefined" && id !== null))) {
|
78
|
+
$e.attr("id", ("" + (map.autoIDPrefix) + (map.count++)));
|
79
|
+
}
|
80
|
+
// Get the position of the current marker.
|
81
|
+
lat = Number(getData($e, "latitude"));
|
82
|
+
lng = Number(getData($e, "longitude"));
|
83
|
+
point = new google.maps.LatLng(lat, lng);
|
84
|
+
// Start setting up the map / create a map element.
|
85
|
+
mapOptions = mapOptionsForElement($e);
|
86
|
+
mapOptions.center = point;
|
87
|
+
$e.empty().addClass('dynamic-google-map').removeClass('static-google-map');
|
88
|
+
currentMap = new google.maps.Map(e, mapOptions);
|
89
|
+
map.maps.push(currentMap);
|
90
|
+
// Now, we need to finally add the marker to the map.
|
91
|
+
markerOptions = {
|
92
|
+
position: point,
|
93
|
+
map: currentMap
|
94
|
+
};
|
95
|
+
mergeDataOptions($e, markerOptions, markerOptionKeys, "marker-");
|
96
|
+
marker = new google.maps.Marker(markerOptions);
|
97
|
+
return currentMap;
|
98
|
+
};
|
99
|
+
// On load, we'll install the maps.
|
100
|
+
$(document).ready(function() {
|
101
|
+
return map.install();
|
102
|
+
});
|
103
|
+
// Return map to set it up.
|
104
|
+
return map;
|
105
|
+
})(jQuery);
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module BHM
|
2
|
+
module GoogleMaps
|
3
|
+
class Builder
|
4
|
+
BasicMarker = Struct.new(:to_s, :lat, :lng)
|
5
|
+
|
6
|
+
def initialize(template, address, options)
|
7
|
+
@template = template
|
8
|
+
@address = address
|
9
|
+
@options = options.symbolize_keys
|
10
|
+
end
|
11
|
+
|
12
|
+
def build_static_map(marker_options)
|
13
|
+
ll = self.ll_pair
|
14
|
+
address = self.address_as_string
|
15
|
+
address_proxy = BasicMarker.new(address, ll[0], ll[1])
|
16
|
+
static_map_url = StaticMap.for_address(address_proxy, marker_options.merge(@options[:static_map] || {}))
|
17
|
+
@template.image_tag(static_map_url, {:alt => address}.reverse_merge(@options[:static_map_html] || {}))
|
18
|
+
end
|
19
|
+
|
20
|
+
def build_container
|
21
|
+
marker_options = @options[:marker] || {}
|
22
|
+
ll = self.ll_pair
|
23
|
+
address = self.address_as_string
|
24
|
+
container_options = {}
|
25
|
+
container_options['data-latitude'] = ll[0]
|
26
|
+
container_options['data-longitude'] = ll[1]
|
27
|
+
marker_options[:title] ||= address
|
28
|
+
marker_options.each_pair do |k, v|
|
29
|
+
container_options["data-marker-#{k.to_s.dasherize}"] = v
|
30
|
+
end
|
31
|
+
default_css_class = "#{BHM::GoogleMaps.container_class} #{BHM::GoogleMaps.static_map_class}"
|
32
|
+
container_options = merge_options_with_class(container_options, :class => default_css_class)
|
33
|
+
@template.content_tag(:div, build_static_map(marker_options), container_options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_html
|
37
|
+
@to_html ||= build_container
|
38
|
+
end
|
39
|
+
|
40
|
+
def ll_pair
|
41
|
+
BHM::GoogleMaps.address_to_lat_lng_proc.call(@address)
|
42
|
+
end
|
43
|
+
|
44
|
+
def address_as_string
|
45
|
+
BHM::GoogleMaps.address_to_s_proc.call(@address)
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
|
50
|
+
def merge_options_with_class(a, b)
|
51
|
+
a, b = (a || {}).stringify_keys, (b || {}).stringify_keys
|
52
|
+
css_class = [a['class'], b['class']].join(" ").squeeze(" ")
|
53
|
+
a.merge(b).merge('class' => css_class)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module BHM
|
2
|
+
module GoogleMaps
|
3
|
+
module Helper
|
4
|
+
|
5
|
+
def using_gmaps_js?
|
6
|
+
instance_variable_defined?(:@using_gmaps_js) && !!@using_gmaps_js
|
7
|
+
end
|
8
|
+
|
9
|
+
def google_maps_url(sensor = false)
|
10
|
+
"http://maps.google.com/maps/api/js?sensor=#{sensor}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def use_gmaps_js
|
14
|
+
return if using_gmaps_js?
|
15
|
+
BHM::GoogleMaps.include_js_proc.call(self)
|
16
|
+
@using_gmaps_js = true
|
17
|
+
end
|
18
|
+
|
19
|
+
def draw_map_of(address, options = {})
|
20
|
+
use_gmaps_js
|
21
|
+
BHM::GoogleMaps::Builder.new(self, address, options).to_html
|
22
|
+
end
|
23
|
+
|
24
|
+
# Given an array of addresses, will return an image an
|
25
|
+
# image tag with a static google map plotting those points.
|
26
|
+
def static_map_of_addresses(addresses, options = {})
|
27
|
+
image_tag(BHM::GoogleMaps::StaticMap.for_addresses(addresses, options), :alt => "#{pluralize addresses.size, "address"} plotted on a map")
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns an image map with a single address plotted on a single static google map.
|
31
|
+
def static_map_of_address(address, options = {})
|
32
|
+
image_tag(BHM::GoogleMaps::StaticMap.for_address(address, options), :alt => BHM::GoogleMaps.address_to_s_proc(address))
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module BHM
|
2
|
+
module GoogleMaps
|
3
|
+
class StaticMap
|
4
|
+
URL_TEMPLATE = "http://maps.google.com/maps/api/staticmap?%s"
|
5
|
+
COLOURS = %w(red white green brown blue orange gray purple yellow black)
|
6
|
+
LABELS = ('A'..'Z').to_a
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@addresses = []
|
10
|
+
@width, @height = options.fetch(:width, 540), options.fetch(:height, 400)
|
11
|
+
@params = Hash.new.tap do |p|
|
12
|
+
p[:sensor] = false
|
13
|
+
p[:size] = "#{@width}x#{@height}"
|
14
|
+
p[:maptype] = options.fetch(:type, "roadmap")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def <<(address)
|
19
|
+
@addresses << address
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_url
|
23
|
+
params = @params.to_param
|
24
|
+
params << "&"
|
25
|
+
params << build_marker_params
|
26
|
+
URL_TEMPLATE % params
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.for_address(address, opts = {})
|
30
|
+
map = self.new(opts.reverse_merge(:zoom => 15))
|
31
|
+
map << address
|
32
|
+
map.to_url
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.for_addresses(*addresses)
|
36
|
+
map = self.new(addresses.extract_options!)
|
37
|
+
addresses.flatten.each { |a| map << a }
|
38
|
+
map.to_url
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
def build_marker_params
|
44
|
+
params = []
|
45
|
+
@addresses.each_with_index do |address, index|
|
46
|
+
return "markers=#{to_ll @addresses.first}" if @addresses.size == 1
|
47
|
+
color = COLOURS[index % COLOURS.size]
|
48
|
+
label = LABELS[index % LABELS.size]
|
49
|
+
params << "markers=color:#{color}|label:#{label}|#{to_ll(address)}"
|
50
|
+
end
|
51
|
+
params.join("&")
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_ll(address)
|
55
|
+
"#{address.lat},#{address.lng}"
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module BHM
|
4
|
+
module GoogleMaps
|
5
|
+
|
6
|
+
autoload :StaticMap, 'bhm/google_maps/static_map'
|
7
|
+
autoload :Helper, 'bhm/google_maps/helper'
|
8
|
+
autoload :Builder, 'bhm/google_maps/builder'
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :container_class, :static_map_class, :include_js_proc,
|
12
|
+
:address_to_s_proc, :address_to_lat_lng_proc
|
13
|
+
end
|
14
|
+
|
15
|
+
self.container_class ||= "gmap"
|
16
|
+
self.static_map_class ||= "static-google-map"
|
17
|
+
self.include_js_proc ||= lambda { |t| t.concat(t.javascript_include_tag(t.google_maps_url(false), "gmap.js")) }
|
18
|
+
self.address_to_s_proc ||= lambda { |a| a.to_s }
|
19
|
+
self.address_to_lat_lng_proc ||= lambda { |a| [a.lat, a.lng] }
|
20
|
+
|
21
|
+
def self.configure
|
22
|
+
yield self if block_given?
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.install_helper!
|
26
|
+
::ActionView::Base.send(:include, BHM::GoogleMaps::Helper)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.install_js!
|
30
|
+
from = File.expand_path("../../javascripts/gmap.js", File.dirname(__FILE__))
|
31
|
+
if File.exist?(from) && defined?(Rails.root)
|
32
|
+
FileUtils.cp from, Rails.root.join("public", "javascripts", "gmap.js")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
self.install_helper! if defined?(::ActionView)
|
37
|
+
|
38
|
+
if defined?(Rails::Railtie)
|
39
|
+
class Railtie < Rails::Railtie
|
40
|
+
rake_tasks do
|
41
|
+
load File.expand_path('./google_maps/tasks/bhm_google_maps.rake', File.dirname(__FILE__))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'bhm/google_maps'
|
data/test/helper_test.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
class HelperTest < ActionView::TestCase
|
5
|
+
|
6
|
+
def setup_default_address
|
7
|
+
@address = Struct.new(:lat, :lng, :to_s).new(10.0, 10.0, "Some address")
|
8
|
+
end
|
9
|
+
|
10
|
+
setup :setup_default_address
|
11
|
+
|
12
|
+
test "should return false if not using google maps js" do
|
13
|
+
assert !using_gmaps_js?
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should let you use google maps js" do
|
17
|
+
use_gmaps_js
|
18
|
+
assert using_gmaps_js?
|
19
|
+
assert_select 'script'
|
20
|
+
end
|
21
|
+
|
22
|
+
test "should let you draw a map of an address" do
|
23
|
+
concat draw_map_of(@address)
|
24
|
+
assert_select '.gmap'
|
25
|
+
end
|
26
|
+
|
27
|
+
test "should automatically use gmap js when an address is drawn" do
|
28
|
+
assert !using_gmaps_js?
|
29
|
+
concat draw_map_of(@address)
|
30
|
+
assert using_gmaps_js?
|
31
|
+
end
|
32
|
+
|
33
|
+
test "should correctly add a container with an address" do
|
34
|
+
concat draw_map_of(@address)
|
35
|
+
assert_select ".#{BHM::GoogleMaps.container_class}"
|
36
|
+
end
|
37
|
+
|
38
|
+
test "should correctly add an image with an address" do
|
39
|
+
concat draw_map_of(@address)
|
40
|
+
assert_select ".#{BHM::GoogleMaps.static_map_class}"
|
41
|
+
end
|
42
|
+
|
43
|
+
test "should append a static map image" do
|
44
|
+
concat draw_map_of(@address)
|
45
|
+
assert_select ".#{BHM::GoogleMaps.static_map_class} img"
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module MiscHelpers
|
2
|
+
|
3
|
+
def assert_no_select(*args)
|
4
|
+
assert_raise Test::Unit::AssertionFailedError do
|
5
|
+
assert_select(*args)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def swap(object, new_values)
|
10
|
+
old_values = {}
|
11
|
+
new_values.each do |key, value|
|
12
|
+
old_values[key] = object.send key
|
13
|
+
object.send :"#{key}=", value
|
14
|
+
end
|
15
|
+
yield
|
16
|
+
ensure
|
17
|
+
old_values.each do |key, value|
|
18
|
+
object.send :"#{key}=", value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'redgreen' if RUBY_VERSION < '1.9'
|
4
|
+
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_view'
|
7
|
+
require 'action_view/template'
|
8
|
+
require 'action_view/test_case'
|
9
|
+
|
10
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
11
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
12
|
+
require 'bhm-google-maps'
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
|
15
|
+
|
16
|
+
class ActionView::TestCase
|
17
|
+
include MiscHelpers
|
18
|
+
|
19
|
+
tests BHM::GoogleMaps::Helper
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bhm-google-maps
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Darcy Laycock
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-26 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A set of helpers and javascript files that makes it trivial to implement google maps unobtrusively in an application.
|
22
|
+
email: darcy.laycock@youthtree.org.au
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.md
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- bhm-google-maps.gemspec
|
37
|
+
- coffeescripts/gmap.coffee
|
38
|
+
- javascripts/gmap.js
|
39
|
+
- lib/bhm-google-maps.rb
|
40
|
+
- lib/bhm/google_maps.rb
|
41
|
+
- lib/bhm/google_maps/builder.rb
|
42
|
+
- lib/bhm/google_maps/helper.rb
|
43
|
+
- lib/bhm/google_maps/static_map.rb
|
44
|
+
- lib/bhm/google_maps/tasks/bhm_google_maps.rake
|
45
|
+
- lib/bhm/google_maps/version.rb
|
46
|
+
- test/builder_test.rb
|
47
|
+
- test/helper_test.rb
|
48
|
+
- test/support/misc_helpers.rb
|
49
|
+
- test/test_helper.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/YouthTree/bhm-google-maps
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.3.6
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Helpers for Google Maps v3 in Rails - Using html 5, the google maps api v3 and the static maps api
|
80
|
+
test_files:
|
81
|
+
- test/builder_test.rb
|
82
|
+
- test/helper_test.rb
|
83
|
+
- test/support/misc_helpers.rb
|
84
|
+
- test/test_helper.rb
|