gpt4 book ai didi

ruby-on-rails - Ruby Geocoder 访问结果信息

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:07 25 4
gpt4 key购买 nike

我正在尝试从 Geocoder 中获取州/县方法。

所以现在我正在做 result = Geocoder.search('53593')。哪个返回这个:

Geocoder: HTTP request being made for http://maps.googleapis.com/maps/api/geocode/json?address=53593&language=en&sensor=false
=> [#<Geocoder::Result::Google:0x000001092deab0 @data={"address_components"=>[{"long_name"=>"53593", "short_name"=>"53593", "types"=>["postal_code"]}, {"long_name"=>"Verona", "short_name"=>"Verona", "types"=>["locality", "political"]}, {"long_name"=>"Dane County", "short_name"=>"Dane County", "types"=>["administrative_area_level_2", "political"]}, {"long_name"=>"Wisconsin", "short_name"=>"WI", "types"=>["administrative_area_level_1", "political"]}, {"long_name"=>"United States", "short_name"=>"US", "types"=>["country", "political"]}], "formatted_address"=>"Verona, WI 53593, USA", "geometry"=>{"bounds"=>{"northeast"=>{"lat"=>43.083595, "lng"=>-89.4592069}, "southwest"=>{"lat"=>42.901066, "lng"=>-89.676706}}, "location"=>{"lat"=>42.999243, "lng"=>-89.5686271}, "location_type"=>"APPROXIMATE", "viewport"=>{"northeast"=>{"lat"=>43.083595, "lng"=>-89.4592069}, "southwest"=>{"lat"=>42.901066, "lng"=>-89.676706}}}, "place_id"=>"ChIJDVeAReqQB4gR9abCGbsr3ls", "postcode_localities"=>["Fitchburg", "Verona"], "types"=>["postal_code"]}, @cache_hit=nil>]
2.1.1 :016 >

根据文档:每个 Geocoder::Result 对象,result,提供以下数据:

result.latitude - float
result.longitude - float
result.coordinates - array of the above two
result.address - string
result.city - string
result.state - string
result.state_code - string
result.postal_code - string
result.country - string
result.country_code - string

但是当我尝试执行 result.stateresult.city 时,我得到了一个 undefined method 'city' 错误。

知道我需要做什么来获取这些数据吗?

最佳答案

Geocoder 返回结果的数组:

require 'geocoder'
result = Geocoder.search '53593'
result.class
#⇒ Array < Object

那么,你开始吧:

result.first.city
#⇒ "Verona"

关于ruby-on-rails - Ruby Geocoder 访问结果信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30965213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com