Cartographer shows correct info box BUT over wrong marker

Hi guys (and gals),

The code formatting is so nice on SO.com, so I thought I'd just share my post in hopes that someone from the group can help.

Feel free to answer back via email if you don't have an SO account.

Thanks!

Abram

I may be wrong but I think that most here will not want to take the time to go over there to look at your problem. If you want to ask a question here it is best to ask it.

Colin

Thanks for that Colin.

I am trying to create a map with markers for Dispensers pulled from the DB. I am able to successfully do this, however, whenever I click on the markers the info box always appears over the same marker. The CORRECT dispenser's info box is popping up on the map, but over the wrong marker! Has anyone run across this issue?

In my controller:

def find_map

    @location = Location.new     @location.address = params[:location][:address]     @latlon = @location.geocode     @dispensers = Dispenser.near(@latlon)     @numrecords = 0     @lat =     @long =     @user_id =     @dispensers.each do |x|       @lat[@numrecords] = x.latitude       @long[@numrecords] = x.longitude       @user_id[@numrecords] = x.user_id       @numrecords += 1     end

    @map = Cartographer::Gmap.new( 'map')     @map.zoom = :bound     @icon = Cartographer::Gicon.new()     @map.icons << @icon

    @count = 0     @numrecords.times do       markername = "marker#{@count}"       markername = Cartographer::Gmarker.new(:name=> "Business", :marker_type => "Building",                              :position => [@lat[@count], @long[@count]],                              :info_window_url => "/bio/#{@user_id[@count]}", :icon => @icon)

      @map.markers << markername       @count += 1     end In my show.html.erb

  <%= raw Cartographer::Header.new.to_s %>   <%= raw @map.to_html %>

  <div style="width:350px;height:250px;" id="map" > [Map]</div>

Thanks again

Thanks for that Colin.

I am trying to create a map with markers for Dispensers pulled from the DB. I am able to successfully do this, however, whenever I click on the markers the info box always appears over the same marker. The CORRECT dispenser's info box is popping up on the map, but over the wrong marker! Has anyone run across this issue?

In my controller:

def find_map

   @location = Location.new    @location.address = params[:location][:address]    @latlon = @location.geocode    @dispensers = Dispenser.near(@latlon)    @numrecords = 0    @lat =    @long =    @user_id =    @dispensers.each do |x|      @lat[@numrecords] = x.latitude      @long[@numrecords] = x.longitude      @user_id[@numrecords] = x.user_id      @numrecords += 1    end

   @map = Cartographer::Gmap.new( 'map')    @map.zoom = :bound    @icon = Cartographer::Gicon.new()    @map.icons << @icon

   @count = 0    @numrecords.times do      markername = "marker#{@count}"      markername = Cartographer::Gmarker.new(:name=> "Business", :marker_type => "Building",                             :position => [@lat[@count], @long[@count]],                             :info_window_url => "/bio/#{@user_id[@count]}", :icon => @icon)

     @map.markers << markername      @count += 1    end In my show.html.erb

<%= raw Cartographer::Header.new.to_s %> <%= raw @map.to_html %>

<div style="width:350px;height:250px;" id="map" > [Map]</div>

Thanks again

Validate the rendered HTML in the W3C validator, see if you have duplicate HTML Element IDs somehow. That's what this sounds like precisely.

Walter

Walter. Thanks for your response. Unfortunately this doesn't seem to be the problem. W3C came up clean!

Great news, I figured it out. In the code below I had to add #{@count} to the business name... I am assuming the marker location is set with position and the marker itself just gets attached to the marker with the same :name attribute, which is presumably the last marker created by the loop.

Thanks!

        @numrecords.times do           markername = "marker#{@count}"           markername = Cartographer::Gmarker.new(:name=> "Business#{@count}", :marker_type => "Building",                      :position => [@lat[@count], @long[@count]],                      :info_window_url => "/bio/#{@user_id[@count]}", :icon => @icon)                                 @map.markers << markername           @count += 1           end

You might find https://github.com/apneadiving/Google-Maps-for-Rails worth a look.