Google Map Markers in YM4R/GM

   By chris on November 28th 2007 in /dev/rails | 3,370 views

When using YM4R/GM, by Guilhem Vellut, in your Rails app chances are you’ll want to mark points on the map using custom icons. The means to do so is not readily apparent or straight-forward. I’ve found that this works well:

map = GMap.new( Constants::GM_DIV_ID, Constants::GM_NAME )

# Define the start and end icons
map.icon_global_init( GIcon.new( :image => "/images/google_maps/icong.png", :icon_size => GSize.new( 24,38 ), :icon_anchor => GPoint.new(12,38), :info_window_anchor => GPoint.new(9,2) ), "icon_start")
map.icon_global_init( GIcon.new( :image => "/images/google_maps/iconr.png", :icon_size => GSize.new( 24,38 ), :icon_anchor => GPoint.new(12,38), :info_window_anchor => GPoint.new(9,2) ), "icon_stop" )

icon_start = Variable.new("icon_start"); icon_stop = Variable.new("icon_stop")

if respond_to?( "start_lat" ) && respond_to?( "start_long" )
    map.center_zoom_init( [start_lat, start_long], Constants::GM_ZOOM )
    map.overlay_init( GMarker.new( [start_lat, start_long], { :icon => icon_start, :title => name + " start", :info_window => "start" } ) )
end

if respond_to?( "end_lat" ) && respond_to?( "end_long" )
   map.overlay_init( GMarker.new( [end_lat, end_long], { :icon => icon_stop, :title => name + " end", :info_window => "end" } ) )
end

5 Responses to “Google Map Markers in YM4R/GM”

  1. georges responded on 16 Jun 2008 at 11:47 am #

    this was helpful, the docs aren’t very clear on custom icons.

  2. Jason Darrow responded on 23 Nov 2008 at 8:26 am #

    You have no idea how much time I burned on this before running across your post here. Thank you for stopping the pain. :-)

  3. Sam Scehnkman-Moore responded on 24 Dec 2008 at 12:03 pm #

    Like the others, THANK YOU

  4. Matt responded on 05 Feb 2009 at 8:08 am #

    Variable.new!!! I’ve been missing that for hours now. stupid little icon…

  5. chris responded on 05 Feb 2009 at 11:08 am #

    Glad it helps!