Class: Smappy::Location

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

Direct Known Subclasses

Marker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(latitude, longitude) ⇒ Location

Returns a new instance of Location.



5
6
7
8
# File 'lib/smappy/location.rb', line 5

def initialize(latitude, longitude)
  self.latitude  = latitude
  self.longitude = longitude
end

Instance Attribute Details

#latitudeObject

Returns the value of attribute latitude.



3
4
5
# File 'lib/smappy/location.rb', line 3

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



3
4
5
# File 'lib/smappy/location.rb', line 3

def longitude
  @longitude
end

Instance Method Details

#position_on_map(map) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/smappy/location.rb', line 17

def position_on_map(map)
  tile   = to_tile(zoomlevel: map.zoomlevel)
  x1, y1 = position_on_tile(tile)
  x2, y2 = tile.position_on_map(map)
  
  [x1 + x2, y1 + y2]
end

#position_on_tile(tile) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/smappy/location.rb', line 25

def position_on_tile(tile)
  x = tile_x_at_zoomlevel(tile.zoomlevel)
  y = tile_y_at_zoomlevel(tile.zoomlevel)
  
  [x,y].map do |position|
    ((position - position.to_i) * Tile::SIZE).to_i
  end
end

#to_tile(options = {}) ⇒ Object



10
11
12
13
14
15
# File 'lib/smappy/location.rb', line 10

def to_tile(options = {})
  tile = Tile.new options
  tile.x = tile_x_at_zoomlevel(tile.zoomlevel)
  tile.y = tile_y_at_zoomlevel(tile.zoomlevel)
  tile
end