安卓GPS获取位置坐标花费时间过长

7

在这里,GPS需要半个小时或更长的时间来获取当前位置坐标。

我如何在同一上下文中同时使用GPS_SATELLITES和GPS_NETWORK_PROVIDERS,并获取最近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
GPS 定位需要至少三颗卫星,才能给出准确的位置信息。定位需要时间,请在后台进行以获得更好的体验。 - M D
1个回答

5

GPS定位速度较慢,建议使用Android Location API using Google Play Services

步骤

在Activity的onResume()方法中检查Google Play服务是否可用。

一旦设备上安装了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)");
        }
    }

您可以在这里找到有关上述方法和步骤的完美解释。同时,请查看官方文档

1
有没有其他解决上述问题的方案,而不是使用Google Play服务? - Deepak D
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - theapache64
Google Play服务和位置API(FusedLocation)从长远来看并不是很友好。我曾经历了一番煎熬才让“单次更新”在位置API上正常工作。位置管理器适用于单次更新,但需要太多时间。因此,我建议使用两者的组合:对于第一次加载,请使用FusedLocation。完成后,删除回调并初始化位置管理器以获取进一步的更新。(我是在谈论单次更新的情况下) - Koushik Shom Choudhury

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接