Domanda

Are there any gems/ways that would allow me to retrieve a city, state, and postal code from a street address? For example:

results = Search.find("100 Rue de la Paix")
results.each do |address|
  address.city        => "Timbuktu"
  address.state       => "Timbuktu Region"
  address.country     => "Mali"
  address.post_code   => "123456"
end

Preferably something that uses Google Maps.

È stato utile?

Soluzione

The ruby geocoder gem does exactly that:

require 'geocoder'
Geocoder.search('100 Rue de la Paix')
=> # Array of results

This searches for the address and displays results.

Altri suggerimenti

You could try SmartyStreets' autocomplete API. I work at SmartyStreets and helped to write it. You can try it on the homepage. I see you have addresses outside the USA, and SmartyStreets autocomplete is currently US-only, but maybe it'll still be helpful. However, SmartyStreets does offer other services such as verification for international addresses.

Technically it accepts a "prefix" (can be just a street address) and will suggest up to 10 city/state combinations. From there, you would proceed to verify the validity of the address using LiveAddress API to fill out the ZIP code and such.

None of the other answers yet given actually do this, and all of them will actually return invalid results, so always make sure to verify it before you're done. Just note that only a handful of countries actually have data to validate a street address at the delivery point level (like a house, or business), some do street, most do at least city/region granularity.

Try OpenStreetMap:

Requests:

Results:

 <searchresults timestamp="Sat, 07 Nov 09 14:42:10 +0000" querystring="135 pilkington, avenue birmingham" polygon="true">
   <place 
     place_id="1620612" osm_type="node" osm_id="452010817" 
     boundingbox="52.548641204834,52.5488433837891,-1.81612110137939,-1.81592094898224" 
     polygonpoints="[['-1.81592098644987','52.5487429714954'],['-1.81592290792183','52.5487234624632'],...]" 
     lat="52.5487429714954" lon="-1.81602098644987" 
     display_name="135, Pilkington Avenue, Wylde Green, City of Birmingham, West Midlands (county), B72, United Kingdom" 
     class="place" type="house">
     <house>135</house>
     <road>Pilkington Avenue</road>
     <village>Wylde Green</village>
     <town>Sutton Coldfield</town>
     <city>City of Birmingham</city>
     <county>West Midlands (county)</county>
     <postcode>B72</postcode>
     <country>United Kingdom</country>
     <country_code>gb</country_code>
   </place>
 </searchresults>

If you can use javascript:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
    function codeAddress(address) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {

                city = results[0].address_components[2].long_name;
                state = results[0].address_components[4].long_name;
                country = results[0].address_components[5].long_name;
                postalCode = results[0].address_components[6].long_name;

                // Example...
                alert(city);

            }
        });
    }

    var address = "100 Rue de la Paix";
    codeAddress(address);
</script>

If you need any more information about the location, you can refer to this document: https://developers.google.com/maps/documentation/geocoding/#GeocodingResponses

Also, if you're using the Google Maps API, you must display a map (Google) somewhere on the page according to their terms of service.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top