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,true);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,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",int which) {
            dialog.cancel();
        }
    });

    alertDialog.show();
}

}

解决方法

GPS总是很慢,因此我建议您使用Google Play服务使用Android Location API.

脚步

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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...