Android GPS需要太多时间来获取位置坐标

标签 android gps

Here, GPS takes half an hour or more (time) to get the current location coordinates.

How can I use GPS from GPS_SATELLITES and GPS _NETWORK_PROVIDERS simultaneously in the same context and get the value of the recent 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();
}

最佳答案

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

步骤

First check for availability of Google Play Services by calling in onResume() of the Activity

Once play services are available on the device, build the GoogleApiClient.

Connect to google api client by calling mGoogleApiClient.connect() in onStart() method. By calling this, onConnectionFailed(), onConnected() and onConnectionSuspended() (GooglePlayServiceListeners) will be triggered depending upon the connection status.

Once google api is successfully connected, displayLocation() should be called in onConnected() method to get the current location.

这就是我们构建 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

关于Android GPS需要太多时间来获取位置坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30342237/

相关文章:

Android GPS list 不起作用

android - 从单独的类获取 GPS 经度和纬度 - Android

android - 删除幻灯片窗口转换在启动时创建的白屏

java - 如何将 arraylist 值链接到类对象? (安卓)

java - 无法在后台获取位置更新

java - Android 9 上的振动

android - 如何检查 GPS 是否已连接以及位置是否已更新

android - 从我的 Android 中的 GPS 硬件获得最高的 GPS 更新率

java - 在尝试使用 GPS 之前如何检查它是否已启用

java - 将 SharedPreferences 与 for 循环一起使用