2017-08-03 80 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如何在代码中使用谷歌或我可以找到更多的信息? –

相关问题