Reverse Geo coding in android java

Activity Forums Android mobile app Reverse Geo coding in android java

  • This topic is empty.
  • Creator
    Topic
  • #131
    adcocitclass
    Keymaster

    Android Code to get address from location. Following code reverse geo code from current location to get latitude and longitude and then from latitude, longitude it gets address

    public static void getAddressFromLocation(
    final Location location, final Context context, final Handler handler) {
    Thread thread = new Thread() {
    @Override public void run() {
    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    String result = null;
    try {
    List

    list = geocoder.getFromLocation(
    location.getLatitude(), location.getLongitude(), 1);
    if (list != null && list.size() > 0) {
    Address address = list.get(0);
    // sending back first address line and locality
    result = address.getAddressLine(0) + ", " + address.getLocality();
    }
    } catch (IOException e) {
    Log.e(TAG, "Impossible to connect to Geocoder", e);
    } finally {
    Message msg = Message.obtain();
    msg.setTarget(handler);
    if (result != null) {
    msg.what = 1;
    Bundle bundle = new Bundle();
    bundle.putString("address", result);
    msg.setData(bundle);
    } else
    msg.what = 0;
    msg.sendToTarget();
    }
    }
    };
    thread.start();
    }

    Call to above getaddresslocation routine

    getAddressFromLocation(mLastKownLocation, this, new GeocoderHandler());

    Following routine is to handle errors for above geocode routine

    @Override
    public void handleMessage(Message message) {
    String result;
    switch (message.what) {
    case 1:
    Bundle bundle = message.getData();
    result = bundle.getString("address");
    break;
    default:
    result = null;
    }
    // replace by what you need to do
    myLabel.setText(result);
    }
    }

  • You must be logged in to reply to this topic.