Class: Google4R::Maps::GIcon

Inherits:
Object
  • Object
show all
Defined in:
lib/google4r/maps.rb

Overview

The Ruby GIcon class wraps around the Javascript GIcon class to create custom icons for markers.

You should not create GIcon instances directly but use the GMap2#create_icon factory method for this.

See www.google.com/apis/maps/documentation/reference.html#GIcon for more details on the attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(copy = false) ⇒ GIcon

If the parameter copy is set to true then the generated Javascript will base the icon on G_DEFAULT_ICON. You only have to specify the parameters you want to override in this case.

G_DEFAULT_ICON based markers - what does this mean? A part of the generated code will look like this:

var marker = new GIcon(G_DEFAULT_ICON)
marker.image = "..."
// other properties of the marker


595
596
597
# File 'lib/google4r/maps.rb', line 595

def initialize(copy=false)
  @copy = (copy == true)
end

Instance Attribute Details

#copyObject

True iff to create the GIcon object based on G_DEFAULT_ICON. – TODO: We might choose to allow Ruby GIcon instances here to allow creation based on those GIcons later on. ++



542
543
544
# File 'lib/google4r/maps.rb', line 542

def copy
  @copy
end

#icon_anchorObject

Pixel coordinates relative to the top left corner of the icon image at which the icon is to be anchored to the map as [width, height].



558
559
560
# File 'lib/google4r/maps.rb', line 558

def icon_anchor
  @icon_anchor
end

#icon_sizeObject

The pixel size of the foreground image of the icon as [width, height].



551
552
553
# File 'lib/google4r/maps.rb', line 551

def icon_size
  @icon_size
end

#imageObject

URL of the image to use as the foreground.



545
546
547
# File 'lib/google4r/maps.rb', line 545

def image
  @image
end

#image_mapObject

An array of [width, height] specifications to use to identify the clickable part in other browsers than MSIE.



583
584
585
# File 'lib/google4r/maps.rb', line 583

def image_map
  @image_map
end

#info_window_anchorObject

Pixel coordinates relative to the top left corner of the icon image at which the info window is to be anchored to the map as [width, height].



562
563
564
# File 'lib/google4r/maps.rb', line 562

def info_window_anchor
  @info_window_anchor
end

#moz_print_imageObject

The URL of the foreground icon image used for printed maps in Firefox/Mozilla. It must be the same size as the main icon image given by image.



570
571
572
# File 'lib/google4r/maps.rb', line 570

def moz_print_image
  @moz_print_image
end

The URL of the foreground icon image used for printed maps as [width, height]. It must be the same size as the main icon image given by image.



566
567
568
# File 'lib/google4r/maps.rb', line 566

def print_image
  @print_image
end

The URL of the shadow image used for printed maps. It should be a GIF image since most browsers cannot print PNG images.



574
575
576
# File 'lib/google4r/maps.rb', line 574

def print_shadow
  @print_shadow
end

#shadowObject

URL of the image to use as the shadow.



548
549
550
# File 'lib/google4r/maps.rb', line 548

def shadow
  @shadow
end

#shadow_sizeObject

The pixel size of the shadow image as [width, height].



554
555
556
# File 'lib/google4r/maps.rb', line 554

def shadow_size
  @shadow_size
end

#transparentObject

The URL of a virtually transparent version of the foreground icon image used to capture click events in Internet Explorer. This image should be a 24-bit PNG version of the main icon image with 1% opacity, but the same shape and size as the main icon.



579
580
581
# File 'lib/google4r/maps.rb', line 579

def transparent
  @transparent
end

Instance Method Details

#to_jsObject

Creates an anonymous Javascript function that creates a GIcon. See the notes on the Google4R::Maps module on the generated Javascript. The generated Javascript looks as follows:

function() {
  var icon = new GIcon();
  icon.image = "image";
  icon.shadow = "shadow";
  icon.printImage = "print image";
  icon.printShadow = "print shadow";
  icon.transparent = "transparent";
  icon.iconSize = new GSize(1, 2);
  icon.shadowSize = new GSize(3, 4);
  icon.iconAnchor = new GPoint(5, 6);
  icon.infoWindowAnchor = new GPoint(7, 8);
  icon.imageMap = [ 10, 11, 12, 13, 14, 15 ];

  return icon;
}


618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/google4r/maps.rb', line 618

def to_js
  lines = []
  
  if @copy then
    lines << %Q{var icon = new GIcon(G_DEFAULT_ICON);}
  else
    lines << %Q{var icon = new GIcon();}
  end
  
  # String properties.
  [
    [ :image, 'image' ], [ :shadow, 'shadow' ], [ :print_image, 'printImage' ], [ :moz_print_image, 'mozPrintImage' ],
    [ :print_shadow, 'printShadow' ], [ :transparent, 'transparent' ]
  ].each do |ruby_name, js_name|
    lines << %Q{icon.#{js_name} = #{self.send(ruby_name).dump};} unless self.send(ruby_name).nil?
  end
  
  # GSize properties.
  [ [ :icon_size, 'iconSize' ], [ :shadow_size, 'shadowSize' ] ].each do |ruby_name, js_name|
    lines << %Q{icon.#{js_name} = new GSize(#{self.send(ruby_name).join(', ')});} unless self.send(ruby_name).nil?
  end
  
  # GPoint properties.
  [ [ :icon_anchor, 'iconAnchor' ], [ :info_window_anchor, 'infoWindowAnchor' ] ].each do |ruby_name, js_name|
    lines << %Q{icon.#{js_name} = new GPoint(#{self.send(ruby_name).join(', ')});} unless self.send(ruby_name).nil?
  end
  
  # The image map.
  lines << %Q{icon.imageMap = [ #{@image_map.flatten.join(', ')} ];} unless @image_map.nil?
  
  # Build result string.
  result = Array.new
  
  result << "function() {"
  result += lines.map { |str| "  " + str }
  result << "  "
  result << "  return icon;"
  result << "}"
  return result.join("\n")
end