Question

I am getting the longitude and latitude of my device, but it takes at 30 seconds to a minute to do so. Any suggestions to cut the time down?

public class MainActivity extends Activity
{
public String zipcode;
public double latG;
public double lonG;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!enabled) 
    {
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);
    }

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.getLastKnownLocation(Context.LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener()
    {

         public void onLocationChanged(Location location) 
         {
                if (location != null) 
                {
                    latG = location.getLatitude();
                    lonG = location.getLongitude();
                    Toast.makeText(MainActivity.this,
                            latG + " " + lonG,
                            Toast.LENGTH_LONG).show();
                }
            }
            public void onProviderDisabled(String provider) 
            {

            }

            public void onProviderEnabled(String provider) 
            {

            }

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

            }



    };
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);



    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = null;
    try 
    {
        addresses = geocoder.getFromLocation(latG, lonG, 1);
    } 
    catch (IOException e) 
    {
        Context context = this;
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle("Error");
        alertDialogBuilder.setMessage("Error in getting address information.");
        alertDialogBuilder.setCancelable(true);

    }

    for (Address address : addresses) 
    {
        if(address.getPostalCode() != null)
        {
            zipcode = address.getPostalCode();
            Toast.makeText(MainActivity.this, zipcode, Toast.LENGTH_LONG).show();
            break;
        }
    }

}

}
Was it helpful?

Solution

You are using GPS_PROVIDER for fetching the GPS data. GPS_PROVIDER fetches details directly from the satellite so it takes time for the first time you load this. Moreover GPS_PROVIDER takes more than 30 seconds if your are not below the open sky. GPS_PROVIDER may return NULL when you are inside the office or in basement.

There is an alternative way for this is to use NETWORK_PROVIDER. This provider will give you GPS details based on your current Network state. This will not be much accurate like GPS_PROVIDER but it works faster.

OTHER TIPS

hi you are using GPS PROVIDER which can take some time as it depends on several constraints like yours building position, physical position, weather as gps data is available from the satellite so use a network provider which may faster in yours case please have a look on the given code snippet at

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

Best option is to use Google Play Services and its LocationClient.

http://developer.android.com/google/play-services/location.html

This gives you a provider that automatically picks the best available information from all the provider types and can return the location immediately in some cases using getLastLocation()

try to run it in device rather than running it in the emulator.

try this

 try {
        gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }
    try {
        network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {}


if (gps_enabled) {
            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
        }
        if (network_enabled) {
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);

It will give the response what ever the service is available.Even you can place your priority

Use this code to fetch faster,

String provider;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        if (provider != null && !provider.equals("")) {
            Location location = locationManager.getLastKnownLocation(provider);

            locationManager.requestLocationUpdates(provider, 20000, 1, this);

            if (location != null) 
                         {
                onLocationChanged(location);
                              //your remaining code
                         }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top