2
我使用gem mongoid_special而不是mongoid_geo。主要問題是如何使用gmaps4rails輸入地址進行地理編碼,並在右邊的mongoid_special數組字段中保存lat和lng等座標?我預測很多協會的座標。示例性材料:如何通過gmaps4rails對地址進行地理編碼並將座標保存在數組中?
/haml view fields (autocompleted with google places)/
= f.text_field :from, :id => 'from'
= f.text_field :waypoints, :id => 'waypoints'
= f.text_field :to, :id => 'from'
class Trip
include Gmaps4rails::ActsAsGmappable
include Mongoid::Document
include Mongoid::Spacial::Document
field :from, :type => String
field :waypoints, type => String
field :to, type => String
field :from_coordinate, type: Array, spacial: {lat: :latitude, lng: :longitude, return_array: true }
field :to_coordinate, type: Array, spacial: {lat: :latitude, lng: :longitude, return_array: true }
embeds_many :coordinates
end
class Coordinates
include Mongoid::Document
include Mongoid::Spacial::Document
field :coordinates, type: Array, spacial: {lat: :latitude, lng: :longitude, return_array: true }
embedded_in :trip
end
另外的問題是如何從這個字段和顯示數據作爲谷歌方向得到反向地理?
編輯感謝提示我的解決方案,從下面的字段,其餘我可以管理。
before_save :from
def from=(from)
self.from_coordinate = Gmaps4rails.geocode(from).first
end