Class: Google4R::Maps::GMarker
- Inherits:
-
Object
- Object
- Google4R::Maps::GMarker
- 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
-
#bounce_gravity ⇒ Object
“Gravity” to use for bouncy markers.
-
#bouncy ⇒ Object
True iff the marker is to bounce on the map when dropped.
-
#clickable ⇒ Object
True iff the marker is to respond to click events.
-
#drag_cross_move ⇒ Object
True iff to keep the marker underneath the cursor.
-
#draggable ⇒ Object
True iff the marker is to be draggable.
-
#icon ⇒ Object
A GIcon instance that represents the icon to use for your class.
-
#info_window_html ⇒ Object
HTML String value to display in the window opened by the JS method GMarker.openInfoWindowHtml() when the marker is clicked on.
-
#onclick_handlers ⇒ Object
An array of Javascript to execute when the user clicks on the map.
-
#point ⇒ Object
An array with the [ latitude, longitude ] of the GMarker instance.
-
#title ⇒ Object
String to display when hovering long enough over the marker.
Instance Method Summary collapse
-
#initialize(point) ⇒ GMarker
constructor
Creates a new GMarker instance at the given [ latitude, longitude ] point.
-
#to_js ⇒ Object
Creates the Javascript to create a new marker.
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_gravity ⇒ Object
“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 |
#bouncy ⇒ Object
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 |
#clickable ⇒ Object
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_move ⇒ Object
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 |
#draggable ⇒ Object
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 |
#icon ⇒ Object
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_html ⇒ Object
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_handlers ⇒ Object
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 |
#point ⇒ Object
An array with the [ latitude, longitude ] of the GMarker instance.
430 431 432 |
# File 'lib/google4r/maps.rb', line 430 def point @point end |
#title ⇒ Object
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_js ⇒ Object
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 = [] << "dragCrossMove: #{(@drag_cross_move == true).to_s}" << "title: #{@title.inspect}" unless @title.nil? << "clickable: #{(@clickable == true).to_s}" << "bouncy: #{(@bouncy == true).to_s}" << "bounceGravity: #{@bounce_gravity}" # build JS to create GMarker lines << "var 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 |