2017-08-03 78 views
1

我正在尋找解決方案來更改郵政編碼,例如DD3 7BN,但不使用地理位置。我找到了一個使用url http://maps.googleapis.com/maps/api/geocode/json?address=DD37bn的解決方案。 一個人提供了另一種解決方案,但它不適合我。如何使用地圖API從郵編中獲取街道名稱?

但它顯示錯誤GRPS失敗。我的問題最好的解決方案是什麼?

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     getAddressByPostalCode(""); 
    } 


    public void getAddressByPostalCode(String val_pcode) { 

     Geocoder geocoder = new Geocoder(this.getApplicationContext(), Locale.getDefault()); 

     try { 

      List<Address> addresses1 = geocoder.getFromLocationName (val_pcode, 1); 

      Address obj1 = addresses1.get(0); 

      List<Address> addresses = geocoder.getFromLocation(obj1.getLatitude(), obj1.getLongitude(), 1); 
      Address obj = addresses.get(0); 
      /*TextView street = (TextView)findViewById(R.id.editText1); 
      TextView pcode = (TextView)findViewById(R.id.editText2); 
      TextView blk = (TextView)findViewById(R.id.editText3); 
      TextView build = (TextView)findViewById(R.id.editText4); 

      street.setText(obj.getThoroughfare()); 
      pcode.setText(obj.getPostalCode()); 
      blk.setText(obj.getSubThoroughfare()); 
      build.setText(obj.getPremises());*/ 


     } catch (IOException e) { 
      Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

回答

0

要知道,英國的郵政編碼don't always relate to single street, HD7 5UZ佔地面積7,因此谷歌API不返回「路線」元素 - http://maps.googleapis.com/maps/api/geocode/json?address=HD75UZ

我不認爲有任何打開的數據集,提供郵編和街道之間的鏈接,但有一些完整的清單streetspostcodes以及其他地理數據雖然。

爲英國另一種解決方案是使用着眼於英國皇家郵政PAF數據集的API,這將有郵政編碼以及它們與街道之間的聯繫。這將帶來一定的成本,但通常情況下,如果您只是在街道名稱之後,而不是前提級別的數據,則成本相當低。

一個例子是來自Allies Computing(我工作的)"street" endpoint,它將返回一組街道。您可以讓用戶選擇正確的一個,或者只有一個,自動爲他們選擇。

大多數其他PAF經銷商也提供街道搜索,他們可以通過Powered by PAF website找到。

+0

我需要這個只用於一個城市。我知道可以有多個結果或null。但我尋找解決方案如何使用例如我發現這個鏈接街道,因爲後來我可以檢查是否這是我期望與否的記錄。 –

+0

如果使用google,你會在「address_components」數組中尋找包含「types」數組的「route」對象。 –

+0

你可以給任何exaples如何在代碼中使用谷歌或我可以找到更多的信息? –

相關問題