使用Android应用中的邮政编码搜索新加坡地址

时间:2014-01-15 13:21:24

标签: android google-maps

我想在 Android应用中使用邮政编码搜索新加坡地址,就好像我将邮政编码放在edittext字段 436914 中然后按按钮,它将返回地址 “121 Tanjong Rhu Road,Singapore 436914”

我正在使用android地理编码类,但它提供的地址并不完全符合我的要求,它给了我  仅限新加坡436914 以及以下链接 http://maps.googleapis.com/maps/api/geocode/json?address=436914&sensor=true

3 个答案:

答案 0 :(得分:1)

现在我找到了两种使用邮政编码获取地址的替代方法,http://maps.googleapis.com/maps/api/geocode/json?address=436914&sensor=true链接给出纬度和经度调用相同的url但现在代替地址参数传递latlong参数,并且它在两个进程中的值你将得到确切的地址。

第二种选择是

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);
        EditText street = (EditText)findViewById(R.id.editText3);
        EditText pcode = (EditText)findViewById(R.id.editText4);
        EditText blk = (EditText)findViewById(R.id.editText13);
        EditText build = (EditText)findViewById(R.id.buildingtext);

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

}

答案 1 :(得分:0)

对于邮政编码,我发现onemap.sg中的数据非常准确。但是对于某些邮政编码,会有不止一个结果。这是因为在同一建筑物内可能存在多个相同邮政编码的列表。尽管如此,它仍然是准确的。

为简单起见,我建议您查看有关api使用情况的github repo

要查看的关键字段是Address,Building,SearchVal。

答案 2 :(得分:0)

这是另一种方法。 Geocode.xyz/SG

{
"standard": {
"addresst": "121 TANJONG RHU ROAD",
"stnumber": "121",
"prov": "SG",
"city": "Singapore",
"countryname": "Singapore",
"postal": "436914",
"confidence": "0.5"
},
"longt": "103.87430",
"alt": {
"loc": {
  "longt": "103.87430",
  "city": "Singapore",
  "cc": "2",
  "latt": "1.29662"
}
},
"latt": "1.29662"
}