Android 根据邮政编码自动检测城市、州

发布于 2024-11-17 11:27:30 字数 273 浏览 7 评论 0原文

我有一个用户需要填写的表格,在触摸屏键盘上有点耗时,所以我想根据一些用户输入(例如邮政编码)“自动完成”建议,

而不是用户输入他们的地址,城市,州,邮政编码

用户可以输入地址、邮政编码并获取城市和州的自动填充建议。

如果它们不正确,他们可以对其进行编辑,但很多时候它足够正确,尤其是对于州而言。

在 Android 中如何以最小的应用程序占用空间来完成此操作?我已经看到谷歌有某种地理编码API,但我不确定它是否与android相关

任何见解都值得赞赏

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪歌 2024-11-24 11:27:30

对于最小的应用程序占用空间,如果您不对用户所在位置做出任何假设,则需要使用某种 Web 服务来获取此信息。粗略的谷歌搜索显示这个作为示例。

如果出于某种原因,您只希望来自较小地理区域(例如在一个城市周围)的用户使用您的应用程序,则内置数据库/查找文件是可行的,但不适用于覆盖整个县。

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.

落叶缤纷 2024-11-24 11:27:30

对于我们的网络应用程序,我们使用 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");
});

//]]>
邮政编码距离

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文