Sha256: 89a2ecc444079abd7d9ed633da6f74aaad2f25f096d1ea255674f14bc1078acb

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Decidim
  # This helper include some methods for rendering resources static and dynamic maps.
  module MapHelper
    # Renders a link to openstreetmaps with the resource latitude and longitude.
    # The link's content is a static map image.
    #
    # resource - A geolocalizable resource
    # options - An optional hash of options (default: { zoom: 17 })
    #           * zoom: A number to represent the zoom value of the map
    def static_map_link(resource, options = {})
      return unless resource.geocoded?

      zoom = options[:zoom] || 17
      latitude = resource.latitude
      longitude = resource.longitude

      map_url = "https://www.openstreetmap.org/?mlat=#{latitude}&mlon=#{longitude}#map=#{zoom}/#{latitude}/#{longitude}"

      link_to map_url, target: "_blank", rel: "noopener" do
        image_tag decidim.static_map_path(sgid: resource.to_sgid.to_s)
      end
    end

    def dynamic_map_for(markers_data)
      return if Decidim.geocoder.blank?

      map_html_options = {
        class: "google-map",
        id: "map",
        "data-markers-data" => markers_data.to_json
      }

      if Decidim.geocoder[:here_api_key]
        map_html_options["data-here-api-key"] = Decidim.geocoder[:here_api_key]
      else
        # Compatibility mode for old api_id/app_code configurations
        map_html_options["data-here-app-id"] = Decidim.geocoder[:here_app_id]
        map_html_options["data-here-app-code"] = Decidim.geocoder[:here_app_code]
      end

      content = capture { yield }.html_safe
      content_tag :div, class: "row column" do
        content_tag(:div, "", map_html_options) + content
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-core-0.21.0 app/helpers/decidim/map_helper.rb
decidim-core-0.20.1 app/helpers/decidim/map_helper.rb