Android GPS需要太多时间才能获得位置坐标

时间:2015-05-20 06:48:54

标签: android gps

  

此处,GPS需要半小时或更长时间(时间)才能获得当前位置坐标。

     

如何使用GPS_SATELLITES和GPS _NETWORK_PROVIDERS中的GPS   同时在相同的上下文中获取最近的值   GPS?

public class ImageFile extends Activity{

    LocationListener listner;
Location location;
LocationManager locationManager ;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; // 1 minute

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


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    listner = new MyLocationListner();
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if(isGPSEnabled==false&&isNetworkEnabled==false)
    {
        showSettingsAlert();
    }
    if (isGPSEnabled) 
    {
        progress = ProgressDialog.show(this, "GPS",
                "Fetching latitude and longitude...", true);
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,listner);   


    }
    else if (isNetworkEnabled)
    {
        progress = ProgressDialog.show(this, "GPS",
                "Fetching latitude and longitude...", true);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,listner);
    }



public class MyLocationListner implements LocationListener {

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        x = location.getLatitude();
        y = location.getLongitude();

        if((x!=0) && (y!=0))
        {
            progress.dismiss();

            locationManager.removeUpdates(listner);
            Geocoder gCoder = new Geocoder(ImageFile.this);

            try {
                addresses = gCoder.getFromLocation(x, y, 1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (addresses != null && addresses.size() > 0) {
                Log.d("TestTag", "Locality: " + addresses.get(0).getLocality()+"getAddressLine"+addresses.get(0).getAddressLine(0)+",getAdminArea"+addresses.get(0).getAdminArea()+",getSubLocality"+addresses.get(0).getSubLocality());
            }

        }
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    // Setting Dialog Title
    alertDialog.setTitle("GPS is settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    alertDialog.show();
}

}

1 个答案:

答案 0 :(得分:3)

GPS总是很慢,所以我建议使用Android Location API using Google Play Services

<强>步骤

  

首先通过调用活动的onResume()来检查Google Play服务的可用性

     

在设备上提供播放服务后,即可构建GoogleApiClient。

     

通过在onStart()方法中调用mGoogleApiClient.connect()来连接到google api客户端。通过调用此方式,将根据连接状态触发onConnectionFailed()onConnected()onConnectionSuspended()(GooglePlayServiceListeners)。

     

成功连接google api后,应在onConnected()方法中调用displayLocation()以获取当前位置。

这就是我们构建GoogleApiClient的方式

protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
    }

<强> displayLocation()

private void displayLocation() {

        mLastLocation = LocationServices.FusedLocationApi
                .getLastLocation(mGoogleApiClient);

        if (mLastLocation != null) {
            double latitude = mLastLocation.getLatitude();
            double longitude = mLastLocation.getLongitude();

            lblLocation.setText(latitude + ", " + longitude);

        } else {

            lblLocation
                    .setText("(Couldn't get the location. Make sure location is enabled on the device)");
        }
    }

可以找到上述方法和步骤的完美解释here。还要查看官方documentation