해야합니다. 그것은 지오 코더 구조에 통합하고, 쿼리가 도시 이름 인 경우 하나 개의 검색을 필요로한다 :
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)
}
지오 코드 보석을 사용하고 있습니까? –
예, 질문에 설명 된대로 – user4523968