Getting The Current Location from the Android device Also getting the address from that location

hey guys in this tutorial i will show u guys how to get the location from your android device and find the address of the location from that device.

please add these permissions

   
    
    
    
    
    
 
public class LocationFinder implements LocationListener {


    Context context;

    Double lat, lon;

    Location location;

    public LocationFinder(Context c) {
        this.context = c;
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 1000 * 60 * 1, this);


        try {
            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            setLat(location.getLatitude());
            setLon(location.getLongitude());
        }catch (Exception e){

            Toast.makeText(c,"couldn't locate your location",Toast.LENGTH_LONG).show();
            setLat(21.0000);
            setLon(78.0000);
        }


    }

    public String getAdress() {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        String result = null;
        try {
            List
addressList = geocoder.getFromLocation( lat, lon, 1); if (addressList != null && addressList.size() > 0) { Address address = addressList.get(0); StringBuilder sb = new StringBuilder(); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { sb.append(address.getAddressLine(i)).append("\n"); } sb.append(address.getLocality()).append("\n"); sb.append(address.getPostalCode()).append("\n"); sb.append(address.getCountryName()); result = sb.toString(); Log.d("Adress", result); } } catch (IOException e) { Log.e("Errorrr", "Unable connect to Geocoder", e); } return result + ""; } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } public void setLat(Double l) { lat = l; } public void setLon(Double l) { lon = l; } public Double getLat() { return lat; } public Double getLon() { return lon; } }
guys if there is any clariffication dont forget to comment

Comments

Popular posts from this blog

Adding Markers in Google Map and Focusing Camera when map loaded

Creating Simple Horizontal graph in android using android Weight property without any External Libraries