2011-06-28 61 views
0

我有一個用戶需要填寫的表單,這是一種耗時的觸摸屏鍵盤,所以我想基於一些用戶輸入「自動完成」的建議,如郵編Android自動檢測城市,州基於郵政編碼

取而代之進入他們的地址,城市,州,郵編

用戶可以改爲輸入地址的用戶,郵編,並獲得城市和國家的自動填充建議。

然後,他們可以編輯這些如果他們不正確,但很多時候它會是正確的,特別是對於國家。

這將如何在Android中以最小的應用程序足跡完成。我已經看到了谷歌有某種地理編碼API的,但我不知道這是否是與Android相關

任何瞭解讚賞

回答

1

對於最小的應用程序佔用,如果你不作任何假設,哪裏的用戶是否需要使用某種Web服務才能獲得此功能?粗略的搜索以this爲例。

如果由於某種原因,您只希望來自一個小地理區域(比如在一個城市附近)的用戶使用您的應用程序,則數據庫/查找文件中的烘焙文件可行,但不適用於覆蓋整個縣。

+0

我喜歡它,但它的速度太慢。如果可能,我需要使用Google的服務 – CQM

+1

http://stackoverflow.com/questions/4749706/lookup-city-and-state-by-zip-google-geocode-api – maxpower47

1

對於我們的Web應用程序,我們使用zipcodeapi/COM /例子:

<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"); 
}); 

//]]> 郵編距離

相關問題