0

What is the most efficient way to get the user's lat lng location in Android?

By efficient I mean fast and not too intrusive on permissions (also in wake of the recent permission changes on Android 6.0).

The second part of the question is what other ways exist of getting the user location other than Google maps api?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104

2 Answers2

0

Use the LocationManager.

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

  private final LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
    longitude = location.getLongitude();
    latitude = location.getLatitude();
}
}

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

You'll need to give your application the ACCESS_FINE_LOCATION permission if you want to use GPS.

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

Hope it helps.

Kristo
  • 1,339
  • 12
  • 22
0

Faster way, using the Google Api, not Android Api:

First, make your class extend GoogleApiClient.ConnectionCallbacks and LocationListener interfaces, and declare a GoogleApiClient variable.

private GoogleApiClient gApiClient;

Second, call this method from onCreate or similar:

protected synchronized void buildGoogleApiClient() {
    gApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {

                }
            })
            .addApi(LocationServices.API)
            .build();

    gApiClient.connect();

    createLocationRequest();
}

protected void createLocationRequest() {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

Finally override this method:

@Override
public void onLocationChanged(Location location) {
    tempLatitude=location.getLatitude();
    tempLongitude=location.getLongitude();
}

---EDIT---

I just realized I was missing a method, this one, called automatically after you connect the fApiClient:

@Override
public void onConnected(Bundle bundle) {
    Location lkn= LocationServices.FusedLocationApi.getLastLocation(
            gApiClient);

    if(lkn!=null){

        tempLatitud=lkn.getLatitude();
        tempLongitud=lkn.getLongitude();
    }

    LocationServices.FusedLocationApi.requestLocationUpdates(
            gApiClient, mLocationRequest, this);
}

And, if you are using a map, you will need to extend another two interfaces: OnMapReadyCallback, and GoogleMap.OnMapClickListener, this last in case you need to react to the user tapping the map. The first one only need to implement method onMapReady(GoogleMap map). Inside that method, you can do

this.map=map;

(this.map refers to a GoogleMap variable declared by you, in your class).

And after that, your map will be ready to be used.

You will have your updates of the location of the device in onLocationChanged method for you to use them.

Answering your second question, you can get your latitude and longitude by triangulation of cell towers. Look here

Community
  • 1
  • 1
Fustigador
  • 6,339
  • 12
  • 59
  • 115
  • Thank you very much. I have a similar implementation, however i am also showing a map (not really needed in app) which is making it a bit more complicated, I will refactor and clean it up and will keep your solution in mind. – Zaki Abbas Feb 02 '16 at 18:08