google_static_maps_helper 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -0
- data/VERSION +1 -1
- data/changelog.txt +5 -0
- data/google_static_maps_helper.gemspec +12 -5
- data/lib/google_static_maps_helper.rb +43 -1
- data/lib/google_static_maps_helper/location.rb +21 -9
- data/lib/google_static_maps_helper/map.rb +82 -13
- data/lib/google_static_maps_helper/marker.rb +42 -35
- data/lib/google_static_maps_helper/path.rb +58 -5
- data/spec/google_static_maps_helper_spec.rb +11 -0
- data/spec/path_spec.rb +17 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -30,6 +30,15 @@ If you are on Rails you can put it in your environment configuration file and wh
|
|
30
30
|
...
|
31
31
|
end
|
32
32
|
|
33
|
+
It is also possible to create paths:
|
34
|
+
point1 = {:lng => 1, :lat => 2}
|
35
|
+
point2 = {:lng => 3, :lat => 4}
|
36
|
+
|
37
|
+
GoogleStaticMapsHelper.url_for do
|
38
|
+
path point1, point2, :color => :red, :weight => 7
|
39
|
+
end
|
40
|
+
|
41
|
+
..and of course you can add paths and markers in the same map.
|
33
42
|
|
34
43
|
= A bit more "low-level" approach instantiating objects yourself
|
35
44
|
|
@@ -100,6 +109,10 @@ color is what will trigger the creation of polygons for the static map API.
|
|
100
109
|
map << path
|
101
110
|
map.url
|
102
111
|
|
112
|
+
If you feel like it, you can add points at construction time:
|
113
|
+
GoogleStaticMapsHelper::Path.new(:color => :red, :points => [point1, point2])
|
114
|
+
# ..or
|
115
|
+
GoogleStaticMapsHelper::Path.new(point1, point2, :color => :red)
|
103
116
|
|
104
117
|
== TODO
|
105
118
|
* Ruby 1.9 support
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.3.
|
1
|
+
1.3.1
|
data/changelog.txt
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
= v.1.3.1
|
2
|
+
* Wrote better docs for all classes.
|
3
|
+
* You can add points to Paths with Path.new(point, second_point, :color => :red, :fillcolor => :blue)
|
4
|
+
* The "DSL" supports adding paths to the map.
|
5
|
+
|
1
6
|
= v.1.3.0
|
2
7
|
* It is now possible to create Paths which is used to represent lines and polygons in the map. This is not included in the little DSL we have though.
|
3
8
|
* The map may now request a URL for a specified image format. As an exampe, set map.format = :jpg to get smaller images (but also less quality).
|
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{google_static_maps_helper}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Thorbj\303\270rn Hermansen"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-28}
|
13
13
|
s.description = %q{This gem provides a simple interface to the Google Static Maps V2 API.}
|
14
14
|
s.email = %q{thhermansen@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,11 +26,15 @@ Gem::Specification.new do |s|
|
|
26
26
|
"changelog.txt",
|
27
27
|
"google_static_maps_helper.gemspec",
|
28
28
|
"lib/google_static_maps_helper.rb",
|
29
|
+
"lib/google_static_maps_helper/location.rb",
|
29
30
|
"lib/google_static_maps_helper/map.rb",
|
30
31
|
"lib/google_static_maps_helper/marker.rb",
|
32
|
+
"lib/google_static_maps_helper/path.rb",
|
31
33
|
"spec/google_static_maps_helper_spec.rb",
|
34
|
+
"spec/location_spec.rb",
|
32
35
|
"spec/map_spec.rb",
|
33
36
|
"spec/marker_spec.rb",
|
37
|
+
"spec/path_spec.rb",
|
34
38
|
"spec/spec_helper.rb"
|
35
39
|
]
|
36
40
|
s.homepage = %q{http://github.com/thhermansen/google_static_maps_helper}
|
@@ -42,7 +46,9 @@ Gem::Specification.new do |s|
|
|
42
46
|
"spec/spec_helper.rb",
|
43
47
|
"spec/marker_spec.rb",
|
44
48
|
"spec/google_static_maps_helper_spec.rb",
|
45
|
-
"spec/
|
49
|
+
"spec/location_spec.rb",
|
50
|
+
"spec/map_spec.rb",
|
51
|
+
"spec/path_spec.rb"
|
46
52
|
]
|
47
53
|
|
48
54
|
if s.respond_to? :specification_version then
|
@@ -58,3 +64,4 @@ Gem::Specification.new do |s|
|
|
58
64
|
s.add_dependency(%q<rspec>, [">= 0"])
|
59
65
|
end
|
60
66
|
end
|
67
|
+
|
@@ -4,7 +4,21 @@ require File.dirname(__FILE__) + '/google_static_maps_helper/location'
|
|
4
4
|
require File.dirname(__FILE__) + '/google_static_maps_helper/marker'
|
5
5
|
require File.dirname(__FILE__) + '/google_static_maps_helper/path'
|
6
6
|
|
7
|
+
#
|
8
|
+
# The Google Static Map Helper provides a simple interface to the
|
9
|
+
# Google Static Maps V2 API (http://code.google.com/apis/maps/documentation/staticmaps/).
|
10
|
+
#
|
11
|
+
# The module is build up of classes maping more or less directly to the entities you'd except:
|
12
|
+
# <tt>Map</tt>:: A map is what keeps all of the state of which you'll build a URL for.
|
13
|
+
# <tt>Marker</tt>:: One or more markers can be added to the map. A marker can be customized with size, label and color.
|
14
|
+
# <tt>Path</tt>:: A path will create lines or polygons in your map.
|
15
|
+
#
|
16
|
+
# == About
|
17
|
+
#
|
18
|
+
# Author:: Thorbjørn Hermansen (thhermansen@gmail.com)
|
19
|
+
#
|
7
20
|
module GoogleStaticMapsHelper
|
21
|
+
# The basic url to the API which we'll build the URL from
|
8
22
|
API_URL = 'http://maps.google.com/maps/api/staticmap'
|
9
23
|
|
10
24
|
class OptionMissing < ArgumentError; end # Raised when required options is not sent in during construction
|
@@ -15,7 +29,35 @@ module GoogleStaticMapsHelper
|
|
15
29
|
|
16
30
|
class << self
|
17
31
|
attr_accessor :key, :size, :sensor
|
18
|
-
|
32
|
+
|
33
|
+
#
|
34
|
+
# Provides a simple DSL stripping away the need of manually instantiating classes
|
35
|
+
#
|
36
|
+
# *NOTE* Paths are not supported by the DSL yet.
|
37
|
+
#
|
38
|
+
# Usage:
|
39
|
+
#
|
40
|
+
# # First of all, you might want to set your key etc
|
41
|
+
# GoogleStaticMapsHelper.key = 'your google key'
|
42
|
+
# GoogleStaticMapsHelper.size = '300x600'
|
43
|
+
# GoogleStaticMapsHelper.sensor = false
|
44
|
+
#
|
45
|
+
# # Then, you'll be able to do:
|
46
|
+
# url = GoogleStaticMapsHelper.url_for do
|
47
|
+
# marker :lng => 1, :lat => 2
|
48
|
+
# marker :lng => 3, :lat => 4
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# # You can send in key, size etc to url_for
|
52
|
+
# url = GoogleStaticMapsHelper.url_for(:key => 'your_key', :size => [300, 600]) do
|
53
|
+
# # ...
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# # If you need to, the map object is yielded to the block, so you can do:
|
57
|
+
# url = GoogleStaticMapsHelper.url_for do |map|
|
58
|
+
# map.marker object_which_responds_to_lng_lat
|
59
|
+
# end
|
60
|
+
#
|
19
61
|
def url_for(map_options = {}, &block)
|
20
62
|
map = Map.new(map_options)
|
21
63
|
block.arity < 1 ? map.instance_eval(&block) : block.call(map)
|
@@ -1,17 +1,26 @@
|
|
1
1
|
module GoogleStaticMapsHelper
|
2
|
-
|
3
|
-
# Represents a location (latitude and longitude)
|
4
2
|
#
|
5
|
-
#
|
6
|
-
#
|
3
|
+
# Represents a location with lat and lng values.
|
4
|
+
#
|
5
|
+
# This classed is used internally to back up Markers' location
|
6
|
+
# and Paths' points.
|
7
|
+
#
|
7
8
|
class Location
|
8
|
-
class NoLngMethod < NoMethodError; end
|
9
|
-
class NoLatMethod < NoMethodError; end
|
10
|
-
class NoLatKey < ArgumentError; end
|
11
|
-
class NoLngKey < ArgumentError; end
|
9
|
+
class NoLngMethod < NoMethodError; end # Raised if incomming object doesnt respond to lng
|
10
|
+
class NoLatMethod < NoMethodError; end # Raised if incomming object doesnt respond to lat
|
11
|
+
class NoLatKey < ArgumentError; end # Raised if incomming Hash doesnt have key lat
|
12
|
+
class NoLngKey < ArgumentError; end # Raised if incomming Hash doesnt have key lng
|
12
13
|
|
13
14
|
attr_accessor :lat, :lng
|
14
15
|
|
16
|
+
# :call-seq:
|
17
|
+
# new(location_object_or_options, *args)
|
18
|
+
#
|
19
|
+
# Creates a new Location which is used by Marker and Path object
|
20
|
+
# to represent it's locations.
|
21
|
+
#
|
22
|
+
# <tt>:args</tt>: Either a location which responds to lat or lng, or a Hash which has :lat and :lng keys.
|
23
|
+
#
|
15
24
|
def initialize(*args)
|
16
25
|
raise ArgumentError, "Must have some arguments." if args.length == 0
|
17
26
|
|
@@ -22,7 +31,10 @@ module GoogleStaticMapsHelper
|
|
22
31
|
end
|
23
32
|
end
|
24
33
|
|
25
|
-
|
34
|
+
#
|
35
|
+
# Returning the location as a string "lat,lng"
|
36
|
+
#
|
37
|
+
def to_url # :nodoc:
|
26
38
|
[lat, lng].join(',')
|
27
39
|
end
|
28
40
|
|
@@ -1,10 +1,12 @@
|
|
1
1
|
module GoogleStaticMapsHelper
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
2
|
+
#
|
3
|
+
# The Map keeps track of the state of which we want to build a URL for.
|
4
|
+
# It will hold Markers and Paths, and other states like dimensions of the map,
|
5
|
+
# image format, language etc.
|
6
|
+
#
|
5
7
|
class Map
|
6
8
|
include Enumerable
|
7
|
-
|
9
|
+
|
8
10
|
MAX_WIDTH = 640
|
9
11
|
MAX_HEIGHT = 640
|
10
12
|
|
@@ -12,15 +14,27 @@ module GoogleStaticMapsHelper
|
|
12
14
|
VALID_MAP_TYPES = %w{roadmap satellite terrain hybrid}
|
13
15
|
|
14
16
|
REQUIRED_OPTIONS = [:key, :size, :sensor]
|
15
|
-
OPTIONAL_OPTIONS = [:center, :zoom, :format, :maptype, :mobile, :language
|
17
|
+
OPTIONAL_OPTIONS = [:center, :zoom, :format, :maptype, :mobile, :language]
|
16
18
|
|
17
19
|
attr_accessor *(REQUIRED_OPTIONS + OPTIONAL_OPTIONS)
|
18
20
|
attr_accessor :width, :height
|
19
21
|
|
20
|
-
# Initialize a new Map object
|
21
22
|
#
|
22
|
-
#
|
23
|
-
#
|
23
|
+
# Creates a new Map object
|
24
|
+
#
|
25
|
+
# <tt>:options</tt>:: The options available are the same as described in
|
26
|
+
# Google's API documentation[http://code.google.com/apis/maps/documentation/staticmaps/#Usage].
|
27
|
+
# In short, valid options are:
|
28
|
+
# <tt>:key</tt>:: Your Google maps API key
|
29
|
+
# <tt>:size</tt>:: The size of the map. Can be a "wxh", [w,h] or {:width => x, :height => y}
|
30
|
+
# <tt>:sensor</tt>:: Set to true if your application is using a sensor. See the API doc.
|
31
|
+
# <tt>:center</tt>:: The center point of your map. Optional if you add markers or path to the map
|
32
|
+
# <tt>:zoom</tt>:: The zoom level you want, also optional as center
|
33
|
+
# <tt>:format</tt>:: Defaults to png
|
34
|
+
# <tt>:maptype</tt>:: Defaults to roadmap
|
35
|
+
# <tt>:mobile</tt>:: Returns map tiles better suited for mobile devices with small screens.
|
36
|
+
# <tt>:language</tt>:: The language used in the map
|
37
|
+
#
|
24
38
|
def initialize(options)
|
25
39
|
inject_defaults_from_module_class_attribute!(options)
|
26
40
|
validate_required_options(options)
|
@@ -30,6 +44,9 @@ module GoogleStaticMapsHelper
|
|
30
44
|
@map_enteties = []
|
31
45
|
end
|
32
46
|
|
47
|
+
#
|
48
|
+
# Builds up a URL representing the state of this Map object
|
49
|
+
#
|
33
50
|
def url
|
34
51
|
raise BuildDataMissing, "We have to have markers, paths or center and zoom set when url is called!" unless can_build?
|
35
52
|
|
@@ -56,10 +73,19 @@ module GoogleStaticMapsHelper
|
|
56
73
|
out
|
57
74
|
end
|
58
75
|
|
76
|
+
#
|
77
|
+
# Returns all the markers which this map holds
|
78
|
+
#
|
59
79
|
def markers
|
60
80
|
@map_enteties.select {|e| e.is_a? Marker}
|
61
81
|
end
|
62
82
|
|
83
|
+
#
|
84
|
+
# Returns the markers grouped by it's label, color and size.
|
85
|
+
#
|
86
|
+
# This is handy when building the URL because the API wants us to
|
87
|
+
# group together equal markers and just list the position of the markers thereafter in the URL.
|
88
|
+
#
|
63
89
|
def grouped_markers
|
64
90
|
markers.inject(Hash.new {|hash, key| hash[key] = []}) do |groups, marker|
|
65
91
|
groups[marker.options_to_url_params] << marker
|
@@ -67,10 +93,16 @@ module GoogleStaticMapsHelper
|
|
67
93
|
end
|
68
94
|
end
|
69
95
|
|
96
|
+
#
|
97
|
+
# Returns all the paths which this map holds
|
98
|
+
#
|
70
99
|
def paths
|
71
100
|
@map_enteties.select {|e| e.is_a? Path}
|
72
101
|
end
|
73
102
|
|
103
|
+
#
|
104
|
+
# Pushes either a Marker or a Path on to the map
|
105
|
+
#
|
74
106
|
def <<(entity)
|
75
107
|
@map_enteties << entity
|
76
108
|
@map_enteties.uniq!
|
@@ -89,11 +121,26 @@ module GoogleStaticMapsHelper
|
|
89
121
|
@map_enteties.length
|
90
122
|
end
|
91
123
|
|
92
|
-
|
93
|
-
|
94
|
-
|
124
|
+
#
|
125
|
+
# Used internally to make the DSL work. Might be changed at any time
|
126
|
+
# to make a better implementation.
|
127
|
+
#
|
128
|
+
def marker(*args) # :nodoc:
|
129
|
+
self << Marker.new(*args)
|
130
|
+
end
|
131
|
+
|
132
|
+
#
|
133
|
+
# Used internally to make the DSL work. Might be changed at any time
|
134
|
+
#
|
135
|
+
def path(*args) # :nodoc:
|
136
|
+
self << Path.new(*args)
|
95
137
|
end
|
96
138
|
|
139
|
+
#
|
140
|
+
# Sets the size of the map
|
141
|
+
#
|
142
|
+
# <tt>size</tt>:: Can be a "wxh", [w,h] or {:width => x, :height => y}
|
143
|
+
#
|
97
144
|
def size=(size)
|
98
145
|
unless size.nil?
|
99
146
|
case size
|
@@ -112,11 +159,19 @@ module GoogleStaticMapsHelper
|
|
112
159
|
self.height = height if height
|
113
160
|
end
|
114
161
|
end
|
115
|
-
|
162
|
+
|
163
|
+
#
|
164
|
+
# Returns size as a string, "wxh"
|
165
|
+
#
|
116
166
|
def size
|
117
167
|
[@width, @height].join('x')
|
118
168
|
end
|
119
|
-
|
169
|
+
|
170
|
+
#
|
171
|
+
# Defines width and height setter methods
|
172
|
+
#
|
173
|
+
# These methods enforces the MAX dimensions of the map
|
174
|
+
#
|
120
175
|
[:width, :height].each do |name|
|
121
176
|
define_method "#{name}=" do |dimension|
|
122
177
|
dimension = dimension.to_i
|
@@ -126,11 +181,22 @@ module GoogleStaticMapsHelper
|
|
126
181
|
end
|
127
182
|
end
|
128
183
|
|
184
|
+
|
185
|
+
#
|
186
|
+
# Sets the format of the map
|
187
|
+
#
|
188
|
+
# <tt>format</tt>:: Can be any values included in VALID_FORMATS.
|
189
|
+
#
|
129
190
|
def format=(format)
|
130
191
|
@format = format.to_s
|
131
192
|
raise UnsupportedFormat unless VALID_FORMATS.include? @format
|
132
193
|
end
|
133
194
|
|
195
|
+
#
|
196
|
+
# Sets the map type of the map
|
197
|
+
#
|
198
|
+
# <tt>type</tt>:: Can be any values included in VALID_MAP_TYPES.
|
199
|
+
#
|
134
200
|
def maptype=(type)
|
135
201
|
@maptype = type.to_s
|
136
202
|
raise UnsupportedMaptype unless VALID_MAP_TYPES.include? @maptype
|
@@ -138,6 +204,9 @@ module GoogleStaticMapsHelper
|
|
138
204
|
|
139
205
|
|
140
206
|
private
|
207
|
+
#
|
208
|
+
# Returns an answer for if we can build the URL or not
|
209
|
+
#
|
141
210
|
def can_build?
|
142
211
|
!@map_enteties.empty? || (center && zoom)
|
143
212
|
end
|
@@ -1,9 +1,8 @@
|
|
1
1
|
module GoogleStaticMapsHelper
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
2
|
+
#
|
3
|
+
# A marker object is representing a marker with a customizable label, color and size.
|
4
|
+
#
|
5
5
|
class Marker
|
6
|
-
# These options are the one we build our parameters from
|
7
6
|
DEFAULT_OPTIONS = {
|
8
7
|
:color => 'red',
|
9
8
|
:size => 'mid',
|
@@ -12,33 +11,41 @@ module GoogleStaticMapsHelper
|
|
12
11
|
|
13
12
|
attr_accessor :location, *DEFAULT_OPTIONS.keys
|
14
13
|
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
14
|
+
# :call-seq:
|
15
|
+
# new(location_object_or_options, *args)
|
16
|
+
#
|
17
|
+
# Creates a new Marker object. A marker object will, when added to a Map, represent
|
18
|
+
# one marker which you can customize with color, size and label.
|
19
|
+
#
|
20
|
+
# <tt>:location_object_or_options</tt>:: Either an object which responds to lat and lng or simply a option hash
|
21
|
+
# <tt>:args</tt>:: A hash of options. Can have keys like <tt>:color</tt>,
|
22
|
+
# <tt>:size</tt>, and <tt>:label</tt>.
|
23
|
+
# See Google's API documentation[http://code.google.com/apis/maps/documentation/staticmaps/#MarkerStyles] for more information.
|
24
|
+
# If a location object hasn't been given you must also include <tt>:lat</tt>
|
25
|
+
# and <tt>:lng</tt> values.
|
26
|
+
#
|
19
27
|
#
|
20
|
-
#
|
21
|
-
#
|
28
|
+
# Usage:
|
29
|
+
#
|
30
|
+
# # Sets location via object which responds to lng and lat
|
31
|
+
# GoogleStaticMapsHelper::Marker.new(location {:label => :a})
|
32
|
+
#
|
33
|
+
# # ..or include the lng and lat in the option hash
|
34
|
+
# GoogleStaticMapsHelper::Marker.new(:lng => 1, :lat => 2, :label => :a)
|
22
35
|
#
|
23
|
-
# You can also send in options like color, size and label in the hash,
|
24
|
-
# or as a secnond parameter if the first was an object.
|
25
36
|
def initialize(*args)
|
26
37
|
raise ArgumentError, "Must have one or two arguments." if args.length == 0
|
27
|
-
|
28
|
-
if args.first.is_a? Hash
|
29
|
-
extract_location_from_hash!(args.first)
|
30
|
-
else
|
31
|
-
extract_location_from_object(args.shift)
|
32
|
-
end
|
33
|
-
|
38
|
+
extract_location!(args)
|
34
39
|
options = DEFAULT_OPTIONS.merge(args.shift || {})
|
35
40
|
validate_options(options)
|
36
41
|
options.each_pair { |k, v| send("#{k}=", v) }
|
37
42
|
end
|
38
43
|
|
39
|
-
#
|
40
|
-
#
|
41
|
-
|
44
|
+
#
|
45
|
+
# Returns a string representing this marker
|
46
|
+
# Used by the Map when building url.
|
47
|
+
#
|
48
|
+
def options_to_url_params # :nodoc:
|
42
49
|
params = DEFAULT_OPTIONS.keys.map(&:to_s).sort.inject([]) do |params, attr|
|
43
50
|
value = send(attr)
|
44
51
|
params << "#{attr}:#{URI.escape(value)}" unless value.nil?
|
@@ -48,35 +55,35 @@ module GoogleStaticMapsHelper
|
|
48
55
|
params.join('|')
|
49
56
|
end
|
50
57
|
|
51
|
-
#
|
52
|
-
|
58
|
+
#
|
59
|
+
# Concatenation of lat and lng value. Used when building URLs and returns them in correct order
|
60
|
+
#
|
61
|
+
def location_to_url # :nodoc:
|
53
62
|
@location.to_url
|
54
63
|
end
|
55
64
|
|
56
|
-
def label
|
65
|
+
def label # :nodoc:
|
57
66
|
@label.to_s.upcase if @label
|
58
67
|
end
|
59
68
|
|
60
|
-
def color
|
69
|
+
def color # :nodoc:
|
61
70
|
@color.downcase if @color
|
62
71
|
end
|
63
72
|
|
64
73
|
|
74
|
+
#
|
75
|
+
# Proxies calls to the internal object which keeps track of this marker's location.
|
76
|
+
# So, if @location responds to method missing in this object, it will respond to it.
|
77
|
+
#
|
65
78
|
def method_missing(method, *args)
|
66
79
|
return @location.send(method, *args) if @location.respond_to? method
|
67
80
|
super
|
68
81
|
end
|
69
82
|
|
70
83
|
private
|
71
|
-
def
|
72
|
-
|
73
|
-
|
74
|
-
to_object[:lng] = location_hash.delete :lng if location_hash.has_key? :lng
|
75
|
-
@location = Location.new(to_object)
|
76
|
-
end
|
77
|
-
|
78
|
-
def extract_location_from_object(location)
|
79
|
-
@location = Location.new(location)
|
84
|
+
def extract_location!(args)
|
85
|
+
@location = Location.new(*args)
|
86
|
+
args.shift unless args.first.is_a? Hash
|
80
87
|
end
|
81
88
|
|
82
89
|
def validate_options(options)
|
@@ -1,4 +1,8 @@
|
|
1
1
|
module GoogleStaticMapsHelper
|
2
|
+
#
|
3
|
+
# A Path is used to draw things in the map, either lines or Polygons.
|
4
|
+
# It is build up of points and if a fill color is set you'll get a Polygon.
|
5
|
+
#
|
2
6
|
class Path
|
3
7
|
include Enumerable
|
4
8
|
|
@@ -6,13 +10,33 @@ module GoogleStaticMapsHelper
|
|
6
10
|
|
7
11
|
attr_accessor :points, *OPTIONAL_OPTIONS
|
8
12
|
|
9
|
-
|
13
|
+
#
|
14
|
+
# Creates a new Path which you can push points on to to make up lines or polygons
|
15
|
+
#
|
16
|
+
# The following options are available, for more information see the
|
17
|
+
# Google API documentation[http://code.google.com/apis/maps/documentation/staticmaps/#Paths].
|
18
|
+
#
|
19
|
+
# <tt>:weight</tt>:: The weight is the thickness of the line, defaults to 5
|
20
|
+
# <tt>:color</tt>:: The color of the border can either be a textual representation like red, green, blue, black etc
|
21
|
+
# or as a 24-bit (0xAABBCC) or 32-bit hex value (0xAABBCCDD). When 32-bit values are
|
22
|
+
# given the two last bits will represent the alpha transparency value.
|
23
|
+
# <tt>:fillcolor</tt>:: With the fill color set you'll get a polygon in the map. The color value can be the same
|
24
|
+
# as described in the <tt>:color</tt>. When used, the static map will automatically create
|
25
|
+
# a closed shape.
|
26
|
+
# <tt>:points</tt>:: An array of points. You can mix objects responding to lng and lat, and a Hash with lng and lat keys.
|
27
|
+
#
|
28
|
+
def initialize(*args)
|
10
29
|
@points = []
|
11
|
-
|
30
|
+
|
31
|
+
extract_options!(args)
|
32
|
+
add_points(args)
|
12
33
|
end
|
13
34
|
|
14
|
-
|
15
|
-
|
35
|
+
#
|
36
|
+
# Returns a string representation of this Path
|
37
|
+
# Used by the Map when building the URL
|
38
|
+
#
|
39
|
+
def url_params # :nodoc:
|
16
40
|
raise BuildDataMissing, "Need at least 2 points to create a path!" unless can_build?
|
17
41
|
out = 'path='
|
18
42
|
|
@@ -30,6 +54,11 @@ module GoogleStaticMapsHelper
|
|
30
54
|
end
|
31
55
|
|
32
56
|
|
57
|
+
#
|
58
|
+
# Sets the points of this Path.
|
59
|
+
#
|
60
|
+
# *WARNING* Using this method will clear out any points which might be set.
|
61
|
+
#
|
33
62
|
def points=(array)
|
34
63
|
raise ArgumentError unless array.is_a? Array
|
35
64
|
@points = []
|
@@ -48,20 +77,44 @@ module GoogleStaticMapsHelper
|
|
48
77
|
length == 0
|
49
78
|
end
|
50
79
|
|
80
|
+
#
|
81
|
+
# Pushes a new point into the Path
|
82
|
+
#
|
83
|
+
# A point might be a Hash which has lng and lat as keys, or an object responding to
|
84
|
+
# lng and lat. Any points pushed in will be converted internally to a Location
|
85
|
+
# object.
|
86
|
+
#
|
51
87
|
def <<(point)
|
52
88
|
@points << ensure_point_is_location_object(point)
|
53
89
|
@points.uniq!
|
54
90
|
self
|
55
91
|
end
|
56
92
|
|
93
|
+
|
57
94
|
private
|
95
|
+
#
|
96
|
+
# Extracts the lng and lat values from incomming point and creates a new Location
|
97
|
+
# object from it.
|
98
|
+
#
|
58
99
|
def ensure_point_is_location_object(point)
|
59
100
|
return point if point.instance_of? Location
|
60
101
|
Location.new(point)
|
61
102
|
end
|
62
|
-
|
103
|
+
|
104
|
+
#
|
105
|
+
# Do we have enough points to build a path?
|
106
|
+
#
|
63
107
|
def can_build?
|
64
108
|
length > 1
|
65
109
|
end
|
110
|
+
|
111
|
+
def extract_options!(args)
|
112
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
113
|
+
options.each_pair {|k, v| send("#{k}=", v)}
|
114
|
+
end
|
115
|
+
|
116
|
+
def add_points(points)
|
117
|
+
points.each {|point| self << point}
|
118
|
+
end
|
66
119
|
end
|
67
120
|
end
|
@@ -29,6 +29,17 @@ describe GoogleStaticMapsHelper do
|
|
29
29
|
out.should include('size=600x400')
|
30
30
|
end
|
31
31
|
|
32
|
+
it "should be able to add paths" do
|
33
|
+
point = {:lat => 1, :lng => 2}
|
34
|
+
point2 = {:lat => 3, :lng => 4}
|
35
|
+
|
36
|
+
out = GoogleStaticMapsHelper.url_for do
|
37
|
+
path point, point2, :color => :red
|
38
|
+
end
|
39
|
+
|
40
|
+
out.should include('path=color:red|1,2|3,4')
|
41
|
+
end
|
42
|
+
|
32
43
|
it "should be possible to use inside a class, using attributes of that class" do
|
33
44
|
class TestingClass
|
34
45
|
attr_reader :location
|
data/spec/path_spec.rb
CHANGED
@@ -19,6 +19,23 @@ describe GoogleStaticMapsHelper::Path do
|
|
19
19
|
path.send(attribute).should == @@options.fetch(attribute)
|
20
20
|
end
|
21
21
|
end
|
22
|
+
|
23
|
+
describe "points" do
|
24
|
+
before do
|
25
|
+
@point = GoogleStaticMapsHelper::Location.new(:lat => 1, :lng => 2)
|
26
|
+
@point2 = GoogleStaticMapsHelper::Location.new(:lat => 1, :lng => 2)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be able to add points via options during initialization" do
|
30
|
+
path = GoogleStaticMapsHelper::Path.new(@@options.merge(:points => [@point, @point2]))
|
31
|
+
path.points.should == [@point, @point2]
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be able to add points before option hash" do
|
35
|
+
path = GoogleStaticMapsHelper::Path.new(@point, @point2, @@options)
|
36
|
+
path.points.should == [@point, @point2]
|
37
|
+
end
|
38
|
+
end
|
22
39
|
end
|
23
40
|
|
24
41
|
describe "points" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_static_maps_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Thorbj\xC3\xB8rn Hermansen"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-31 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|