Class: GoogleStaticMap
- Inherits:
-
Object
- Object
- GoogleStaticMap
- Defined in:
- lib/googlestaticmap.rb
Overview
Main class for creating a static map. Create an instance, Set attributes that describe properties of the map. Then call url to get a URL that you can use as the src of an img tag. You can also call get_map to actually download the map from Google, and optionally write it to a file.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
API Key - see developers.google.com/maps/documentation/static-maps/get-api-key#key for details Note that if this is set, you cannot provide a client ID.
-
#center ⇒ Object
MapLocation for the center of the map.
-
#channel ⇒ Object
Channel - identifier channel for tracking API source in enterprise tools see developers.google.com/maps/documentation/business/clientside/quota for details.
-
#client_id ⇒ Object
ClientId for business customers - see developers.google.com/maps/documentation/static-maps/get-api-key#key for details Note that if this is set, you cannot provide an API key.
-
#format ⇒ Object
format of the image: * png8 - 8 bit PNG (default) * png32 - 32 bit PNG * gif * jpg * jpg-baseline - non-progressive JPEG.
-
#height ⇒ Object
Height of resulting image in pixels, defaults to 350, maximum 640.
-
#language ⇒ Object
Language - :en, :ja see developers.google.com/maps/documentation/static-maps/intro for details.
-
#maptype ⇒ Object
Type of map to create: * roadmap (default) * satellite * terrain * hybrid - satellite imagery with roads.
-
#markers ⇒ Object
An optional array of MapMarker instances.
-
#paths ⇒ Object
An optional array of MapPath instances and/or MapPolygon instances to draw.
-
#plain_string ⇒ Object
In case when some parameter should be inserted manually For example, using mapstyle.withgoogle.com/ tool and inserting generated style as is.
-
#private_key ⇒ Object
The private key, also known as the URL signing secret, is used to to generate the signature parameter in the URL.
-
#proxy_address ⇒ Object
If you need to use a proxy server to reach Google, set the name/address of the proxy server here.
-
#proxy_port ⇒ Object
If proxy_address is set, set this to the port of the proxy server.
-
#scale ⇒ Object
1, 2, or 4 for Maps API Premier customers.
-
#sensor ⇒ Object
Applications that determine the user’s location via a sensor must set this to true, defaults to false.
-
#styles ⇒ Object
Styles - see developers.google.com/maps/documentation/maps-static/styling styles is should be represented as array of objects [‘featureArgument, element: ’elementArgument’, rule1: ‘rule1Arguement’, rule2: ‘rule2Arguement’, …, …].
-
#visible ⇒ Object
An optional array of MapLocation instances that should remain visible on the map, though no markers or other indicators will be displayed.
-
#width ⇒ Object
Width of resulting image in pixels, defaults to 500, maximum 640.
-
#zoom ⇒ Object
0 (the whole world) to 21 (individual buildings).
Instance Method Summary collapse
-
#get_map(output_file = nil, protocol = 'https') ⇒ Object
Connects to Google, retrieves the map, and returns the bytes for the image.
- #get_styles ⇒ Object
-
#initialize(attrs = {}) ⇒ GoogleStaticMap
constructor
Takes an optional hash of attributes.
-
#relative_url(protocol = 'https') ⇒ Object
Returns the URL to retrieve the map, relative to maps.googleapis.com Example - “/maps/api/staticmap?params…”.
-
#url(protocol = 'https') ⇒ Object
Returns the full URL to retrieve this static map.
Constructor Details
#initialize(attrs = {}) ⇒ GoogleStaticMap
Takes an optional hash of attributes
104 105 106 107 108 109 110 111 112 |
# File 'lib/googlestaticmap.rb', line 104 def initialize(attrs={}) defaults = {:width => 500, :height => 350, :markers => [], :visible => [], :sensor => false, :maptype => "roadmap", :paths => [], :proxy_port => nil, :proxy_address => nil, :api_key => nil, :client_id => nil, :private_key => nil, :language => nil} attributes = defaults.merge(attrs) attributes.each {|k,v| self.send("#{k}=".to_sym,v)} end |
Instance Attribute Details
#api_key ⇒ Object
API Key - see developers.google.com/maps/documentation/static-maps/get-api-key#key for details Note that if this is set, you cannot provide a client ID
72 73 74 |
# File 'lib/googlestaticmap.rb', line 72 def api_key @api_key end |
#center ⇒ Object
MapLocation for the center of the map. If this is not specified, the map will zoom to the markers
33 34 35 |
# File 'lib/googlestaticmap.rb', line 33 def center @center end |
#channel ⇒ Object
Channel - identifier channel for tracking API source in enterprise tools
see https://developers.google.com/maps/documentation/business/clientside/quota for details
87 88 89 |
# File 'lib/googlestaticmap.rb', line 87 def channel @channel end |
#client_id ⇒ Object
ClientId for business customers - see developers.google.com/maps/documentation/static-maps/get-api-key#key for details Note that if this is set, you cannot provide an API key.
77 78 79 |
# File 'lib/googlestaticmap.rb', line 77 def client_id @client_id end |
#format ⇒ Object
format of the image:
-
png8 - 8 bit PNG (default)
-
png32 - 32 bit PNG
-
gif
-
jpg
-
jpg-baseline - non-progressive JPEG
54 55 56 |
# File 'lib/googlestaticmap.rb', line 54 def format @format end |
#height ⇒ Object
Height of resulting image in pixels, defaults to 350, maximum 640
20 21 22 |
# File 'lib/googlestaticmap.rb', line 20 def height @height end |
#language ⇒ Object
Language - :en, :ja
see https://developers.google.com/maps/documentation/static-maps/intro for details
91 92 93 |
# File 'lib/googlestaticmap.rb', line 91 def language @language end |
#maptype ⇒ Object
Type of map to create:
-
roadmap (default)
-
satellite
-
terrain
-
hybrid - satellite imagery with roads
61 62 63 |
# File 'lib/googlestaticmap.rb', line 61 def maptype @maptype end |
#markers ⇒ Object
An optional array of MapMarker instances
23 24 25 |
# File 'lib/googlestaticmap.rb', line 23 def markers @markers end |
#paths ⇒ Object
An optional array of MapPath instances and/or MapPolygon instances to draw
29 30 31 |
# File 'lib/googlestaticmap.rb', line 29 def paths @paths end |
#plain_string ⇒ Object
In case when some parameter should be inserted manually For example, using mapstyle.withgoogle.com/ tool and inserting generated style as is
101 102 103 |
# File 'lib/googlestaticmap.rb', line 101 def plain_string @plain_string end |
#private_key ⇒ Object
The private key, also known as the URL signing secret, is used to to generate the signature parameter in the URL. This is required if you are using a client ID, or a premium API key, and is optional if you are using a standard API key. See developers.google.com/maps/documentation/static-maps/get-api-key for more details
83 84 85 |
# File 'lib/googlestaticmap.rb', line 83 def private_key @private_key end |
#proxy_address ⇒ Object
If you need to use a proxy server to reach Google, set the name/address of the proxy server here
65 66 67 |
# File 'lib/googlestaticmap.rb', line 65 def proxy_address @proxy_address end |
#proxy_port ⇒ Object
If proxy_address is set, set this to the port of the proxy server
68 69 70 |
# File 'lib/googlestaticmap.rb', line 68 def proxy_port @proxy_port end |
#scale ⇒ Object
1, 2, or 4 for Maps API Premier customers. Defaults to 1. Makes everything in the image appear larger, useful for displaying on high res mobile screens. When setting this, the image’s actual width and height in pixels will be scale * width and scale * height
46 47 48 |
# File 'lib/googlestaticmap.rb', line 46 def scale @scale end |
#sensor ⇒ Object
Applications that determine the user’s location via a sensor must set this to true, defaults to false
40 41 42 |
# File 'lib/googlestaticmap.rb', line 40 def sensor @sensor end |
#styles ⇒ Object
Styles - see developers.google.com/maps/documentation/maps-static/styling styles is should be represented as array of objects [‘featureArgument, element: ’elementArgument’, rule1: ‘rule1Arguement’, rule2: ‘rule2Arguement’, …, …]
97 98 99 |
# File 'lib/googlestaticmap.rb', line 97 def styles @styles end |
#visible ⇒ Object
An optional array of MapLocation instances that should remain visible on the map, though no markers or other indicators will be displayed
26 27 28 |
# File 'lib/googlestaticmap.rb', line 26 def visible @visible end |
#width ⇒ Object
Width of resulting image in pixels, defaults to 500, maximum 640
17 18 19 |
# File 'lib/googlestaticmap.rb', line 17 def width @width end |
#zoom ⇒ Object
0 (the whole world) to 21 (individual buildings)
36 37 38 |
# File 'lib/googlestaticmap.rb', line 36 def zoom @zoom end |
Instance Method Details
#get_map(output_file = nil, protocol = 'https') ⇒ Object
Connects to Google, retrieves the map, and returns the bytes for the image. Optionally, pass it an output name and the contents will get written to this file name output_file - optionally give the name of a file to write the output to.
Pass nil to not write the output to a file
protocol - specify http or https here for the protocol to retrieve the
map with. Defaults to http
return value - the binary data for the map
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/googlestaticmap.rb', line 176 def get_map(output_file=nil, protocol='https') protocol = 'https' unless protocol == 'http' || protocol == 'https' port = protocol == 'https' ? 443 : 80 http = Net::HTTP.Proxy(@proxy_address,@proxy_port).new("maps.googleapis.com", port) http.use_ssl = protocol == 'https' resp = http.get2(relative_url(protocol)) if resp && resp.is_a?(Net::HTTPSuccess) if output_file File.open(output_file, "wb") {|f| f << resp.body } end resp.body else if resp raise Exception.new("Error encountered while retrieving google map, code #{resp.code}, text #{resp.body}") else raise Exception.new("Error while retrieve google map, no response") end end end |
#get_styles ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/googlestaticmap.rb', line 197 def get_styles @styles.map do |style| values = style.each_pair.map do |(key, value)| "#{key.to_s}:#{value}" end ["style", values.join(CGI.escape('|'))] end end |
#relative_url(protocol = 'https') ⇒ Object
Returns the URL to retrieve the map, relative to maps.googleapis.com Example - “/maps/api/staticmap?params…”
163 164 165 |
# File 'lib/googlestaticmap.rb', line 163 def relative_url(protocol='https') url(protocol).gsub(/[^\/]*\/\/maps\.googleapis\.com/, "") end |
#url(protocol = 'https') ⇒ Object
Returns the full URL to retrieve this static map. You can use this as the src for an img to display an image directly on a web page Example - “maps.googleapis.com/maps/api/staticmap?params…” protocol can be ‘http’, ‘https’ or :auto. Specifying :auto will not return
a protocol in the URL ("//maps.googleapis.com/..."), allowing the browser to
select the appropriate protocol (if the page is loaded with https, it will
use https). Defaults to https
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/googlestaticmap.rb', line 121 def url(protocol='https') unless @center || @markers.length > 0 || @paths.length > 0 || @visible.length > 0 raise Exception.new("Need to specify either a center, markers, visible points, or a path") end if !@api_key.nil? && !@client_id.nil? raise Exception.new("You cannot specify both an API key and a client ID, only specify one") end if @api_key.nil? && @client_id.nil? 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.") end if !@client_id.nil? && @private_key.nil? raise Exception.new("private_key must be specified if using a client ID") end protocol = 'https' unless protocol == 'http' || protocol == 'https' || protocol == :auto protocol = protocol == :auto ? '' : protocol + ":" base = "#{protocol}//maps.googleapis.com" path = "/maps/api/staticmap?" attrs = GoogleStaticMapHelpers.safe_instance_variables(self, ["markers", "visible", "paths", "width", "height", "center", "proxy_address", "proxy_port", "api_key", "client_id", "private_key", "styles", "plain_string"], :cgi_escape_values => true).to_a attrs << ["size", "#{@width}x#{@height}"] if @width && @height @markers.each {|m| attrs << ["markers",m.to_s] } @paths.each {|p| attrs << ["path",p.to_s] } attrs << ["visible", @visible.join(MAP_SEPARATOR)] if !@visible.nil? && @visible.any? attrs << ["center", @center.to_s] if !@center.nil? attrs << ["key", @api_key] if !@api_key.nil? attrs << ["client", @client_id] if !@client_id.nil? get_styles.each { |style| attrs << style } if !@styles.nil? path << attrs.sort_by {|k,v| k}.collect {|attr| "#{attr[0]}=#{attr[1]}"}.join("&") path << "&#{@plain_string}" if !@plain_string.nil? if (!@api_key.nil? || !@client_id.nil?) && !@private_key.nil? signature = GoogleStaticMapHelpers.sign(path, @private_key) path << "&signature=" << signature end base + path end |