0

我用我用https://github.com/alexreisner/geocoder寶石和讀取數值時,我打電話:results = Geocoder.search("McCarren Park, Brooklyn, NY")我的結果看起來變種就像這裏:如何從ActiveRecord的

[#<Geocoder::Result::Google:0x007fc0e6a016e0 @data={"address_components"=> 
[{"long_name"=>"McCarren Park", "short_name"=>"McCarren Park", "types"=>["point_of_interest", "establishment"]}, {"long_name"=>"776", "short_name"=>"776", "types"=>["street_number"]}, {"long_name"=>"Lorimer Street", "short_name"=>"Lorimer St", "types"=>["route"]}, {"long_name"=>"Williamsburg", "short_name"=>"Williamsburg", "types"=>["neighborhood", "political"]}, {"long_name"=>"Brooklyn", "short_name"=>"Brooklyn", "types"=>["sublocality_level_1", "sublocality", "political"]}, {"long_name"=>"Brooklyn", "short_name"=>"Brooklyn", "types"=>["locality", "political"]}, {"long_name"=>"Kings County", "short_name"=>"Kings County", "types"=>["administrative_area_level_2", "political"]}, {"long_name"=>"New York", "short_name"=>"NY", "types"=>["administrative_area_level_1", "political"]}, {"long_name"=>"United States", "short_name"=>"US", "types"=>["country", "political"]}, {"long_name"=>"11222", "short_name"=>"11222", "types"=>["postal_code"]}], "formatted_address"=>"McCarren Park, 776 Lorimer St, Brooklyn, NY 11222, USA", "geometry"=>{"location"=>{"lat"=>40.7214499, "lng"=>-73.9520707}, "location_type"=>"APPROXIMATE", "viewport"=>{"northeast"=>{"lat"=>40.72279888029149, "lng"=>-73.95072171970848}, "southwest"=>{"lat"=>40.7201009197085, "lng"=>-73.9534196802915}}}, "place_id"=>"ChIJtzAltURZwokRSNfNakChZPU", "types"=>["park", "point_of_interest", "establishment"]}, @cache_hit=nil>] 

如何我可以從results可變的讀取latlng瓦爾?

回答

1

由於安德烈說,它是一個散列數組,你將需要選擇具有latlng變量的數組,這似乎是'postal_code'散列。搜索「幾何」鍵存在的散列。

所以,

location_results = results.data.select{|r| r['geometry']}.first 
location_results['geometry']['location']['lat'] # 40.7214499 
location_results['geometry']['location']['lng'] # 73.9520707 
+0

第一行後我有錯誤:** NoMethodError:未定義的方法'[]'爲# ** – Ponciusz

+0

啊,對,該數組是在'data'屬性。我修改了我的答案,讓我知道如果這樣做效果更好... – SteveTurczyn

1

source code

# Takes a search string (eg: "Mississippi Coast Coliseumf, Biloxi, MS", 
# "205.128.54.202") for geocoding, or coordinates (latitude, longitude) 
# for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s. 

所以這是一個簡單的數組。

結賬this documentation有關查詢的詳細信息。

+0

不工作的#未定義的方法數據<陣:0x007fc0e2679d28>' – Ponciusz

+0

@Ponciusz Checkout現在請 –

+0

'類型錯誤:字符串的隱式轉換成Integer' – Ponciusz

0
result = Geocoder.search("McCarren Park, Brooklyn, NY").first 

And then simply call it like this, 
-> result.latitude and 
-> result.longitude 

而且一定要檢查結果,有時地理編碼API的響應時間超過默認指定的超時值,並返回nil。所以如果你也面對這種情況,只需簡單地增加初始化器中的超時值即可。

Geocoder :: Configuration.timeout = 15#將15更改爲您喜歡的任何值。

更多參考退房https://github.com/alexreisner/geocoder#advanced-geocoding

0
location_results = results.data.select{|r| r['geometry']}.first 

,我現在有

["geometry", 
{"location"=>{"lat"=>40.7214499, "lng"=>-73.9520707}, 
"location_type"=>"APPROXIMATE", 
"viewport"=>{"northeast"=>{"lat"=>40.72279888029149, "lng"=>-73.95072171970848}, 
"southwest"=>{"lat"=>40.7201009197085, 
"lng"=>-73.9534196802915}}}] 

但這

location_results['geometry']['location']['lat'] 

後,我再次得到錯誤:

`NoMethodError:當我嘗試,我有這210