class MapProject::MapProject

Attributes

center_lat[RW]
center_long[RW]
geo_bounds_based_on_center[RW]
pixel_bounds_based_on_center[RW]
pixel_per_lat[RW]
pixel_per_long[RW]
viewport_size[RW]
world_coords[RW]

Public Class Methods

new(opts) click to toggle source
# File lib/map_project/map_project.rb, line 15
def initialize(opts)
  @viewport_w = opts[:viewport_w]
  @viewport_h = opts[:viewport_h]
  @center_lat = opts[:lat]
  @center_long = opts[:long]
  @zoom_level = opts[:zoom_level].nil? ? get_zoom_from_bound(opts[:bound]) : opts[:zoom_level]
  @tile_number = 2 ** @zoom_level
end

Public Instance Methods

offset_on_viewport(p_lat, p_long) click to toggle source

project the map coords to viewport, return a point's offsets on viewport (0,0) (0, x)

------------
|          |
| viewport |
|          |
------------

(y, 0) (x, y) Return: the projected offset of the point Usage example: css sprite

# File lib/map_project/map_project.rb, line 52
def offset_on_viewport(p_lat, p_long)
  input_pixel_coords = world_to_pixel(lat_lng_to_world(p_lat, p_long))

  [
    (pixel_bounds_based_on_center[:sw][1] - input_pixel_coords[1]).abs,
    (pixel_bounds_based_on_center[:ne][0] - input_pixel_coords[0]).abs
  ]
end
pixel_coords() click to toggle source
# File lib/map_project/map_project.rb, line 28
def pixel_coords
  @pixel_coords ||= world_to_pixel(world_coords)
end

Private Instance Methods

lat_lng_to_world(p_lat, p_long) click to toggle source
# File lib/map_project/map_project.rb, line 89
def lat_lng_to_world(p_lat, p_long)
  sin_y = Math.sin(Rational(p_lat * Math::PI, 180))
  sin_y = [[sin_y, -1].max, 1].min
  world_coord_long = TILE_SIZE * (0.5 + Rational(p_long, 360))
  world_coord_lat = TILE_SIZE * (0.5 - Rational(Math.log(Rational(1 + sin_y, 1 - sin_y)), 4 * Math::PI))
  [world_coord_lat, world_coord_long]
end
world_to_pixel(world_coords) click to toggle source
# File lib/map_project/map_project.rb, line 97
def world_to_pixel(world_coords)
  scale = 1 << @zoom_level
  [(world_coords[0] * scale).floor,
   (world_coords[1] * scale).floor]
end