Class: MapPrint::LatLng

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

Constant Summary collapse

EARTH_RADIUS_IN_METERS =
6_376_772.71

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat, lng) ⇒ LatLng

Returns a new instance of LatLng.



29
30
31
32
# File 'lib/map_print/lat_lng.rb', line 29

def initialize(lat, lng)
  @lat = lat
  @lng = lng
end

Instance Attribute Details

#latObject

Returns the value of attribute lat.



5
6
7
# File 'lib/map_print/lat_lng.rb', line 5

def lat
  @lat
end

#lngObject

Returns the value of attribute lng.



5
6
7
# File 'lib/map_print/lat_lng.rb', line 5

def lng
  @lng
end

Class Method Details

.distance_between(from, to) ⇒ Object



8
9
10
11
12
# File 'lib/map_print/lat_lng.rb', line 8

def distance_between(from, to)
  return 0.0 if from.lat == to.lat && from.lng == to.lng

  distance_between_sphere(from, to)
end

Instance Method Details

#distance_to(other) ⇒ Object Also known as: distance_from



45
46
47
# File 'lib/map_print/lat_lng.rb', line 45

def distance_to(other)
  self.class.distance_between(self, other)
end

#get_slippy_map_tile_number(zoom) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/map_print/lat_lng.rb', line 34

def get_slippy_map_tile_number(zoom)
  return @get_slippy_map_tile_number if @get_slippy_map_tile_number

  lat_rad = @lat / 180 * Math::PI
  n = 2.0 ** zoom
  x = (@lng + 180.0) / 360.0 * n
  y = (1.0 - Math::log(Math::tan(lat_rad) + (1 / Math::cos(lat_rad))) / Math::PI) / 2.0 * n

  @get_slippy_map_tile_number = {x: x.to_i, y: y.to_i, offset: {x: x - x.to_i, y: y - y.to_i}}
end