Class: Dotter::Tile

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

Overview

Dotter::Tile is abstracting google maps’ tiles (squares, usually 256x256, that are loaded as parts of the map).

More on tiles and how google maps works can be found here: code.google.com/apis/maps/documentation/overlays.html#Google_Maps_Coordinates

Usage

To generate a tile you must give upper left corner of a tile and zoom.

tile_start = Dotter::LatLng.new(tile_start_latitude, tile_start_longitude)
tile = Dotter::Tile.new(tile_start, zoom)

tile.locations = locations_array # locations should respond_to latitude and longitude

tile.image # generates RMagick::Magick image with "png" format set

You can also use tile in Dotter::Dotter explicitly (tile.image is a simple helper, but if you want to set some specific options you will have to do that this way):

dots = tile.generate_xy_coordinates!
dotter = Dotter::Dotter.new(:dots => dots)
dotter.generate_image

It passes locations changed into points relative to current tile’s start.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(latlng, zoom) ⇒ Tile

Returns a new instance of Tile.



25
26
27
28
# File 'lib/dotter/tile.rb', line 25

def initialize(latlng, zoom)
  @zoom = zoom.to_i
  @start = Dotter::GMap.latlng_to_pixel(latlng, @zoom)
end

Instance Attribute Details

#locationsObject

Returns the value of attribute locations.



24
25
26
# File 'lib/dotter/tile.rb', line 24

def locations
  @locations
end

#startObject

Returns the value of attribute start.



24
25
26
# File 'lib/dotter/tile.rb', line 24

def start
  @start
end

#zoomObject

Returns the value of attribute zoom.



24
25
26
# File 'lib/dotter/tile.rb', line 24

def zoom
  @zoom
end

Instance Method Details

#convert_places_to_pointsObject



49
50
51
# File 'lib/dotter/tile.rb', line 49

def convert_places_to_points
  self.locations = locations_as_points
end

#generate_xy_coordinates!Object

Method sets x and y coordinates for each place



55
56
57
58
59
60
61
# File 'lib/dotter/tile.rb', line 55

def generate_xy_coordinates!
  locations_as_points do |point, location|
    location.x = point.x
    location.y = point.y
  end
  locations
end

#imageObject

Generate tile image with Dotter



64
65
66
67
68
69
# File 'lib/dotter/tile.rb', line 64

def image
  dotter = Dotter::Dotter.new(:dots => locations)
  img = dotter.generate_image
  img.format = "png"
  img
end

#locations_as_points(options = {}) ⇒ Object

Method that takes loactions passed to Tile and converts them into points with x an y relative to tile start

Returns

Array[~x, ~y]

points array



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dotter/tile.rb', line 34

def locations_as_points(options = {})
  points = []
  locations.each do |location|
    latlng = Dotter::LatLng.new(location.latitude, location.longitude)
    point = Dotter::GMap.latlng_to_pixel(latlng, zoom)
    point = point - start # location is relative to big map, let's make it relative to start of tile
    if block_given?
      yield(point, location)
    else
      points << point
    end
  end
  points
end