Module: EasyMapTiles

Defined in:
lib/easy_map_tiles.rb,
lib/easy_map_tiles/version.rb

Constant Summary collapse

TO_DEG =
Rational(180.0, Math::PI)
TO_RAD =
Rational(Math::PI, 180)
TILE_SIZE =

in px

256.0
EARTH_RADIUS =

in meters

6378137.0
PX_PER_RAD =
Rational(TILE_SIZE, (2 * Math::PI))
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.most_fitting_zoom_level(lat_deg, lon_deg, zoom_levels) ⇒ Object



35
36
37
38
39
# File 'lib/easy_map_tiles.rb', line 35

def most_fitting_zoom_level(lat_deg, lon_deg, zoom_levels)
  Array(zoom_levels).min_by do |zoom|
    to_world_px(lat_deg, lon_deg, zoom).reduce(0){|s,e| s += (TILE_SIZE / 2 - (e % TILE_SIZE)).abs}
  end
end

.tile_div(lat_deg, lon_deg, zoom_levels, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/easy_map_tiles.rb', line 41

def tile_div(lat_deg, lon_deg, zoom_levels, opts={})
  satellite = opts[:satellite] || false
  marker = opts[:marker] || false
  zoom = most_fitting_zoom_level(lat_deg, lon_deg, zoom_levels)
  if marker
    px,py = to_world_px(lat_deg, lon_deg, zoom).map{|e| (e % TILE_SIZE) / TILE_SIZE}
    styles = [
      "left: #{(px*100).round(3)}%",
      "top: #{(py*100).round(3)}%"
    ]
    styles += [
      'border-radius: 50%',
      'background: rgba(255,50,50,0.9)',
      'width: 12px',
      'height: 12px',
      'position: absolute',
      'box-shadow: 0px 0px 6px 2px rgba(255,50,50,0.9)'
    ] unless opts[:marker_styles] == false
    marker_div = "<div class='emt-marker' style='#{styles.join(';')}'></div>"
  end

  styles = [
    "background-image: url(\"#{tile_url(lat_deg, lon_deg, zoom, satellite)}\")"
  ]
  styles += [
    'background-size: cover',
    'position: relative',
    'display: inline-block',
    'width: 256px',
    'height: 256px'
  ] unless opts[:tile_styles] == false
  "<div class='emt-tile' style='#{styles.join(';')}'>#{marker_div}</div>"
end

.tile_url(lat_deg, lon_deg, zoom, satellite = false) ⇒ Object



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

def tile_url(lat_deg, lon_deg, zoom, satellite=false)
  px,py = to_world_px(lat_deg, lon_deg, zoom)
  x, y = Rational(px, TILE_SIZE).to_i, Rational(py, TILE_SIZE).to_i
  if satellite
    "https://khms#{rand(0..3)}.google.com/kh/v=131&x=#{x}&y=#{y}&z=#{zoom}"
  else
    "http://mt#{rand(0..3)}.google.com/vt/hl=en&x=#{x}&y=#{y}&z=#{zoom}"
  end
end

.to_world_px(lat_deg, lon_deg, zoom) ⇒ Object

returns the pixel coordinates at the given zoom level on the world map



14
15
16
17
18
19
20
21
22
23
# File 'lib/easy_map_tiles.rb', line 14

def to_world_px(lat_deg, lon_deg, zoom)
  # scale the latitude via mercator projections
  # see http://en.wikipedia.org/wiki/Mercator_projection#Derivation_of_the_Mercator_projection
  lat_scaled_rad = Math.log( Math.tan( Rational(Math::PI, 4) + Rational(TO_RAD * lat_deg, 2) ))
  # https://developers.google.com/maps/documentation/javascript/examples/map-coordinates
  wx = (TILE_SIZE / 2) + PX_PER_RAD * (TO_RAD * lon_deg)
  wy = (TILE_SIZE / 2) - PX_PER_RAD * lat_scaled_rad
  tiles_count = 2**zoom
  [(wx * tiles_count).to_i, (wy * tiles_count).to_i]
end