我正在使用 Rails 对邮政编码进行地理编码。作为测试,我使用了邮政编码 60601 上的代码:
Geocoder.search(60601).first.latitude
但是,这会导致
9.42233953846154
,这是不正确的。正确的值应该是 41.something。所以还差得远呢
有没有更好的方法来获得准确的结果?
你应该尝试一下,
Geocoder.search(60601)[1].latitude
因为这个特定的“60601”邮政编码在地球上有多个位置,所以你需要从这里的哈希中找到一个特定的地址,当你搜索 60601 时,你可以检查那里总共有 3 个位置。
Geocoder.search(60601)
=>
[#<Geocoder::Result::Nominatim:0x00007faaf0440ac8
@cache_hit=nil,
@data=
{"place_id"=>328970067,
"licence"=>"Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"lat"=>"9.42233953846154",
"lon"=>"-84.15445643076923",
"class"=>"place",
"place_rank"=>21,
"importance"=>0.22500009999999993,
"addresstype"=>"postcode",
"name"=>"60601",
"display_name"=>"Quepos, Cantón Quepos, 60601, Puntarenas Province, Costa Rica",
"address"=>{"town"=>"Quepos", "county"=>"Cantón Quepos", "postcode"=>"60601", "province"=>"Puntarenas Province", "ISO3166-2-lvl4"=>"CR-P", "country"=>"Costa Rica", "country_code"=>"cr"},
"boundingbox"=>["9.2623395", "9.5823395", "-84.3144564", "-83.9944564"]}>,
#<Geocoder::Result::Nominatim:0x00007faaf04407d0
@cache_hit=nil,
@data=
{"place_id"=>331500121,
"licence"=>"Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"lat"=>"41.838471146546546",
"lon"=>"-87.80685671291292",
"class"=>"place",
"type"=>"postcode",
"place_rank"=>21,
"addresstype"=>"postcode",
"name"=>"60601",
"display_name"=>"Riverside, Riverside Township, Cook County, 60601, Illinois, United States",
"address"=>
{"village"=>"Riverside",
"municipality"=>"Riverside Township",
"county"=>"Cook County",
"postcode"=>"60601",
"state"=>"Illinois",
"ISO3166-2-lvl4"=>"US-IL",
"country"=>"United States",
"country_code"=>"us"},
"boundingbox"=>["41.6784711", "41.9984711", "-87.9668567", "-87.6468567"]}>,
#<Geocoder::Result::Nominatim:0x00007faaf04404d8
@cache_hit=nil,
{"place_id"=>331343428,
"licence"=>"Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"lat"=>"52.41794258392857",
"lon"=>"16.910198301785712",
"class"=>"place",
"type"=>"postcode",
"place_rank"=>25,
"importance"=>0.12500009999999995,
"addresstype"=>"postcode",
"name"=>"60-601",
"display_name"=>"Sołacz, Poznan, 60-601, Greater Poland Voivodeship, Poland",
"address"=>{"suburb"=>"Sołacz", "city"=>"Poznan", "postcode"=>"60-601", "state"=>"Greater Poland Voivodeship", "ISO3166-2-lvl4"=>"PL-30", "country"=>"Poland", "country_code"=>"pl"},
"boundingbox"=>["52.2579426", "52.5779426", "16.7501983", "17.0701983"]}>]