Class: Mapstatic::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/mapstatic/map.rb

Constant Summary collapse

TILE_SIZE =
256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Map

Returns a new instance of Map.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mapstatic/map.rb', line 11

def initialize(params={})
  if params[:bbox]
    @bounding_box = params[:bbox].split(',').map(&:to_f)
  else
    @lat    = params.fetch(:lat).to_f
    @lng    = params.fetch(:lng).to_f
    @width  = params.fetch(:width).to_f
    @height = params.fetch(:height).to_f
  end
  @zoom = params.fetch(:zoom).to_i
  @tile_source = TileSource.new(params[:provider])
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/mapstatic/map.rb', line 8

def height
  @height
end

#latObject (readonly)

Returns the value of attribute lat.



8
9
10
# File 'lib/mapstatic/map.rb', line 8

def lat
  @lat
end

#lngObject (readonly)

Returns the value of attribute lng.



8
9
10
# File 'lib/mapstatic/map.rb', line 8

def lng
  @lng
end

#tile_sourceObject

Returns the value of attribute tile_source.



9
10
11
# File 'lib/mapstatic/map.rb', line 9

def tile_source
  @tile_source
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/mapstatic/map.rb', line 8

def width
  @width
end

#zoomObject (readonly)

Returns the value of attribute zoom.



8
9
10
# File 'lib/mapstatic/map.rb', line 8

def zoom
  @zoom
end

Instance Method Details

#metadataObject



49
50
51
52
53
54
55
56
57
# File 'lib/mapstatic/map.rb', line 49

def 
  {
    :bbox => bounding_box.join(','),
    :width => width.to_i,
    :height => height.to_i,
    :zoom => zoom,
    :number_of_tiles => required_tiles.length,
  }
end

#render_map(filename) ⇒ Object



45
46
47
# File 'lib/mapstatic/map.rb', line 45

def render_map(filename)
  to_image.write filename
end

#to_imageObject



38
39
40
41
42
43
# File 'lib/mapstatic/map.rb', line 38

def to_image
  base_image = create_uncropped_image
  base_image = fill_image_with_tiles(base_image)
  crop_to_size base_image
  base_image
end