지오 코더가 '미국'과 같은 메시지를 받으면 하나의 중심 좌표 쌍을 선택한 다음 검색합니다. 따라서 귀하의 경우 미국의 중심에서 20 마일 이내에있는 사용자를 검색합니다. 당신이 원하는 무엇
은, 컨트롤러, 예를 들어, 검색을 지오 코딩하고 그것이 입력의 종류
그래서 (등 국가)를 발견하는 것입니다
def search
search = Geocoder.search(params[:search]).first
if search.types.includes?('country')
@users = User.where(country: search.address)
else
@users = User.near(search.coordinates, 20) # Will be ordered by distance by default
end
end
주소 구성 요소를 데이터베이스에 저장해야합니다.
일관된 데이터를 가지고하는 가장 좋은 방법은 사용자 정의 지오 콜백을 실행하는 것입니다 :
class User < ActiveRecord::Base
geocoded_by :address do |obj, results|
if result = results.first
obj.street = result.street_address
obj.zip = result.postal_code
obj.city = result.city
obj.country = result.country
obj.latitude = result.latitude
obj.longitude = result.longitude
end
end
after_validation :geocode, if: :address_changed?
def address
"#{street}, #{zip} #{city}, #{country}"
end
def address_changed?
street_changed? || city_changed? || zip_changed? || country_changed?
end
end
더 구체적인 검색어를 입력하면 작동합니까? (뉴욕 예를 들면?) –
@MichaelSzyndel 예. 우편 번호를 사용하는 경우와 같이 특정 용어로 작동합니다. –
그럼 국가를 검색하기 위해 지오 코딩을 사용하는 것은 무엇입니까? –