PhilBillingsby
PhilBillingsby

Reputation: 11

Use Ruby Geocoder gem to create Location object

I need to use the Ruby Geocoder gem to create an object using attributes back from an address search query. I expect to create a new location from the received data from the Geocoder results and create a new object with attributes from the migration. I have searched resources online to see how to create an object from the results but I need to know how to get the locations attributes from longitude and latitude co-ordinates.

expected example search query: 'Wall St, NY' => {address: "Wall Street", city: "New York, state: "New York", country: "United States of America"

Location Model

class Location < ApplicationRecord
  has_many :users
  geocoded_by :address
  after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
end

LocationController#create

def create
    @location = Location.new(location_params)
    if @location.save
      flash[:success] = "location added!"
      redirect_to location_path(@location)
    else
      render 'new'
    end
  end

Migration

class CreateLocations < ActiveRecord::Migration[6.0]
  def change
    create_table :locations do |t|
      t.string :address
      t.string :city
      t.string :state
      t.string :country
      t.float :longitude
      t.float :latitude
    end
  end
end

Upvotes: 1

Views: 784

Answers (1)

Viktor
Viktor

Reputation: 2783

expected example search query: 'Wall St, NY' => {address: "Wall Street", city: "New York, state: "New York", country: "United States of America"

You can use the #search function:

Geocoder.serach("Wall ST, NY")

This will return an array of results. The first one, for example, looks like this:

#=>  => #<Geocoder::Result::Nominatim:0x00007ffc62aa37d0 @data={"place_id"=>184441192, "licence"=>"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright", "osm_type"=>"way", "osm_id"=>447018423, "boundingbox"=>["40.7051753", "40.706379", "-74.009502", "-74.0074038"], "lat"=>"40.7060194", "lon"=>"-74.0088308", "display_name"=>"Wall Street, Financial District, Manhattan Community Board 1, Manhattan, New York County, New York, 10005, United States of America", "class"=>"highway", "type"=>"residential", "importance"=>0.758852318921325, "address"=>{"road"=>"Wall Street", "suburb"=>"Financial District", "city"=>"Manhattan Community Board 1", "county"=>"New York County", "state"=>"New York", "postcode"=>"10005", "country"=>"United States of America", "country_code"=>"us"}}, @cache_hit=nil> 

You can take the values you need and assign them to a new Location object.

The search function can also take an array of two with longitude and latitude:

Geocoder.search([lat, lng])

Unfortunately, if I understood you correctly, and location_params are the results of the query, I don't think you can do just this:

    @location = Location.new(location_params)

You will have to extract the necessary attributes from the results of the search query first and then provide them to the #new method of Location.

Upvotes: 1

Related Questions