static_map 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +87 -0
- data/Rakefile +2 -0
- data/lib/static_map.rb +78 -0
- data/lib/static_map/version.rb +3 -0
- data/static_map.gemspec +17 -0
- data/test/static_map_test.rb +49 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Sean Behan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# StaticMap
|
2
|
+
|
3
|
+
Build static image maps using Google's Static Map API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem 'static_map'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install static_map
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
image = StaticMap::Image.new({
|
21
|
+
size: '500x500',
|
22
|
+
sensor: false,
|
23
|
+
markers: [],
|
24
|
+
maptype: 'road',
|
25
|
+
zoom: 8,
|
26
|
+
title: 'A map',
|
27
|
+
alt: 'Alt text for img html',
|
28
|
+
path: './my-map.png'
|
29
|
+
})
|
30
|
+
|
31
|
+
image.url
|
32
|
+
# => http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true
|
33
|
+
|
34
|
+
image.to_html
|
35
|
+
# => <img src='http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true' title='' alt=''/>
|
36
|
+
|
37
|
+
img.save # save map to disk with path option
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
The options are
|
42
|
+
|
43
|
+
* center String - center the map around this location
|
44
|
+
* zoom Integer - zoom level of the map
|
45
|
+
* size String - in pixels of the image. 500x500
|
46
|
+
* sensor Boolean - autodetect user location
|
47
|
+
* markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates
|
48
|
+
* maptype String - type of map (satellite, road, hybrid, terrain)
|
49
|
+
* path String - path to write file to when #save is called
|
50
|
+
* alt String - alt text if using image tag
|
51
|
+
* title String - title text if using image tag
|
52
|
+
|
53
|
+
### Usage with Rails
|
54
|
+
|
55
|
+
```erb
|
56
|
+
<%=raw StaticMap::Image.new %>
|
57
|
+
```
|
58
|
+
|
59
|
+
The markers option accepts either a location (E.g., "Burlington, Vermont") or latitude and longitude coordinates.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
<%=raw StaticMap::Image.new({
|
63
|
+
zoom: 13,
|
64
|
+
markers: [
|
65
|
+
{ location: "Winooski,Vermont", color: "green", label: "A" },
|
66
|
+
{ latitude: 44.477171, longitude: -73.222032, color: "blue", label: "B" }
|
67
|
+
]
|
68
|
+
}) %>
|
69
|
+
```
|
70
|
+
|
71
|
+
![StaticMap::Image of Burlington Vermont](http://maps.google.com/maps/api/staticmap?size=500x500&zoom=13&sensor=true&markers=color:green|label:A|Winooski,VT&markers=color:blue|label:B|44.477171,-73.222032)
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create new Pull Request
|
80
|
+
|
81
|
+
## Credits and Resources
|
82
|
+
|
83
|
+
The following links are great resources for working with Google Maps API
|
84
|
+
|
85
|
+
- https://developers.google.com/maps/
|
86
|
+
- http://www.lateralcode.com/new-google-maps-api/
|
87
|
+
- https://developers.google.com/maps/documentation/staticmaps/
|
data/Rakefile
ADDED
data/lib/static_map.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# http://www.lateralcode.com/new-google-maps-api/
|
2
|
+
require "static_map/version"
|
3
|
+
require 'cgi'
|
4
|
+
require 'open-uri'
|
5
|
+
|
6
|
+
module StaticMap
|
7
|
+
#
|
8
|
+
# Example
|
9
|
+
#
|
10
|
+
# img = StaticMap::Image.new(size: 500)
|
11
|
+
# img.center = 'Burlington, VT'
|
12
|
+
# img.zoom = 4
|
13
|
+
# img.markers << {latitude: 123, longitude: 321, color: 'red', label: 'VT'}
|
14
|
+
#
|
15
|
+
class Image
|
16
|
+
# Base URL for Google Static Maps API endpoint
|
17
|
+
URL = "http://maps.google.com/maps/api/staticmap"
|
18
|
+
|
19
|
+
# center String - center the map around this location
|
20
|
+
# zoom Integer - zoom level of the map
|
21
|
+
# size String - in pixels of the image. 500x500
|
22
|
+
# sensor Boolean - autodetect user user location
|
23
|
+
# markers Hash - location of pin on map, requires location address or lat/long
|
24
|
+
# maptype String - satelite, road... etc
|
25
|
+
# path String - where to save the file
|
26
|
+
# alt String - alt text if using image tag
|
27
|
+
# title String - title text if using image tag
|
28
|
+
attr_accessor :center, :zoom, :size, :sensor, :markers, :maptype, :path, :alt, :title
|
29
|
+
|
30
|
+
def initialize(options={})
|
31
|
+
@markers = options[:markers] || []
|
32
|
+
@size = options[:size] || '500x500'
|
33
|
+
@sensor = options[:sensor] || true
|
34
|
+
@zoom = options[:zoom] || 1
|
35
|
+
@center = options[:center] || nil
|
36
|
+
@maptype = options[:maptype] || 'road'
|
37
|
+
@path = options[:path] || nil
|
38
|
+
@alt = options[:alt] || nil
|
39
|
+
@title = options[:title] || nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def save
|
43
|
+
raise "Please set the path argument to save the image" unless @path
|
44
|
+
|
45
|
+
File.open(@path, "w") { |f| f.write(open(url).read) }
|
46
|
+
end
|
47
|
+
|
48
|
+
def url
|
49
|
+
"#{URL}?#{params}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def params
|
53
|
+
x = { size: size, center: center, zoom: zoom, sensor: sensor, maptype: maptype }.reject { |k,v| v.nil? }.map do |k,v|
|
54
|
+
"#{k}=#{CGI.escape(v.to_s)}"
|
55
|
+
end.join("&")
|
56
|
+
|
57
|
+
x += "&#{marker_params}" if @markers.any?
|
58
|
+
x
|
59
|
+
end
|
60
|
+
|
61
|
+
def marker_params
|
62
|
+
@markers.map do |marker|
|
63
|
+
str = ["markers="]
|
64
|
+
str << ["color:#{marker[:color]}"] if marker[:color]
|
65
|
+
str << ["label:#{marker[:label]}"] if marker[:label]
|
66
|
+
str << ["#{marker[:location]}"] if marker[:location]
|
67
|
+
str << ["#{marker[:latitude]},#{marker[:longitude]}"] if marker[:latitude] && marker[:longitude]
|
68
|
+
str.join("|")
|
69
|
+
end.join("&").gsub(/\=\|/i, '=')
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_html
|
73
|
+
"<img src='#{url}' title='#{title}' alt='#{alt}'/>"
|
74
|
+
end
|
75
|
+
alias_method :to_s, :to_html
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
data/static_map.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/static_map/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Sean Behan"]
|
6
|
+
gem.email = ["inbox@seanbehan.com"]
|
7
|
+
gem.description = %q{Google Static Map API with Ruby}
|
8
|
+
gem.summary = %q{Build static maps using Google's Static Map API}
|
9
|
+
gem.homepage = "http://github.com/gristmill/static_map"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "static_map"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = StaticMap::VERSION
|
17
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib/')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'static_map'
|
5
|
+
|
6
|
+
class StaticMapTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def test_defaults
|
9
|
+
img = StaticMap::Image.new
|
10
|
+
assert_equal "http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true", img.url
|
11
|
+
assert_equal %{<img src='http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true' title='' alt=''/>}, img.to_html
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_params
|
15
|
+
img = StaticMap::Image.new
|
16
|
+
assert_equal "size=500x500&zoom=1&sensor=true", img.params
|
17
|
+
img.size = '900x900'
|
18
|
+
assert_equal "size=900x900&zoom=1&sensor=true", img.params
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_marker_params
|
22
|
+
img = StaticMap::Image.new
|
23
|
+
img.markers << { latitude: 44.477462, longitude: -73.212032, color: "red", label: "A" }
|
24
|
+
img.markers << { location: "Winooski, Vermont", color: "red", label: "B" }
|
25
|
+
assert_equal "markers=color:red|label:A|44.477462,-73.212032&markers=color:red|label:B|Winooski, Vermont", img.marker_params
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_url
|
29
|
+
img = StaticMap::Image.new
|
30
|
+
img.center = nil
|
31
|
+
img.size = '300x300'
|
32
|
+
img.zoom = 11
|
33
|
+
img.sensor = false
|
34
|
+
img.markers << { latitude: 44.477462, longitude: -73.212032, color: "green", label: "A" }
|
35
|
+
assert_equal "http://maps.google.com/maps/api/staticmap?size=300x300&zoom=11&sensor=false&markers=color:green|label:A|44.477462,-73.212032", img.url
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_to_html
|
39
|
+
img = StaticMap::Image.new(:size => "500x500", :alt => "Alt Test", :title => "Title Test")
|
40
|
+
assert_equal "<img src='http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true' title='Title Test' alt='Alt Test'/>", img.to_html
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_save
|
44
|
+
# TODO fake the remote resource
|
45
|
+
img = StaticMap::Image.new(path: "./test/tmp.png").save
|
46
|
+
assert File.exists?("./test/tmp.png")
|
47
|
+
File.delete("./test/tmp.png")
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: static_map
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sean Behan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Google Static Map API with Ruby
|
15
|
+
email:
|
16
|
+
- inbox@seanbehan.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/static_map.rb
|
27
|
+
- lib/static_map/version.rb
|
28
|
+
- static_map.gemspec
|
29
|
+
- test/static_map_test.rb
|
30
|
+
homepage: http://github.com/gristmill/static_map
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.17
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Build static maps using Google's Static Map API
|
54
|
+
test_files:
|
55
|
+
- test/static_map_test.rb
|