0

So I've searched through all the documentation on Geokit for rails and didn't find any setup process for it except for installing the gem which I know how to do. what tables do I add to an existing "listings" table?

I've like I said, have it installed. And I've added the acts_as_mappable to the listings ruby file.

class Listing < ApplicationRecord
  acts_as_mappable
end
gem 'geokit-rails'

It would be nice if there was a video tutorial done on how to set this entire thing up.

1 Answers1

1

You need to add a lat and lng field to your Listing model. You can do so like this:

rails g migration add_lat_lng_to_listings lat:decimal lng:decimal

Then migrate:

rails db:migrate

Once you have those fields, you should be able to do something like this:

Listing.in_range(1..35, origin: [29.794664, -98.731970])

  • 1..35 being the mile distance if I'm understanding correctly? – Timofey A Bogdanov Sep 26 '19 at 16:43
  • 1
    @TimofeyABogdanov Yup! – Mathias Pfeil Sep 26 '19 at 17:59
  • returns: "Listing id: 14, title: "iPhone 6s Plus", description: "$100", created_at: "2019-09-26 18:02:28", updated_at: "2019-09-26 18:02:28", user_id: 1, lat: nil, lng: nil" – Timofey A Bogdanov Sep 26 '19 at 18:04
  • @TimofeyABogdanov You will need to set the lat and lng before saving your record. You could do that in the controller by getting the ip address from the request object, then use Geokit to get the coordinates from that. After setting `@listing = Listing.new(listing_params)` in your create method, you could add `@listing.lat = Listing.geocode(request.ip).lat` and `@listing.lng = Listing.geocode(request.ip).lng` below it. That is just one way you could go. You could also allow users to enter their address, then use Geokit to turn the address into lat and lng coordinates. Hope that helps! – Mathias Pfeil Sep 26 '19 at 20:44
  • Just as a quick note, using `request.ip` probably wont work on localhost. Maybe just replace it with a placeholder IP address for some quick testing. – Mathias Pfeil Sep 26 '19 at 20:46