Adresleri ve koordinatları aramak için geocoder taşını kullanmaya çalışıyorum. PostGIS mekansal veritabanım ve enlem ve boylam değerlerini ayrı ayrı kaydetmek yerine POINT özelliklerini kullanan RGeo gemiyle birlikte çalışmasını istiyorum. Yani bir RGeo NOKTASI özellik haline geocoder arama sonuçları kaydetmek üzere benim modele çalışmak için çalıştık:Geocoder adresleri geocoder gem ve PostGIS veritabanı ile RGeo
test = Location.new(name: "Eiffel Tower") // => #<Location id: nil, name: "Eiffel Tower", latlon: #<RGeo::Geographic::SphericalPointImpl:0x81579974 "POINT (0.0 0.0)">, created_at: nil, updated_at: nil, address: nil>
test.geocode // => #<RGeo::Geographic::SphericalPointImpl:0x81581ac0 "POINT (2.294254 48.858278)">
Harika: Şu anda yapabileceğim Raylar konsolunda
class Location < ActiveRecord::Base
attr_accessible :latlon, :name, :address
set_rgeo_factory_for_column(:latlon, RGeo::Geographic.spherical_factory(:srid => 4326))
geocoded_by :name_and_address do |obj, results|
if geo = results.first
obj.latlon = Location.rgeo_factory_for_column(:latlon).point(geo.longitude, geo.latitude)
end
end
after_initialize :init
after_validation :geocode
def init
self.latlon ||= Location.rgeo_factory_for_column(:latlon).point(0, 0)
end
def latitude
self.latlon.lat
end
def latitude=(value)
lon = self.latlon.lon
self.latlon = Location.rgeo_factory_for_column(:latlon).point(lon, value)
end
def longitude
self.latlon.lon
end
def longitude=(value)
lat = self.latlon.lat
self.latlon = Location.rgeo_factory_for_column(:latlon).point(value, lat)
end
def name_and_address
"#{self.name}, #{self.address}"
end
end
! Ben :geocode
için :after_validation
çağrıyı tetikler benim test Konumu, kaydetmek istediğiniz zaman Ancak, bir hata alıyorum:
NoMethodError: undefined method `lon' for nil:NilClass
from /Users/sg/rails-projects/geo_rails_test/app/models/location.rb:24:in `latitude='
from /Users/sg/rails-projects/geo_rails_test/app/models/location.rb:7:in `block in <class:Location>'
from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/geocoder-1.1.2/lib/geocoder/stores/base.rb:108:in `call'
from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/geocoder-1.1.2/lib/geocoder/stores/base.rb:108:in `do_lookup'
from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/geocoder-1.1.2/lib/geocoder/stores/active_record.rb:278:in `geocode'
from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `_run__1498365873506454431__validation__51840359655181017__callbacks'
[...]
coğrafi kod geri arama RGeo nesneyi siler görünüyor ki onun yöntemlerle inşa yanı sıra alıcı ve ayarlayıcı yöntemlerim artık mevcut değil. Neden? Herhangi bir fikir?