Class: GoogleStaticMapsHelper::Marker

Inherits:
Object
  • Object
show all
Defined in:
lib/google_static_maps_helper/marker.rb

Overview

A marker object is representing a marker with a customizable label, color and size.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :color => 'red',
  :size => 'mid',
  :label => nil,
  :icon => nil,
  :shadow => nil
}

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Marker

:call-seq:

new(location_object_or_options, *args)

Creates a new Marker object. A marker object will, when added to a Map, represent one marker which you can customize with color, size and label.

:location_object_or_options

Either an object which responds to lat and lng or simply a option hash

:args

A hash of options. Can have keys like :color, :size, and :label. See Google’s API documentation for more information. If a location object hasn’t been given you must also include :lat and :lng values.

Usage:

# Sets location via object which responds to lng and lat
GoogleStaticMapsHelper::Marker.new(location {:label => :a})

# ..or include the lng and lat in the option hash
GoogleStaticMapsHelper::Marker.new(:lng => 1, :lat => 2, :label => :a)

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# File 'lib/google_static_maps_helper/marker.rb', line 39

def initialize(*args)
  raise ArgumentError, "Must have one or two arguments." if args.length == 0
  extract_location!(args)
  options = DEFAULT_OPTIONS.merge(args.shift || {})
  validate_options(options)
  options.each_pair { |k, v| send("#{k}=", v) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Proxies calls to the internal object which keeps track of this marker’s location. So, if @location responds to method missing in this object, it will respond to it.



88
89
90
91
# File 'lib/google_static_maps_helper/marker.rb', line 88

def method_missing(method, *args)
  return @location.send(method, *args) if @location.respond_to? method
  super
end

Instance Method Details

#colorObject

:nodoc:



75
76
77
# File 'lib/google_static_maps_helper/marker.rb', line 75

def color # :nodoc:
  @color.downcase if @color
end

#has_icon?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/google_static_maps_helper/marker.rb', line 79

def has_icon?
  !!@icon
end

#labelObject

:nodoc:



71
72
73
# File 'lib/google_static_maps_helper/marker.rb', line 71

def label # :nodoc:
  @label.to_s.upcase if @label
end

#location_to_urlObject

Concatenation of lat and lng value. Used when building URLs and returns them in correct order



67
68
69
# File 'lib/google_static_maps_helper/marker.rb', line 67

def location_to_url # :nodoc:
  @location.to_url
end

#options_to_url_paramsObject

Returns a string representing this marker Used by the Map when building url.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/google_static_maps_helper/marker.rb', line 51

def options_to_url_params # :nodoc:
  params = DEFAULT_OPTIONS.keys.map(&:to_s).sort.inject([]) do |params, attr|
    primary_getter = "#{attr}_to_be_used_in_param"
    secondary_getter = attr

    value = send(primary_getter) rescue send(secondary_getter)
    params << "#{attr}:#{URI.escape(value.to_s)}" unless value.nil?
    params
  end

  params.join('|')
end