Class: StaticMap
- Inherits:
-
Object
- Object
- StaticMap
- Defined in:
- lib/google_maps_embed/static_map.rb
Overview
This module provides functionality to generate URLs for Google Maps Static API. It supports specifying map parameters like center (latitude/longitude or address), zoom level, markers, paths, and various other options.
The primary method, ‘GoogleMapsEmbed::StaticMap.url`, constructs the URL with the provided options and includes the necessary API key. If a secret key is configured, it will sign the URL to ensure secure usage of the API. module GoogleMapsEmbed
Class Method Summary collapse
Class Method Details
.parse_options(options, option_type) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/google_maps_embed/static_map.rb', line 68 def self.(, option_type) raise ArgumentError, "Markers should be given as an array of objects." unless .is_a?(Array) = [] .each do |option| current = [] option.each do |k, v| current << "#{k}:#{URI.encode_www_form_component(v)}" if v.is_a? String current << URI.encode_www_form_component(v.join("|")) if v.is_a? Array end << ("#{option_type}=" + current.join("%7C")) end .join("&").gsub("%2C", ",") end |
.url(options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/google_maps_embed/static_map.rb', line 15 def self.url( = {}) unless (.key?(:latitude) && .key?(:longitude) && .key?(:zoom)) || (.key?(:address) && .key?(:zoom)) || .key?(:markers) || .key?(:path) raise ArgumentError, "Maps Static API requires 'center' and 'zoom' parameters if 'markers' not present" end coords = .key?(:latitude) && .key?(:longitude) ? "#{[:latitude]},#{[:longitude]}" : "" center = if coords.empty? && .key?(:address) "center=#{URI.encode_www_form_component([:address])}" else "center=#{coords}" end api_key = [[:api_key], GoogleMapsEmbed.configuration.static_key, GoogleMapsEmbed.configuration.api_key].compact.first = { width: 400, height: 400 } = .merge() base = "https://maps.googleapis.com/maps/api/staticmap?" base_url = [base, center, "size=#{[:width]}x#{[:height]}", "zoom=#{[:zoom]}"] .reject(&:empty?).join("&").gsub("%2C", ",") markers_url = .key?(:markers) ? ([:markers], "markers") : "" path_url = .key?(:path) ? ([:path], "path") : "" exclude_keys = %i[latitute longitude address zoom path api_key width height markers] = .reject { |key, _| exclude_keys.include?(key) } extra_url = .map do |key, value| "#{key}=#{URI.encode_www_form_component(value)}" end.join("&").gsub("%2C", ",") api_url = "key=#{api_key}" url = [base_url, markers_url, path_url, extra_url, api_url].reject(&:empty?).join("&") if GoogleMapsEmbed.configuration.secret_key UrlSigner.sign(url, GoogleMapsEmbed.configuration.secret_key) else url end end |