Getting longitude and latitude takes a very long time

梦想与她 提交于 2019-12-24 12:07:36

问题


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;
        }
    }

}

}

回答1:


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.




回答2:


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/




回答3:


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()




回答4:


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




回答5:


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




回答6:


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
                         }


来源:https://stackoverflow.com/questions/21898272/getting-longitude-and-latitude-takes-a-very-long-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!