0

I want find current user location on android.

I use these code:

mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(1000); // Update location every second
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

            mMap.setMyLocationEnabled( true );
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                    mGoogleApiClient);
            if (mLastLocation != null) {
                lat = String.valueOf(mLastLocation.getLatitude());
                lon = String.valueOf(mLastLocation.getLongitude());

            }
            firstLoction();
        }
        else
        {
            Toast.makeText(this, "Not promisseion", Toast.LENGTH_SHORT).show();
        }

It works with some network (Data mobile). but not works for all location. (Also It works with GPS)

Can I do more thing to handle networks better?

partiz
  • 1,206
  • 2
  • 13
  • 34
  • You should not use getLastLocation. After you build LocationRequest you should request updates based on this request and implement LocationListener interface for receiving them. – andrea.petreri Jan 23 '16 at 09:22

3 Answers3

1
Context ctx = this;
        gpsLocationListener = new OLL(ctx);
        networkLocationListener = new OLL(ctx);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location temploc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (useGPS) {
          if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                     5000, 1, gpsLocationListener);
               }
        }
        if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
         lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
            5000, 1, networkLocationListener);
       }

public class OLL implements LocationListener {
Context ctx;
private final static String TAG = "TAG"; 
    public OVoidLL(Context ctx) {

        this.ctx = ctx;
    }
@Override
    public void onLocationChanged(Location location) {
Log.d(TAG, location.toString);

    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
1

Make your activity implementing LocationListener interface and then exploit FusedLocationApi, that take care of all available providers (e.g. GPS, Network).

In your onConnected method you need to proceed as follows:

@Override
public void onConnected(Bundle connectionHint) {
    LocationRequest request = new LocationRequest();
    request.setInterval(10000);
    request.setFastestInterval(5000);
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    // using fused location api, taking care of all
    // available providers (e.g. GPS, NETWORK)
    // here do not forget to check for available LOCATION
    // permission before asking for updates
    LocationServices.FusedLocationApi.requestLocationUpdates(
        mGoogleApiClient, request, this);
}

Since your Activity implements LocationListener interface, you will receive updates in onLocationChanged method.

@Override
public void onLocationChanged(Location location) {
    // this method is defined by LocationListener interface
    // here you will receive location updates based
    // on fused location api        
}

Refer here for additional details on how to improve code for reducing battery drain.

I see you are using GoogleMap in your activity. It is possible also to get current location directly from GoogleMap object by using its method setOnMyLocationChangeListener. Unfortunately this method is now deprecated, so the recommendation is to use FusedLocationProviderApi.

andrea.petreri
  • 4,137
  • 2
  • 22
  • 21
1

see this link by How to get and use location data in your Android app

M.Ganji
  • 818
  • 8
  • 13