1

I'm currently working on implementing Geokit into my project. I want to take the info that a user enters onto the create website, geocode the address, and create the entry with the new latitude and longitude created. However, lat or long won't be entered by the user because our geokit will do it for them. Overall, when the user presses the "Create" button, our controller should take all the info (assuming it's been validated) geocode the given address, and use the generated lat and long to create the new entry. I have some general idea how to but not exactly sure if directly changing the controller is the right method to do so. I've included my controller code for visual help. Many thanks in advance!

Overall question - Where do you put the method for geocoding an address and automatically using the generated lat and long to create the rest of the data entry?

class PocsController < ApplicationController

# GET /pocs # GET /pocs.json def index @pocs = Poc.all @pocs = Poc.find(:all) @pocs.sort! { |a,b| a.name.downcase <=> b.name.downcase }

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @pocs }
end

end

# GET /pocs/1 # GET /pocs/1.json def show @poc = Poc.find(params[:id]) @pocs = Poc.all

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @poc }
end

end

# GET /pocs/new # GET /pocs/new.json def new @poc = Poc.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @poc }
end

end

# GET /pocs/1/edit def edit @poc = Poc.find(params[:id]) end

# POST /pocs # POST /pocs.json def create @poc = Poc.new(params[:poc])

respond_to do |format|
  if @poc.save
    format.html { redirect_to @poc, notice: 'Poc was successfully created.' }
    format.json { render json: @poc, status: :created, location: @poc }
  else
    format.html { render action: "new" }
    format.json { render json: @poc.errors, status: :unprocessable_entity }
  end
end

end

# PUT /pocs/1 # PUT /pocs/1.json def update @poc = Poc.find(params[:id])

respond_to do |format|
  if @poc.update_attributes(params[:poc])
    format.html { redirect_to @poc, notice: 'Poc was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @poc.errors, status: :unprocessable_entity }
  end
end

end

# DELETE /pocs/1 # DELETE /pocs/1.json def destroy @poc = Poc.find(params[:id]) @poc.destroy

respond_to do |format|
  format.html { redirect_to pocs_url }
  format.json { head :no_content }
end

end end

tkrajcar
  • 1,702
  • 1
  • 17
  • 34

1 Answers1

1

Based on your comment on the question itself, it sounds like you're looking for help figuring out where to plug in the geocoding. According to the docs (specifically the 'Auto Geocoding' section), Geokit can handle this for you. So, your app/model/Poc.rb should just need:

class Poc < ActiveRecord::Base
  acts_as_mappable :auto_geocode=>true
end

Note you'll need fields named address, lat, and lng (or will need to specify your own overrides).

Since this defines a before_validation_on_create, it will run automatically whenever a new Poc is created. That won't cover the case of updating the lat/lng fields if the address field changes... but by researching ActiveModel callbacks, I bet you can figure out how to handle that, if it's needed. :)

tkrajcar
  • 1,702
  • 1
  • 17
  • 34
  • 1
    This is very helpful because I wouldn't have been able to figure that out (sorry, new to this) but I was wondering now that we have this info in the model, where do we put the code that takes the address, generates the lat and long, and uses that in the creation of a new POC? I've seen code like: require 'geokit' include GeoKit::Geocoders coords = MultiGeocoder.geocode(location) puts coords.lat puts coords.lng but I'm not exactly sure where to put it. Thank you so much for your help I really appreciate it. – user3242950 Feb 19 '14 at 01:41
  • That process happens automatically since you are setting `:auto_geocode` to true. The `address` field will be read, geocoded, and saved into the `lat` and `lng` fields automatically when you create a new Poc. Said another way, you just need to specify the `address` field and `lat` and `lng` will be auto-populated before the record is saved. – tkrajcar Feb 19 '14 at 03:46
  • Yes, I may have missed it, but the docs should have specified that you needed to add the address, lat and lng fields yourself. – iphone007 Sep 08 '15 at 06:10