【问题标题】:Triggering auto geocoding using Geokit使用 Geokit 触发自动地理编码
【发布时间】:2014-03-19 02:28:54
【问题描述】:

我目前正在将 Geokit 实施到我的项目中。我想获取用户输入到创建网站的信息,对地址进行地理编码,并使用创建的新纬度和经度创建条目。但是,用户不会输入 lat 或 long,因为我们的 geokit 会为他们输入。总的来说,当用户按下“创建”按钮时,我们的控制器应该获取所有信息(假设它已经过验证)对给定地址进行地理编码,并使用生成的纬度和经度来创建新条目。我有一些大致的想法,但不确定直接更改控制器是否是正确的方法。我已经包含了我的控制器代码以获得视觉帮助。非常感谢!

总体问题 - 您将用于对地址进行地理编码并自动使用生成的纬度和经度来创建其余数据条目的方法放在哪里?

class PocsController < ApplicationController

# 获取 /pocs # 获取 /pocs.json 定义索引 @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

结束

# 获取 /pocs/1 # 获取 /pocs/1.json 定义显示 @poc = Poc.find(参数[:id]) @pocs = Poc.all

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

结束

# GET /pocs/new # 获取 /pocs/new.json 定义新 @poc = Poc.new

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

结束

# GET /pocs/1/edit 定义编辑 @poc = Poc.find(参数[:id]) 结束

#POST /pocs # 发布 /pocs.json 定义创建 @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

结束

#PUT /pocs/1 # 放置 /pocs/1.json 定义更新 @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

结束

# 删除 /pocs/1 # 删除 /pocs/1.json 定义破坏 @poc = Poc.find(参数[:id]) @poc.destroy

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

结束 结束

【问题讨论】:

  • 您的具体问题是什么?
  • 将地址地理编码并自动使用生成的纬度和经度创建其余数据条目的方法放在哪里?
  • 好的,在下面发布了答案。

标签: ruby-on-rails geokit


【解决方案1】:

根据您对问题本身的评论,听起来您正在寻求帮助,以确定在哪里插入地理编码。根据the docs (specifically the 'Auto Geocoding' section),Geokit 可以为您处理这个问题。所以,你的 app/model/Poc.rb 应该只需要:

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

请注意,您需要名为 addresslatlng 的字段(或者需要指定您自己的覆盖)。

因为它定义了一个before_validation_on_create,所以每当创建一个新的 Poc 时它都会自动运行。如果地址字段发生更改,这将不包括更新 lat/lng 字段的情况......但是通过研究 ActiveModel 回调,我敢打赌,如果需要,您可以弄清楚如何处理它。 :)

【讨论】:

  • 这很有帮助,因为我无法弄清楚(对不起,这是新的)但我现在想知道我们在模型中有这个信息,我们应该把获取地址、生成经纬度并将其用于创建新 POC 的代码?我见过这样的代码: require 'geokit' include GeoKit::Geocoders coords = MultiGeocoder.geocode(location) puts coords.lat puts coords.lng 但我不确定该放在哪里。非常感谢您的帮助,我真的很感激。
  • 该过程自动发生,因为您将:auto_geocode 设置为true。创建新 Poc 时,address 字段将被读取、地理编码并自动保存到 latlng 字段中。换句话说,您只需要指定address 字段,latlng 将在记录保存之前自动填充。
  • 是的,我可能错过了,但文档应该指定您需要自己添加地址、纬度和经度字段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-07
  • 2019-02-13
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多