Search code examples
ruby-on-railsruby-on-rails-3gmaps4rails

Add marker to center location with gmaps4rails


I have an application where I'm showing a business listing and then displaying various jobs related to that business. The job locations are working great but I'd like to add a marker for the business which should be in the center which I can then customise.

Here is my controller/business_controller

def show
  @business = Business.find(params[:id])
  @distance = @business.service_area
  @jobs = Job.near(@business.location, @distance, {:order => :distance, :units => :km})
  @json = @jobs.to_gmaps4rails
  @mapoptions = {
       "map_options" => {"center_latitude" => @business.latitude, 
                         "center_longitude" => @business.longitude,
                         "auto_zoom" => true}
                 }
  respond_to do |format|
    format.html
  end
end

In my view views/businesses/show

<p><%= gmaps(:map_options => @mapoptions,"markers" => { "data" => @json }) %></p>

How can I create a marker for the business location?


Solution

  • I'd say:

    markers_array = JSON.parse @jobs.to_gmaps4rails
    markers_array << {lat: @business.latitude, lng: @business.longitude}
    @json = markers_array.to_json