Ruby on Rails - 限制地理编码器对城市的响应并获取 ID
Ruby on Rails - Restrict geocoder response to city and get the ID
我在 Rails 项目上有一个 Ruby 和 users
table,我需要存储来自用户输入的 location_id
。
如果用户输入 Buckingham Palace
那么 location_id 应该是 london_id
如果用户输入 Wall Street
那么 location_id 应该是 new_york_id
如果用户输入 Statue of Liberty
那么 location_id 应该是 new_york_id
如果用户输入 United States
那么 location_id 应该是 nil
我正在使用 geocoder
gem,但我无法检索城市 ID。例如:
Geocoder.search("London").first.place_id # => ChIJdd4hrwug2EcRmSrV3Vo6llI
Geocoder.search("Buckingham Palace").first.place_id # => ChIJtV5bzSAFdkgRpwLZFPWrJgo
我需要两者都是 ChIJdd4hrwug2EcRmSrV3Vo6llI
(伦敦身份证)
这是我更新后的答案。它集成在 Geocoder 结构中,如果查询是城市名称,只需搜索一次:
require 'geocoder'
module Geocoder
# Returns the Google ID of the city containing the queried location. Returns nil if nothing found or if location contains multiple cities.
def self.get_city_id(query, options={})
results = search(query, options)
unless results.empty?
result = results.first
city = result.city
if city then
if city == query then
result.place_id
else
sleep(1)
search(city,options).first.place_id
end
end
end
end
end
Geocoder::Configuration.timeout = 15
["London", "Buckingham Palace", "Wall Street", "Statue of Liberty Monument", "United States", "WEIRDqueryWithNoResult"].each{|query|
puts query
puts Geocoder.get_city_id(query).inspect
sleep(1)
}
它输出:
London
"ChIJdd4hrwug2EcRmSrV3Vo6llI"
Buckingham Palace
"ChIJdd4hrwug2EcRmSrV3Vo6llI"
Wall Street
"ChIJOwg_06VPwokRYv534QaPC8g"
Statue of Liberty Monument
"ChIJOwg_06VPwokRYv534QaPC8g"
United States
nil
WEIRDqueryWithNoResult
nil
出于文档目的,这是我的原始答案:
require 'geocoder'
def get_city(search_term)
Geocoder.search(search_term).first.city
end
def get_place_id(search_term)
Geocoder.search(search_term).first.place_id
end
["London", "Buckingham Palace", "Wall Street", "Statue of Liberty Monument", "United States"].each{|term|
puts (city=get_city(term)) && sleep(1) && get_place_id(city)
sleep(1)
}
我在 Rails 项目上有一个 Ruby 和 users
table,我需要存储来自用户输入的 location_id
。
如果用户输入
Buckingham Palace
那么 location_id 应该是london_id
如果用户输入
Wall Street
那么 location_id 应该是new_york_id
如果用户输入
Statue of Liberty
那么 location_id 应该是new_york_id
如果用户输入
United States
那么 location_id 应该是nil
我正在使用 geocoder
gem,但我无法检索城市 ID。例如:
Geocoder.search("London").first.place_id # => ChIJdd4hrwug2EcRmSrV3Vo6llI
Geocoder.search("Buckingham Palace").first.place_id # => ChIJtV5bzSAFdkgRpwLZFPWrJgo
我需要两者都是 ChIJdd4hrwug2EcRmSrV3Vo6llI
(伦敦身份证)
这是我更新后的答案。它集成在 Geocoder 结构中,如果查询是城市名称,只需搜索一次:
require 'geocoder'
module Geocoder
# Returns the Google ID of the city containing the queried location. Returns nil if nothing found or if location contains multiple cities.
def self.get_city_id(query, options={})
results = search(query, options)
unless results.empty?
result = results.first
city = result.city
if city then
if city == query then
result.place_id
else
sleep(1)
search(city,options).first.place_id
end
end
end
end
end
Geocoder::Configuration.timeout = 15
["London", "Buckingham Palace", "Wall Street", "Statue of Liberty Monument", "United States", "WEIRDqueryWithNoResult"].each{|query|
puts query
puts Geocoder.get_city_id(query).inspect
sleep(1)
}
它输出:
London
"ChIJdd4hrwug2EcRmSrV3Vo6llI"
Buckingham Palace
"ChIJdd4hrwug2EcRmSrV3Vo6llI"
Wall Street
"ChIJOwg_06VPwokRYv534QaPC8g"
Statue of Liberty Monument
"ChIJOwg_06VPwokRYv534QaPC8g"
United States
nil
WEIRDqueryWithNoResult
nil
出于文档目的,这是我的原始答案:
require 'geocoder'
def get_city(search_term)
Geocoder.search(search_term).first.city
end
def get_place_id(search_term)
Geocoder.search(search_term).first.place_id
end
["London", "Buckingham Palace", "Wall Street", "Statue of Liberty Monument", "United States"].each{|term|
puts (city=get_city(term)) && sleep(1) && get_place_id(city)
sleep(1)
}