0

I have a form that users need to fill out, it is sort of time consuming on a touchscreen keyboard, so I want to "autocomplete" suggestions based on some user input such as zipcode

Instead of the user entering their Address, City, State, Zip Code

the user can instead enter Address, Zip Code and get auto filled suggestions for city and state.

They can then edit those if they are incorrect, but many times it will be correct enough, especially for state.

How would this be done in Android with the smallest app footprint. I've seen that google has some kind of geocode API but I'm not sure if it is relevant to android

Any insight appreciated

Matt
  • 22,721
  • 17
  • 71
  • 112
CQM
  • 42,592
  • 75
  • 224
  • 366

2 Answers2

1

For our web app, we use zipcodeapi/com/examples:

<script type="text/javascript">//<![CDATA[
$(function() {
    // IMPORTANT: Fill in your client key
    var clientKey = "js-9qZHzu2Flc59Eq5rx10JdKERovBlJp3TQ3ApyC4TOa3tA8U7aVRnFwf41RpLgtE7";

    var cache = {};
    var container = $("#example1");
    var errorDiv = container.find("div.text-error");

    /** Handle successful response */
    function handleResp(data)
    {
        // Check for error
        if (data.error_msg)
            errorDiv.text(data.error_msg);
        else if ("city" in data)
        {
            // Set city and state
            container.find("input[name='city']").val(data.city);
            container.find("input[name='state']").val(data.state);
        }
    }

    // Set up event handlers
    container.find("input[name='zipcode']").on("keyup change", function() {
        // Get zip code
        var zipcode = $(this).val().substring(0, 5);
        if (zipcode.length == 5 && /^[0-9]+$/.test(zipcode))
        {
            // Clear error
            errorDiv.empty();

            // Check cache
            if (zipcode in cache)
            {
                handleResp(cache[zipcode]);
            }
            else
            {
                // Build url
                var url = "https://www.zipcodeapi.com/rest/"+clientKey+"/info.json/" + zipcode + "/radians";

                // Make AJAX request
                $.ajax({
                    "url": url,
                    "dataType": "json"
                }).done(function(data) {
                    handleResp(data);

                    // Store in cache
                    cache[zipcode] = data;
                }).fail(function(data) {
                    if (data.responseText && (json = $.parseJSON(data.responseText)))
                    {
                        // Store in cache
                        cache[zipcode] = json;

                        // Check for error
                        if (json.error_msg)
                            errorDiv.text(json.error_msg);
                    }
                    else
                        errorDiv.text('Request failed.');
                });
            }
        }
    }).trigger("change");
});

//]]> Zip Code Distance

Bob Bickel
  • 349
  • 2
  • 3
1

For the smallest app footprint, if you make no assumptions about where the user is you'll need to use some sort of web service to get this. A cursory googling reveals this as an example.

If for some reason you only expect users from a small geographic area (like just around a single city) to be using your app, a baked in database/lookup file would be feasible, but not for covering the whole county.

maxpower47
  • 1,666
  • 1
  • 10
  • 14