googlestaticmap 1.2.5 → 1.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +7 -0
- data/README.md +14 -8
- data/lib/googlestaticmap.rb +21 -14
- data/test/tc_google_static_map.rb +22 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dc261d1cb824be2c2acf1cc600b8d03c9332882
|
4
|
+
data.tar.gz: ec31683c4c3e972035b13f60d92746e4b29116ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 779b4a497a77ab34c9e528495b37809639fc703210f6370715aff3ef606f5492a9b6fe168ef5e9df3d800b43f8d68316b403478d92a33ffef5e734c2d0ba9526
|
7
|
+
data.tar.gz: 62590db99bc0e285cf2bf6efb1cbc67cc97c72cbb9726a195a1fd4b05909dd845f2f939a7dce7d9ac49f67039729e25bbddf8f331b93b7626127f628887a6eba
|
data/History.txt
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
== 1.2.6 / 2018-12-08
|
2
|
+
* Added the visible property to set map locations that will be visible (thanks luca!)
|
3
|
+
* Changed default protocol to https
|
4
|
+
* Updated example script to ask for an API key
|
5
|
+
* Forced specification of an API key or client ID/private key, since Google now requires this
|
6
|
+
* Updated documentation to specify that an API key or client ID/private key is required
|
7
|
+
|
1
8
|
== 1.2.5 / 2018-05-07
|
2
9
|
* Added styles, plain_string options to GoogleStaticMap (thanks anplug!)
|
3
10
|
* Added anchor option to MapMarker (thanks anplug!)
|
data/README.md
CHANGED
@@ -11,20 +11,26 @@ URL to download the map from. You can also call get_map to actually
|
|
11
11
|
download the map from Google, return the bytes, and optionally write the
|
12
12
|
image to a file.
|
13
13
|
|
14
|
+
You must have an API key from Google for personal use, or if you have a business
|
15
|
+
account, a client ID and private key from Google. If you do not have a key, you
|
16
|
+
can obtain one from https://developers.google.com/maps/documentation/javascript/get-api-key
|
17
|
+
|
14
18
|
## Examples:
|
15
19
|
|
16
20
|
Get a simple static map centered at Washington, DC, with the default size
|
17
21
|
(500 x 350), zoomed to level 11. image will be the binary data of the map
|
18
22
|
|
19
23
|
require 'googlestaticmap'
|
20
|
-
map = GoogleStaticMap.new(:
|
24
|
+
map = GoogleStaticMap.new(:api_key => "your API key", :zoom => 11,
|
25
|
+
:center => MapLocation.new(:address => "Washington, DC"))
|
21
26
|
image = map.get_map
|
22
27
|
|
23
28
|
Get the URL of the image described in the previous example, so you can insert
|
24
29
|
this URL as the src of an img element on an HTML page
|
25
30
|
|
26
31
|
require 'googlestaticmap'
|
27
|
-
map = GoogleStaticMap.new(:
|
32
|
+
map = GoogleStaticMap.new(:api_key => "your API key", :zoom => 11,
|
33
|
+
:center => MapLocation.new(:address => "Washington, DC"))
|
28
34
|
image_url = map.url(:auto)
|
29
35
|
|
30
36
|
When using the url function you can force the final url to use http or https:
|
@@ -37,6 +43,7 @@ size. image will be the binary data of the map
|
|
37
43
|
|
38
44
|
require 'googlestaticmap'
|
39
45
|
map = GoogleStaticMap.new
|
46
|
+
map.api_key = "your API key"
|
40
47
|
map.markers << MapMarker.new(:color => "blue", :location => MapLocation.new(:address => "1600 Pennsylvania Ave., Washington, DC"))
|
41
48
|
map.markers << MapMarker.new(:color => "blue", :location => MapLocation.new(:address => "1 1st Street Northeast, Washington, DC"))
|
42
49
|
image = map.get_map
|
@@ -47,7 +54,8 @@ outline solid, centered at the middle of the box, written out to the file
|
|
47
54
|
map.gif:
|
48
55
|
|
49
56
|
require 'googlestaticmap'
|
50
|
-
map = GoogleStaticMap.new(:
|
57
|
+
map = GoogleStaticMap.new(:api_key => "your API key", :maptype => "satellite",
|
58
|
+
:format => "gif", :width => 640, :height => 480)
|
51
59
|
poly = MapPolygon.new(:color => "0x00FF00FF", :fillcolor => "0x00FF0060")
|
52
60
|
poly.points << MapLocation.new(:latitude => 38.8, :longitude => -77.5)
|
53
61
|
poly.points << MapLocation.new(:latitude => 38.8, :longitude => -76.9)
|
@@ -61,10 +69,6 @@ If you're working behind a proxy, create the map object this way:
|
|
61
69
|
|
62
70
|
map = GoogleStaticMap.new(:proxy_address=>'my.proxy.host', :proxy_port=>8080, :width => 640, :height => 480)
|
63
71
|
|
64
|
-
If you have a public API key for tracking usage (https://developers.google.com/maps/documentation/staticmaps/#api_key):
|
65
|
-
|
66
|
-
map = GoogleStaticMap.new(:api_key => "my_api_key")
|
67
|
-
|
68
72
|
If you are a Maps For Businesses customer with a client ID and private key (https://developers.google.com/maps/documentation/business/webservices/#client_id)
|
69
73
|
(note that you cannot set an api_key if you want to use client_id and private_key):
|
70
74
|
|
@@ -74,11 +78,13 @@ You can also specify a channel for better tracking if you are a Maps For Busines
|
|
74
78
|
|
75
79
|
map.channel = "Tracking channel"
|
76
80
|
|
81
|
+
There are many other properties on the map that you can set. See https://github.com/brentsowers1/googlestaticmap/blob/master/lib/googlestaticmap.rb for all parameters.
|
82
|
+
|
77
83
|
|
78
84
|
|
79
85
|
## Compatibility
|
80
86
|
|
81
|
-
This has been tested and is working with Ruby 1.9.3, 2.0.0, 2.1.1, 2.1.10, 2.2.6, 2.3.
|
87
|
+
This has been tested and is working with Ruby 1.9.3, 2.0.0, 2.1.1, 2.1.10, 2.2.6, 2.3.7, 2.4.4, 2.5.1, and the latest JRuby.
|
82
88
|
|
83
89
|
## Author
|
84
90
|
|
data/lib/googlestaticmap.rb
CHANGED
@@ -22,6 +22,9 @@ class GoogleStaticMap
|
|
22
22
|
# An optional array of MapMarker instances
|
23
23
|
attr_accessor :markers
|
24
24
|
|
25
|
+
# An optional array of MapLocation instances that should remain visible on the map, though no markers or other indicators will be displayed
|
26
|
+
attr_accessor :visible
|
27
|
+
|
25
28
|
# An optional array of MapPath instances and/or MapPolygon instances to draw
|
26
29
|
attr_accessor :paths
|
27
30
|
|
@@ -99,7 +102,7 @@ class GoogleStaticMap
|
|
99
102
|
|
100
103
|
# Takes an optional hash of attributes
|
101
104
|
def initialize(attrs={})
|
102
|
-
defaults = {:width => 500, :height => 350, :markers => [],
|
105
|
+
defaults = {:width => 500, :height => 350, :markers => [], :visible => [],
|
103
106
|
:sensor => false, :maptype => "roadmap", :paths => [],
|
104
107
|
:proxy_port => nil, :proxy_address => nil, :api_key => nil,
|
105
108
|
:client_id => nil, :private_key => nil, :language => nil}
|
@@ -110,34 +113,38 @@ class GoogleStaticMap
|
|
110
113
|
|
111
114
|
# Returns the full URL to retrieve this static map. You can use this as the
|
112
115
|
# src for an img to display an image directly on a web page
|
113
|
-
# Example - "
|
116
|
+
# Example - "https://maps.googleapis.com/maps/api/staticmap?params..."
|
114
117
|
# +protocol+ can be 'http', 'https' or :auto. Specifying :auto will not return
|
115
118
|
# a protocol in the URL ("//maps.googleapis.com/..."), allowing the browser to
|
116
119
|
# select the appropriate protocol (if the page is loaded with https, it will
|
117
|
-
# use https). Defaults to
|
118
|
-
def url(protocol='
|
119
|
-
unless @center || @markers.length > 0 || @paths.length > 0
|
120
|
-
raise Exception.new("Need to specify either a center, markers, or a path")
|
120
|
+
# use https). Defaults to https
|
121
|
+
def url(protocol='https')
|
122
|
+
unless @center || @markers.length > 0 || @paths.length > 0 || @visible.length > 0
|
123
|
+
raise Exception.new("Need to specify either a center, markers, visible points, or a path")
|
121
124
|
end
|
122
125
|
if !@api_key.nil? && !@client_id.nil?
|
123
|
-
|
126
|
+
raise Exception.new("You cannot specify both an API key and a client ID, only specify one")
|
127
|
+
end
|
128
|
+
if @api_key.nil? && @client_id.nil?
|
129
|
+
raise Exception.new("You must specify either an API key, or a client ID/private key. Google requires this and the calls to Google will fail without one of these.")
|
124
130
|
end
|
125
131
|
if !@client_id.nil? && @private_key.nil?
|
126
132
|
raise Exception.new("private_key must be specified if using a client ID")
|
127
133
|
end
|
128
|
-
protocol = '
|
129
|
-
|
134
|
+
protocol = 'https' unless protocol == 'http' || protocol == 'https' ||
|
135
|
+
protocol == :auto
|
130
136
|
protocol = protocol == :auto ? '' : protocol + ":"
|
131
137
|
base = "#{protocol}//maps.googleapis.com"
|
132
138
|
path = "/maps/api/staticmap?"
|
133
139
|
attrs = GoogleStaticMapHelpers.safe_instance_variables(self,
|
134
|
-
["markers", "paths", "width", "height", "center",
|
140
|
+
["markers", "visible", "paths", "width", "height", "center",
|
135
141
|
"proxy_address", "proxy_port", "api_key", "client_id",
|
136
142
|
"private_key", "styles", "plain_string"],
|
137
143
|
:cgi_escape_values => true).to_a
|
138
144
|
attrs << ["size", "#{@width}x#{@height}"] if @width && @height
|
139
145
|
@markers.each {|m| attrs << ["markers",m.to_s] }
|
140
146
|
@paths.each {|p| attrs << ["path",p.to_s] }
|
147
|
+
attrs << ["visible", @visible.join(MAP_SEPARATOR)] if !@visible.nil? && @visible.any?
|
141
148
|
attrs << ["center", @center.to_s] if !@center.nil?
|
142
149
|
attrs << ["key", @api_key] if !@api_key.nil?
|
143
150
|
attrs << ["client", @client_id] if !@client_id.nil?
|
@@ -151,9 +158,9 @@ class GoogleStaticMap
|
|
151
158
|
base + path
|
152
159
|
end
|
153
160
|
|
154
|
-
# Returns the URL to retrieve the map, relative to
|
161
|
+
# Returns the URL to retrieve the map, relative to https://maps.googleapis.com
|
155
162
|
# Example - "/maps/api/staticmap?params..."
|
156
|
-
def relative_url(protocol='
|
163
|
+
def relative_url(protocol='https')
|
157
164
|
url(protocol).gsub(/[^\/]*\/\/maps\.googleapis\.com/, "")
|
158
165
|
end
|
159
166
|
|
@@ -166,8 +173,8 @@ class GoogleStaticMap
|
|
166
173
|
# +protocol+ - specify http or https here for the protocol to retrieve the
|
167
174
|
# map with. Defaults to http
|
168
175
|
# return value - the binary data for the map
|
169
|
-
def get_map(output_file=nil, protocol='
|
170
|
-
protocol = '
|
176
|
+
def get_map(output_file=nil, protocol='https')
|
177
|
+
protocol = 'https' unless protocol == 'http' || protocol == 'https'
|
171
178
|
port = protocol == 'https' ? 443 : 80
|
172
179
|
http = Net::HTTP.Proxy(@proxy_address,@proxy_port).new("maps.googleapis.com", port)
|
173
180
|
http.use_ssl = protocol == 'https'
|
@@ -56,23 +56,26 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
|
|
56
56
|
assert_equal 600, g.width
|
57
57
|
assert_equal "hybrid", g.maptype
|
58
58
|
assert_equal 1, g.markers.length
|
59
|
+
assert_equal 0, g.visible.length
|
59
60
|
end
|
60
61
|
|
61
62
|
def test_url
|
62
63
|
g = default_map
|
64
|
+
g.visible = [MapLocation.new(:latitude => 43.71, :longitude => 10.41), MapLocation.new(:latitude => 44.16, :longitude => 11.62)]
|
63
65
|
u = nil
|
64
66
|
assert_nothing_raised { u = g.url }
|
65
|
-
assert_equal
|
67
|
+
assert_equal 9, u.split("&").length, u
|
66
68
|
assert u.include?("size=600x400"), "width and height did not get converted in to a size"
|
67
69
|
assert u.include?("maptype=hybrid")
|
68
70
|
assert u.include?("scale=2")
|
69
71
|
assert u.include?("asdf")
|
70
|
-
assert u.include?("
|
72
|
+
assert u.include?("https://maps.googleapis.com")
|
71
73
|
assert u.include?("color:0x00FF00FF")
|
72
74
|
assert u.include?("fillcolor:0x00FF0060")
|
73
75
|
assert u.include?("38.8,-77.5#{MAP_SEPARATOR}38.8,-76.9#{MAP_SEPARATOR}39.2,-76.9#{MAP_SEPARATOR}39.2,-77.5#{MAP_SEPARATOR}38.8,-77.5"), "Polygon not in URL - #{u}"
|
76
|
+
assert u.include?("43.71,10.41#{MAP_SEPARATOR}44.16,11.62"), "Visible locations not in URL - #{u}"
|
74
77
|
assert u.include?("Washington%2C+DC")
|
75
|
-
assert
|
78
|
+
assert u.include?("key=asdf")
|
76
79
|
assert !u.include?("client"), "Client included when it shouldn't be"
|
77
80
|
|
78
81
|
f = nil
|
@@ -90,7 +93,7 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
|
|
90
93
|
g = default_map
|
91
94
|
u = nil
|
92
95
|
assert_nothing_raised { u = g.url(:auto) }
|
93
|
-
assert_equal
|
96
|
+
assert_equal 8, u.split("&").length, u
|
94
97
|
assert u =~ /^\/\/maps.googleapis.com/, u
|
95
98
|
f = nil
|
96
99
|
assert_nothing_raised {f = g.relative_url}
|
@@ -105,12 +108,11 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
|
|
105
108
|
assert g.url.include?("language=jp")
|
106
109
|
end
|
107
110
|
|
108
|
-
|
109
111
|
def test_url_https
|
110
112
|
g = default_map
|
111
113
|
u = nil
|
112
114
|
assert_nothing_raised { u = g.url('https') }
|
113
|
-
assert_equal
|
115
|
+
assert_equal 8, u.split("&").length, u
|
114
116
|
assert u =~ /^https:\/\/maps.googleapis.com/
|
115
117
|
f = nil
|
116
118
|
assert_nothing_raised {f = g.relative_url}
|
@@ -128,7 +130,7 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
|
|
128
130
|
u = nil
|
129
131
|
|
130
132
|
assert_nothing_raised { u = g.url }
|
131
|
-
assert_equal
|
133
|
+
assert_equal 12, u.split("&").length, u
|
132
134
|
assert !u.include?("styles"), "styles have to be convered to array of 'style'"
|
133
135
|
assert u.include?('style=feature:road.local%7Celement:geometry%7Ccolor:0x00ff00'), u
|
134
136
|
assert u.include?('style=feature:landscape%7Celement:geometry.fill%7Ccolor:0x000000'), u
|
@@ -143,7 +145,7 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
|
|
143
145
|
u = nil
|
144
146
|
|
145
147
|
assert_nothing_raised { u = g.url }
|
146
|
-
assert_equal
|
148
|
+
assert_equal 10, u.split("&").length, u
|
147
149
|
assert !u.include?("plain_string")
|
148
150
|
assert u.include?('&style=feature:road.local%7Celement:geometry%7Ccolor:0x00ff00&style=feature:landscape%7Celement:geometry.fill%7Ccolor:0x000000')
|
149
151
|
|
@@ -175,6 +177,7 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
|
|
175
177
|
|
176
178
|
def test_url_for_business
|
177
179
|
g = default_map
|
180
|
+
g.api_key = nil
|
178
181
|
g.client_id = "asdfclientid"
|
179
182
|
g.private_key = "vNIXE0xscrmjlyV-12Nj_BvUPaw="
|
180
183
|
u = nil
|
@@ -185,8 +188,16 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
|
|
185
188
|
assert !u.include?("key="), u
|
186
189
|
end
|
187
190
|
|
191
|
+
def test_no_api_key_or_client_id
|
192
|
+
g = default_map
|
193
|
+
g.api_key = nil
|
194
|
+
u = nil
|
195
|
+
assert_raise { u = g.url }
|
196
|
+
end
|
197
|
+
|
188
198
|
def test_url_for_business_no_private_key
|
189
199
|
g = default_map
|
200
|
+
g.api_key = nil
|
190
201
|
g.client_id = "asdfclientid"
|
191
202
|
u = nil
|
192
203
|
assert_raise { u = g.url }
|
@@ -204,7 +215,7 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
|
|
204
215
|
def test_get_map_success_no_file_http
|
205
216
|
test_data = "asdf"
|
206
217
|
MockHttp.any_instance.expects(:get2).returns(MockSuccess.new(test_data))
|
207
|
-
MockHttp.any_instance.expects(:"use_ssl=").with(
|
218
|
+
MockHttp.any_instance.expects(:"use_ssl=").with(true).returns(true)
|
208
219
|
Net::HTTP.expects(:new).returns(MockHttp.new)
|
209
220
|
|
210
221
|
g = default_map
|
@@ -294,6 +305,7 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
|
|
294
305
|
:center => MapLocation.new(:address => "Washington, DC"),
|
295
306
|
:paths => [poly],
|
296
307
|
:scale => 2,
|
297
|
-
:maptype => "hybrid"
|
308
|
+
:maptype => "hybrid",
|
309
|
+
:api_key => "asdf")
|
298
310
|
end
|
299
311
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: googlestaticmap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brent Sowers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|