Follow the steps mentioned below to get your location latitude and longitude
1) Create a class and name it LocationManger.java and impliment GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener in the class
LocationManger.class
public class LocationManger implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private final String TAG = "MyAwesomeApp";
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private static Context context;
private LocationManger() {
}
private static LocationManger manager;
public static LocationManger getInstance(Context _context) {
if (manager == null) {
manager = new LocationManger();
}
context = _context;
return manager;
}
public void setUpLocation() {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
if (servicesConnected()) {
startPeriodicUpdates();
}
}
private void startPeriodicUpdates() {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(10000); // Update location every second
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
/**
* In response to a request to stop updates, send a request to
* Location Services
*/
private void stopPeriodicUpdates() {
if (mGoogleApiClient.isConnected())
System.out.println("destroy2");
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
public void stopLocationUpdate() {
stopPeriodicUpdates();
}
private boolean servicesConnected() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
// Continue
return true;
// Google Play services was not available for some reason
} else {
// Display an error dialog
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, (Activity) context, 0);
if (dialog != null) {
}
return false;
}
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "GoogleApiClient connection has been suspend");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "GoogleApiClient connection has failed");
((LocationFound) context).locationFailed();
}
@Override
public void onLocationChanged(Location location) {
((LocationFound) context).locationFound(location);
stopLocationUpdate(); //stop location update. comment this line if you wish to get updated location latitude and longitude
}
public interface LocationFound {
public void locationFound(Location location);
public void locationFailed();
}
}
2) Now in your manin activity impliment LocationManger.LocationFound, this will help you to override locationFound and locationFaild method and call setUpLocation through LocationManger object.
MainActivity.class
public class MainActivity extends Activity implements LocationManger.LocationFound {
private Location mLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManger locationManger=LocationManger.getInstance(this);
locationManger.setUpLocation();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void locationFound(Location location) {
//this.mLocation=location;
Float speed=location.getSpeed();
double latitude=location.getLatitude();
double longitude=location.getLongitude();
Toast.makeText(MainActivity.this, "latitude" + latitude + "longitude" + longitude, Toast.LENGTH_LONG).show(); //show your latitude and longitude on toast
}
@Override
public void locationFailed() {
}
}
0 Comment(s)