ZipCode from location

后端 未结 3 554
野趣味
野趣味 2020-12-06 08:13

Is there any way to get zip-code of the current user location from location or lat or long. If so how?

3条回答
  •  臣服心动
    2020-12-06 08:27

    Geocoder based implementation:

    private String getZipCodeFromLocation(Location location) {
        Address addr = getAddressFromLocation(location);
        return addr.getPostalCode() == null ? "" : addr.getPostalCode();
    }
    
    private Address getAddressFromLocation(Location location) {
        Geocoder geocoder = new Geocoder(this);
        Address address = new Address(Locale.getDefault());
        try {
            List
    addr = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (addr.size() > 0) { address = addr.get(0); } } catch (IOException e) { e.printStackTrace(); } return address; }

    You can get also other information from address, for example city name.

提交回复
热议问题