The Google Location Services API, part of Google Play services, is the preferred way to add location-awareness to an android app.
Follow the below steps to get current position of an android device.
1. To use the location manager make the Google play service available via your app level build.gradle file.
compile 'com.google.android.gms:play-services-location:10.0.1'
2. Add required permission in your AndroidManifest.xml file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
3. Now, create a LocationManager.java class.
public class LocationManager implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private final String TAG = "LocationManager";
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private static Context context;
private LocationManager() {
}
private static LocationManager manager;
public static LocationManager getInstance(Context _context) {
if (manager == null) {
manager = new LocationManager();
}
context = _context;
return manager;
}
public void setUpLocation() {
if(servicesConnected()){
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();}
}
@Override
public void onConnected(Bundle bundle) {
startPeriodicUpdates();
}
private void startPeriodicUpdates() {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(10000); // Update location every second
try {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}catch (SecurityException e){
e.printStackTrace();
}
}
/**
* In response to a request to stop updates, send a request to
* Location Services
*/
private void stopPeriodicUpdates() {
if (mGoogleApiClient.isConnected())
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
public void stopLocationUpdate() {
stopPeriodicUpdates();
}
public boolean servicesConnected() {
// Check that Google Play services is available
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int resultCode = googleAPI.isGooglePlayServicesAvailable(context);
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 = googleAPI.getErrorDialog((Activity) context,resultCode, 0);
if (dialog != null) {
dialog.show();
}
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();
}
public interface LocationFound {
public void locationFound(Location location);
public void locationFailed();
}
}
4. Now in our activity class create an instance of LocationManager class and call setUpLocation().
package com.realmdemo.activity;
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.realmdemo.R;
import com.realmdemo.utility.LocationManager;
public class LocationActivity extends AppCompatActivity implements LocationManager.LocationFound {
private final String TAG = "LocationActivity";
LocationManager locationManger=null;
Location myLocation=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
locationManger=LocationManager.getInstance(this);
locationManger.setUpLocation();
}
public void locationFound(Location location) {
// TODO Auto-generated method stub
this.myLocation=location;
Log.i(TAG, myLocation.getLatitude() + "----" + myLocation.getLongitude());
}
@Override
public void locationFailed() {
}
}
Thanks you.
0 Comment(s)