在我的应用程序中,我想要 GPS 和/或网络的纬度和经度值。我的代码可以工作,但有时它给出了准确的值,有时它没有给出准确的值,有时它给出的值距离准确位置 10 或 20 米。代码:
mlocListener = new MyLocationListener(getApplicationContext());
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
10000, 0, mlocListener);
} else if (mlocManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
mlocManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 10000, 0, mlocListener);
}
Location gpsLocation = mlocManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location netLocation = mlocManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (gpsLocation != null) {
mlocListener.latitude = gpsLocation.getLatitude();
mlocListener.longitude = gpsLocation.getLongitude();
double lat = (double) (gpsLocation.getLatitude());
double lng = (double) (gpsLocation.getLongitude());
Toast.makeText(this, "Location attributes using GPS Provider",
Toast.LENGTH_LONG).show();
} else if (netLocation != null) {
mlocListener.latitude = netLocation.getLatitude();
mlocListener.longitude = netLocation.getLongitude();
double lat = (double) (netLocation.getLatitude());
double lng = (double) (netLocation.getLongitude());
Toast.makeText(this, "Location attributes using NETWORK Provider",
Toast.LENGTH_SHORT).show();
}