Thursday, 6 November 2014

Retrieve current location by gps as well as interent

hey follows few steps to get proper and accurate current location. This way provided you current location both by interent as well as gps.
Step 1.

Put this class in your code.

 GetCurLocation.java

public class GetCurLocation implements LocationListener,
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener {

public static LocationClient mLocationClient;
LocationRequest mLocationRequest;
public static LocationManager locationmanager;
float accuracy = 500;
Activity context;
boolean getLocRegularly = false;
int interval = 1000;
float Radius;
GoogleMap gmap;

SetOnLocationFoundListner OLF;

public interface SetOnLocationFoundListner {
    public void onLocationFound(Location location, boolean getLocRegularly,
            GoogleMap gmap);
}

public void RemoveUpdates() {
    try {
        if (mLocationClient != null)
            mLocationClient.removeLocationUpdates(this);
        if (locationmanager != null)
            locationmanager.removeUpdates(LocUpByLocMgr);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

/*
 * radius should be in meters
 */
public GetCurLocation(Activity activity, int interval,
        boolean getLocRegularly, GoogleMap gmap,
        SetOnLocationFoundListner OLF, float Radius) {
    this.OLF = OLF;
    this.gmap = gmap;
    this.context = activity;
    this.getLocRegularly = getLocRegularly;
    this.interval = interval;
    this.Radius = Radius;
    if (servicesConnected()) {
        mLocationClient = new LocationClient(context, this, this);
        mLocationClient.connect();
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(interval);
        mLocationRequest
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setFastestInterval(interval);
    }

    locationmanager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    Criteria cr = new Criteria();
    String provider = locationmanager.getBestProvider(cr, true);
    locationmanager.requestLocationUpdates(provider, interval, 0,
            LocUpByLocMgr);
}

private boolean servicesConnected() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(context);
    if (ConnectionResult.SUCCESS == resultCode) {
        return true;
    } else {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode,
                (Activity) context, 0);
        if (dialog != null) {
        }
        return false;
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {

}

@Override
public void onConnected(Bundle connectionHint) {
    try {
        Location location = mLocationClient.getLastLocation();
        Log.e("testing",
                location.getLatitude() + "," + location.getLongitude()
                        + "," + location.getAccuracy());
        if (location.getAccuracy() < Radius) {
            OLF.onLocationFound(location, getLocRegularly, gmap);
            locationmanager.removeUpdates(LocUpByLocMgr);
        } else
            mLocationClient.requestLocationUpdates(mLocationRequest, this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onDisconnected() {
}

@Override
public void onLocationChanged(Location location) {
    try {
        if (location.getAccuracy() > Radius) {
            Log.e("testing LC", location.getAccuracy()
                    + " Its Not Accurate");
        } else {
            Log.e("testing LC", location.getAccuracy() + " Its Accurate");
            try {
                OLF.onLocationFound(location, getLocRegularly, gmap);
                if (!getLocRegularly) {
                    mLocationClient.removeLocationUpdates(this);
                    locationmanager.removeUpdates(LocUpByLocMgr);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

android.location.LocationListener LocUpByLocMgr = new android.location.LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {

        try {
            if (location.getAccuracy() > Radius) {
                Log.e("testing LM", location.getAccuracy()
                        + " Its Not Accurate");
            } else {
                Log.e("testing LM", location.getAccuracy()
                        + " Its Accurate");

                try {
                    OLF.onLocationFound(location, getLocRegularly, gmap);
                    if (!getLocRegularly) {
                        mLocationClient
                                .removeLocationUpdates(GetCurLocation.this);
                        locationmanager.removeUpdates(LocUpByLocMgr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

}

Step 2.
implement setonlocationfoundlistener in class where you want current location,
and declear one more method in your oncreate and after you will get one location found method and it return location and you can get location.getlatitude and location.getlongitude.

GetCurLocation gcl = new GetCurLocation(activity, 0, true, null, this,
                2000); 

Step 3. make permission

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

thats all. thanks