获得经度和纬度需要很长时间

时间:2014-02-20 04:39:42

标签: java android locationmanager locationlistener

我获得了设备的经度和纬度,但这需要30秒到一分钟。有什么建议可以缩短时间吗?

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

}

}

6 个答案:

答案 0 :(得分:8)

您正在使用GPS_PROVIDER来获取GPS数据。 GPS_PROVIDER直接从卫星获取详细信息,因此第一次加载时需要时间。此外,如果您不在空旷的地方,GPS_PROVIDER需要超过30秒。当你在办公室或地下室时,GPS_PROVIDER可能会返回NULL。

另一种方法是使用NETWORK_PROVIDER。此提供商将根据您当前的网络状态为您提供GPS详细信息。这不会像GPS_PROVIDER那样准确,但效果会更快。

答案 1 :(得分:2)

嗨,你使用GPS PROVIDER可能需要一些时间,因为它取决于你的建筑位置,物理位置,天气等几个限制,因为gps数据可以从卫星获得,所以使用网络提供商可能会更快在你的情况下请查看

上给定的代码段

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

答案 2 :(得分:2)

最佳选择是使用Google Play服务及其LocationClient。

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

这为您提供了一个提供程序,可以自动从所有提供程序类型中选择最佳可用信息,并且可以使用getLastLocation()

在某些情况下立即返回该位置

答案 3 :(得分:1)

尝试在设备中运行它,而不是在模拟器中运行它。

答案 4 :(得分:1)

试试这个

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

它将为响应提供服务可用的内容。即使您可以优先考虑

答案 5 :(得分:1)

使用此代码获取更快,

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
                         }