Abdennacer Lachiheb
Abdennacer Lachiheb

Reputation: 4888

Google play service is slow finding my location

I added google play api to my android application to get my current location because i need in my project.

After adding the necessary code to get my current location, it work fine but it is slow, so after clicking the button to get my location it respond after 20-25 seconds .

Is there is a way to make it faster ?

Here is my code :

   ....
   LocationManager myManager;
    MyLocListener loc;
    loc = new MyLocListener();
    myManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
}

public class MyLocListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location)
    {
        if(location != null)
        {
            // it logs after 20-25 s
            Log.d("Latitude :", "" + location.getLatitude());
            Log.d("Latitude :", "" + location.getLongitude());
        }
    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

}

Upvotes: 1

Views: 1377

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200030

Using the GPS_PROVIDER requires a strict GPS fix, which generally does take 10-30 seconds depending on the hardware.

You should consider using the FusedLocationProviderApi as explained on the Receiving location updates training page, which combines the location information from many sources such as Wifi, Bluetooth, and GPS and provides a much faster location fix in general, even for high accuracy requests.

Upvotes: 4

Related Questions