Class: Decidim::Map::StaticMap

Inherits:
Utility
  • Object
show all
Defined in:
lib/decidim/map/static_map.rb

Overview

A base class for static mapping functionality, common to all static map services.

Constant Summary collapse

DEFAULT_SIZE =
300
DEFAULT_ZOOM =
15

Instance Attribute Summary

Attributes inherited from Utility

#configuration, #locale, #organization

Instance Method Summary collapse

Methods inherited from Utility

#initialize

Constructor Details

This class inherits a constructor from Decidim::Map::Utility

Instance Method Details

#image_data(latitude:, longitude:, options: {}) ⇒ String

Creates a static map image data for the given map location with the given options.

Parameters:

  • latitude (Float, String)

    :latitude The latitude of the map position

  • longitude (Float, String)

    The longitude of the map position

  • options (Hash) (defaults to: {})

    Extra options for the static map URL

Returns:

  • (String)

    The raw data for the image.



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/decidim/map/static_map.rb', line 109

def image_data(latitude:, longitude:, options: {})
  request_url = url(
    latitude:,
    longitude:,
    options:
  )
  return "" unless request_url

  response = Faraday.get(request_url) do |req|
    req.headers["Referer"] = organization.host
  end
  response.body
end

Creates a link for the static maps. This will point to an external map service where the user can further explore the given location.

Parameters:

  • latitude (Float, String)

    The latitude of the map position to be linked to

  • longitude (Float, String)

    The longitude of the map position to be linked to

  • options (Hash{Symbol => Object}) (defaults to: {})

    The parameters for the static map URL

Options Hash (options:):

  • :zoom (Integer)

    A number to represent the zoom value of the map (default 17)

Returns:

  • (String)

    The link where the static map images link to.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/decidim/map/static_map.rb', line 20

def link(latitude:, longitude:, options: {})
  zoom = options.fetch(:zoom, 17)
  base_url = configuration.fetch(
    :link,
    "https://www.openstreetmap.org/"
  )

  params = { mlat: latitude, mlon: longitude }
  fragment = "map=#{zoom}/#{latitude}/#{longitude}"

  URI.parse(base_url).tap do |uri|
    uri.query = URI.encode_www_form(params)
    uri.fragment = fragment
  end.to_s
end

#url(latitude:, longitude:, options: {}) ⇒ String

Creates a URL that generates a static map image for the given map location with the given options.

Parameters:

  • latitude (Float, String)

    :latitude The latitude of the map position

  • longitude (Float, String)

    The longitude of the map position

  • options (Hash) (defaults to: {})

    Extra options for the static map URL

Options Hash (options:):

  • :zoom (Integer)

    Zoom level of the map image (default 15)

  • :width (Integer)

    Pixel width of the map image (default 300)

  • :height (Integer)

    Pixel height of the map image (default 300)

Returns:

  • (String)

    The URL to request for the static map image.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/decidim/map/static_map.rb', line 47

def url(latitude:, longitude:, options: {})
  map_url = configuration.fetch(:url, nil)
  return unless map_url

  # If a lambda or proc is passed as the :static_map_url configuration.
  if map_url.respond_to?(:call)
    return map_url.call(
      latitude:,
      longitude:,
      options:
    ).to_s
  end

  # Fetch the "extra" parameters from the configured map URL
  configured_uri = URI.parse(map_url)
  configured_params = Rack::Utils.parse_nested_query(
    configured_uri.query
  ).symbolize_keys

  # Generate a base URL without the URL parameters
  configured_uri.query = nil
  configured_uri.fragment = nil
  base_url = configured_uri.to_s

  # Generate the actual parameters by combining the configured parameters
  # with the provider specific parameters, giving priority to the
  # dynamically set parameters.
  params = configured_params.merge(
    url_params(
      latitude:,
      longitude:,
      options:
    )
  )

  # Generate the actual URL to call with all the prepared parameters.
  URI.parse(base_url).tap do |uri|
    uri.query = URI.encode_www_form(params)
  end.to_s
end

#url_params(latitude:, longitude:, options: {}) ⇒ Hash

Prepares the URL params for the static map URL.

Parameters:

  • latitude (Float, String)

    :latitude The latitude of the map position

  • longitude (Float, String)

    The longitude of the map position

  • options (Hash) (defaults to: {})

    Extra options for the static map URL

Returns:

  • (Hash)

    The parameters to pass to the static map image URL.



93
94
95
96
97
98
99
100
101
# File 'lib/decidim/map/static_map.rb', line 93

def url_params(latitude:, longitude:, options: {})
  {
    latitude:,
    longitude:,
    zoom: options.fetch(:zoom, DEFAULT_ZOOM),
    width: options.fetch(:width, DEFAULT_SIZE),
    height: options.fetch(:height, DEFAULT_SIZE)
  }
end