Module: RailsGoogleMap::MapHelper
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/rails_google_map/map_helper.rb,
sig/rails_google_map.rbs
Overview
View helpers for embedding a Google Map.
In a Rails app the engine mixes this into ActionView automatically, so the helpers are available in every template. Elsewhere, include it yourself.
Constant Summary collapse
- EMBED_API_URL =
Maps Embed API endpoint. Requires a key and a billing-enabled project.
"https://www.google.com/maps/embed/v1/place"- KEYLESS_EMBED_URL =
Keyless embed endpoint. No key, no billing, but no zoom/maptype either.
"https://www.google.com/maps/embed"
Instance Method Summary collapse
-
#embed_google_map(options = {}) ⇒ Object
Hash-argument flavour of #google_map_for, kept for callers that already build an options hash: :address, :lat and :lng (or :long) plus anything #google_map_for accepts.
-
#google_map_embed_url(query, zoom: nil, map_type: nil, language: nil, api_key: nil) ⇒ String?
The embeddable Google Maps URL for
query(an address or a "lat,lng"). -
#google_map_for(query = nil, wrapper: nil, width: nil, height: nil, zoom: nil, map_type: nil, language: nil, api_key: nil, **html_options) ⇒ Object
Renders an
-
#render_google_map(query = nil, partial: nil, title: nil, wrapper_class: nil, map_class: nil, width: nil, height: nil, zoom: nil, map_type: nil, language: nil, api_key: nil, **locals) ⇒ Object
Renders the map through a partial instead of building the tag in Ruby, so the markup is yours to change.
Instance Method Details
#embed_google_map(options = {}) ⇒ Object
Hash-argument flavour of #google_map_for, kept for callers that already build an options hash: :address, :lat and :lng (or :long) plus anything #google_map_for accepts.
<%= embed_google_map(address: @business.address, height: 300) %>
<%= embed_google_map(lat: 27.7172, long: 85.3240) %>
133 134 135 136 |
# File 'lib/rails_google_map/map_helper.rb', line 133 def ( = {}) = .to_h.transform_keys(&:to_sym) google_map_for(.delete(:address), **) end |
#google_map_embed_url(query, zoom: nil, map_type: nil, language: nil, api_key: nil) ⇒ String?
The embeddable Google Maps URL for query (an address or a "lat,lng").
Uses the official Maps Embed API when a key is configured; without one it
falls back to the keyless embed endpoint. That fallback puts the place in
pb, not q: /maps/embed?q= answers with a blank page, and
/maps?q=...&output=embed only redirects to this same URL.
Returns nil for a blank query rather than a URL pointing nowhere.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rails_google_map/map_helper.rb', line 107 def (query, zoom: nil, map_type: nil, language: nil, api_key: nil) query = query.to_s.strip return if query.empty? config = RailsGoogleMap.configuration key = api_key || (config.api_key if config.api_key?) return "#{KEYLESS_EMBED_URL}?origin=mfe&pb=!1m2!2m1!1s#{CGI.escape(query)}" if key.nil? || key.to_s.strip.empty? params = { key: key, q: query, zoom: zoom || config.zoom, maptype: map_type || config.map_type, language: language || config.language }.reject { |_, value| value.nil? || value.to_s.empty? } "#{EMBED_API_URL}?#{URI.encode_www_form(params)}" end |
#google_map_for(query = nil, wrapper: nil, width: nil, height: nil, zoom: nil, map_type: nil, language: nil, api_key: nil, **html_options) ⇒ Object
Renders an
<%= google_map_for("Thamel, Kathmandu") %>
<%= google_map_for(@business.address, height: "300", zoom: 17) %>
<%= google_map_for(lat: 27.7172, lng: 85.3240) %>
query may be an address, a "lat,lng" string, or omitted in favour of the
:lat/:lng options. Returns nil when there is nothing to show, so a caller
can write if (map = google_map_for(...)).
Any option other than the ones named below is passed straight through as
an iframe attribute, e.g. class: "map" or title: "Our office".
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 |
# File 'lib/rails_google_map/map_helper.rb', line 37 def google_map_for(query = nil, wrapper: nil, width: nil, height: nil, zoom: nil, map_type: nil, language: nil, api_key: nil, **) query = google_map_query(query, .delete(:lat), .delete(:lng) || .delete(:long)) src = (query, zoom: zoom, map_type: map_type, language: language, api_key: api_key) return if src.nil? config = RailsGoogleMap.configuration attributes = { src: src, class: config.map_class, width: width || config.width, height: height || config.height, style: "border:0;", loading: "lazy", referrerpolicy: "no-referrer-when-downgrade", allowfullscreen: true, frameborder: "0" }.merge() iframe = content_tag(:iframe, "", attributes) wrapper_attributes = google_map_wrapper_attributes(wrapper, config) return iframe if wrapper_attributes.nil? content_tag(:div, iframe, wrapper_attributes) end |
#render_google_map(query = nil, partial: nil, title: nil, wrapper_class: nil, map_class: nil, width: nil, height: nil, zoom: nil, map_type: nil, language: nil, api_key: nil, **locals) ⇒ Object
Renders the map through a partial instead of building the tag in Ruby, so the markup is yours to change. Ships as ERB; run
rails generate rails_google_map:views --format slim
to copy it into your app as Slim (or ERB) and edit it there -- app view paths win over the engine's, so the copy takes over automatically.
= render_google_map(@business.map_query, title: t("partners.map_of", name: @business.name))
= render_google_map(@business.address, map_class: "partners-map")
Returns nil when there is nothing to map, exactly like #google_map_for. Options it does not recognise are passed to the partial as extra locals, which is how a custom partial receives its own data.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rails_google_map/map_helper.rb', line 79 def render_google_map(query = nil, partial: nil, title: nil, wrapper_class: nil, map_class: nil, width: nil, height: nil, zoom: nil, map_type: nil, language: nil, api_key: nil, **locals) query = google_map_query(query, locals.delete(:lat), locals.delete(:lng) || locals.delete(:long)) url = (query, zoom: zoom, map_type: map_type, language: language, api_key: api_key) return if url.nil? config = RailsGoogleMap.configuration render(partial: partial || config.partial, locals: { url: url, title: title, wrapper_class: wrapper_class || config.wrapper_class, map_class: map_class || config.map_class, width: width || config.width, height: height || config.height }.merge(locals)) end |