AlexBrand
AlexBrand

Reputation: 12399

gmaps4Rails - json is being returned empty

I installed gmaps4rails in my application.

The view related stuff seems to be working, since the an empty google maps shows up. So I am thinking that I installed it correctly.

I followed the quick start on the github page:

My controller:

def map
    @businesses = Business.all
    @json = @businesses.to_gmaps4rails

    respond_to do |format|
      format.html
      format.json {render json:@businesses}
    end
  end

My view:

=gmaps4rails(@json)
=@json

I print @json and realized that the value is empty i.e. []

My business.rb model:

class Business < ActiveRecord::Base
  belongs_to :city
  belongs_to :category
  has_many :reviews

  attr_accessible :address, :description, :name, :phone, :urbanization, :url, :city_id

  include FriendlyId
  friendly_id :name, :use => :slugged

  acts_as_gmappable

  def gmaps4rails_address
    # "#{self.address}, #{self.urbanization}, #{self.city.name}"
    "619 Johnson Street, Kingston, ON"
  end
end

I hard coded the address for testing...

Upvotes: 3

Views: 466

Answers (2)

Michael Yagudaev
Michael Yagudaev

Reputation: 6129

If you need to add longitude and latitude to all your objects, you can run this in the rails console to get all coordinates.

Venue.all.each do |venue|
  geo_data = Gmaps4rails.geocode(venue.gmaps4rails_address)
  venue.update_attributes(latitude: geo_data[0][:lat], longitude: geo_data[0][:lng])
  sleep(1)
end

Upvotes: 0

apneadiving
apneadiving

Reputation: 115521

Gmaps4rails doesn't geocode on the fly for performance concerns.

to_gmaps4rails returns empty json if objects don't have lat or long.

You should preprocess your objects (simply save them).

Upvotes: 4

Related Questions