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>
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 info box location is set to the marker with
the same :name attribute, which is presumably the last marker created
by the loop.
Thanks!