Class: Google4R::Maps::GMarker

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

Overview

Javascript GMarker instances represent markers. The Ruby class GMarker allows you to generate the Javascript which creates a new GMarker.

Use GMap2#create_marker to create new markers instead of intanciating them directly.

The Google API documentation can be found here: www.google.com/apis/maps/documentation/reference.html#GMarker

Example

map = GMap2.new("map", "map", :size => [ 100, 200 ])

my_icon = map.create_icon
...

marker = GMarker.new([ -42.0, 0 ])
marker.title = "Nice Title!"
marker.icon = my_icon
map.markers << marker

– TODO: Add support for info windows. ++

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(point) ⇒ GMarker

Creates a new GMarker instance at the given [ latitude, longitude ] point.



464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/google4r/maps.rb', line 464

def initialize(point)
  @point = point
  
  @onclick_handlers = Array.new
  
  # initialize defaults
  @drag_cross_move = false
  @clickable = true
  @draggable = false
  @bouncy = false
  @bounce_gravity = 1
end

Instance Attribute Details

#bounce_gravityObject

“Gravity” to use for bouncy markers. Defaults to 1.



448
449
450
# File 'lib/google4r/maps.rb', line 448

def bounce_gravity
  @bounce_gravity
end

#bouncyObject

True iff the marker is to bounce on the map when dropped. Defaults to false.



445
446
447
# File 'lib/google4r/maps.rb', line 445

def bouncy
  @bouncy
end

#clickableObject

True iff the marker is to respond to click events. Defaults to true.



439
440
441
# File 'lib/google4r/maps.rb', line 439

def clickable
  @clickable
end

#drag_cross_moveObject

True iff to keep the marker underneath the cursor. Defaults to false.



433
434
435
# File 'lib/google4r/maps.rb', line 433

def drag_cross_move
  @drag_cross_move
end

#draggableObject

True iff the marker is to be draggable. Defaults to false.



442
443
444
# File 'lib/google4r/maps.rb', line 442

def draggable
  @draggable
end

#iconObject

A GIcon instance that represents the icon to use for your class. The GIcon object must also be available in the belonging GMap. The default icon provided by Google is used iff this value is nil.



427
428
429
# File 'lib/google4r/maps.rb', line 427

def icon
  @icon
end

#info_window_htmlObject

HTML String value to display in the window opened by the JS method GMarker.openInfoWindowHtml() when the marker is clicked on. If unset then no window is opened.



461
462
463
# File 'lib/google4r/maps.rb', line 461

def info_window_html
  @info_window_html
end

#onclick_handlersObject

An array of Javascript to execute when the user clicks on the map. Each Javascript string will be wrapped into a function so global variables are not available outside this function.

The marker object will available as “marker” in the handler code you pass in.

Example: function(){ alert("My marker is " + marker); }



457
458
459
# File 'lib/google4r/maps.rb', line 457

def onclick_handlers
  @onclick_handlers
end

#pointObject

An array with the [ latitude, longitude ] of the GMarker instance.



430
431
432
# File 'lib/google4r/maps.rb', line 430

def point
  @point
end

#titleObject

String to display when hovering long enough over the marker.



436
437
438
# File 'lib/google4r/maps.rb', line 436

def title
  @title
end

Instance Method Details

#to_jsObject

Creates the Javascript to create a new marker. Creates an anonymous function returning a marker as documented in Google4R::Maps.



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/google4r/maps.rb', line 479

def to_js
  lines = Array.new

  # build constructor options
  options = []
  options << "dragCrossMove: #{(@drag_cross_move == true).to_s}"
  options << "title: #{@title.inspect}" unless @title.nil?
  options << "clickable: #{(@clickable == true).to_s}"
  options << "bouncy: #{(@bouncy == true).to_s}"
  options << "bounceGravity: #{@bounce_gravity}"

  # build JS to create GMarker
  lines << "var options = { #{options.join(", ")} }"
  lines << "if (icon != null) options['icon'] = icon;"
  lines << ""
  lines << "var marker = new GMarker(new GLatLng(#{@point.join(", ")}), options);"
  
  # set options settable with accessor
  if @draggable then
    lines << "marker.enableDragging();"
  else
    lines << "marker.disableDragging();"
  end
  lines << ""
  
  # build event handler setup
  lines << "/* Setup event handlers. */"
  onclick_handlers.each do |function_body|
    lines << %Q{GEvent.addListener(marker, "click", function() {
  #{function_body}
});}
  end
  
  # add event handler generated for the info_window_html attribute
  if not info_window_html.nil? then
    lines << %Q{GEvent.addListener(marker, "click", function() {
  marker.openInfoWindowHtml(#{@info_window_html.inspect});
});}
  end
  
  # build result
  result = Array.new
  result << "function(icon) {"
  result += lines.map { |str| "  " + str }
  result << "  "
  result << "  return marker;"
  result << "}"
  
  return result.join("\n")
end