Class: MapKit::BoundingBox

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

Overview

The class represents a bounding box specified by a top/left point and a bottom/right point (the coordinates can be pixels or degrees)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(top, left, bottom, right, zoom = nil) ⇒ BoundingBox

initialize the bounding box using the positions of two points and a optional zoom level

     top
left o------+
     |      |
     |      |
     +------o right
            bottom


69
70
71
# File 'lib/mapkit.rb', line 69

def initialize(top, left, bottom, right, zoom = nil)
  @top, @left, @bottom, @right, @zoom = top, left, bottom, right, zoom
end

Instance Attribute Details

#bottomObject

Returns the value of attribute bottom.



57
58
59
# File 'lib/mapkit.rb', line 57

def bottom
  @bottom
end

#leftObject

Returns the value of attribute left.



57
58
59
# File 'lib/mapkit.rb', line 57

def left
  @left
end

#rightObject

Returns the value of attribute right.



57
58
59
# File 'lib/mapkit.rb', line 57

def right
  @right
end

#topObject

Returns the value of attribute top.



57
58
59
# File 'lib/mapkit.rb', line 57

def top
  @top
end

#zoomObject

Returns the value of attribute zoom.



57
58
59
# File 'lib/mapkit.rb', line 57

def zoom
  @zoom
end

Instance Method Details

#centerObject

returns [lat, lnt] of bounding box



84
85
86
# File 'lib/mapkit.rb', line 84

def center
  [@left + (@right - @left) / 2, @top + (@bottom - @top) / 2]
end

#coordsObject

returns array of [top, left, bottom, right]



74
75
76
# File 'lib/mapkit.rb', line 74

def coords
  [@top, @left, @bottom, @right]
end

#grow(percent) ⇒ Object

grow bounding box by percentage and return new bounding box object



99
100
101
102
103
# File 'lib/mapkit.rb', line 99

def grow(percent)
  copy = self.clone
  copy.grow!(percent)
  copy
end

#grow!(percent) ⇒ Object

grow bounding box by percentage



89
90
91
92
93
94
95
96
# File 'lib/mapkit.rb', line 89

def grow!(percent)
  lng = percent * ((@right - @left) / 100)
  lat = percent * ((@top - @bottom) / 100)
  @top += lat
  @left -= lng
  @bottom -= lat
  @right += lng
end

#sspnObject

returns array of [width, height] of sspn



79
80
81
# File 'lib/mapkit.rb', line 79

def sspn
  [(@right - @left) / 2, (@bottom - @top) / 2]
end