I am using rails to geocode zip codes. As a test I used the code on zip code 60601 using:
Geocoder.search(60601).first.latitude
However, this results in 9.42233953846154
, which is incorrect. The correct value should be 41.something. So it's way off.
Is there a better way to get an accurate result?
You should try,
Geocoder.search(60601)[1].latitude
because this specific '60601' zipcode has multiple locations on Earth so u need to find a specific address from the hash here, when you search 60601 then you can check that there is a total of 3 location there.
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"]}>]