Android GPS showing different location and counting wrong distance - android

i'm making app where i using GPS and counting traveled distance, i'm downloading current loaction and counting distance between two location. When i'm trying to get current location it is showing me different location (i don't move my phone) and when i'm trying to count distance i'm geting wrong result :/ here source code
GPSTracker:
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
public static double distance=0;
Location location1,location2;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Activity activity;
Location location;
double latitude;
double longitude;
private static final float MIN_DISTANCE_CHANGE_FOR_UPDATES = 50;
private static final long MIN_TIME_BW_UPDATES = 1000 * 10;
protected LocationManager locationManager;
public GPSTracker(Context context, Activity activity) {
this.mContext = context;
this.activity = activity;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
int requestPermissionsCode = 50;
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, requestPermissionsCode);
} else {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
if(isGPSEnabled) {
if(location == null) {
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50);
} else {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}
} catch (Exception e){
e.printStackTrace();
}
return location;
}
private final LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
if(location1 == null){
location1 = location;
} else {
location2 = location1;
location1 = location;
distance += location1.distanceTo(location2) / 1000f;
}
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
DistanceActivity:
public class DistanceActivity extends Activity{
Button startB, stopB;
TextView textView;
GPSTracker gps;
Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distance);
startB = findViewById(R.id.start_B);
stopB = findViewById(R.id.stop_B);
textView = findViewById(R.id.distance_TV);
mContext = this;
startB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gps = new GPSTracker(mContext,DistanceActivity.this);
if(gps.canGetLocation()){
textView.setText("Distance: "+String.format("%.2f Km",gps.distance ));
} else {
textView.setText("Cannot count distance.");
}
}
});
}
}

Related

getLastKnownLocation sometimes returns null

Let me start by acknowledging that there are many results when you search for this particular question. Yet, nothing I've tried, according to those answers, works. Sometimes I get the location, sometimes I don't. If I play with the Location enabling/disabling I always get null when calling getLastKnownLocation. If I sometimes manually revoke the Location permission and enable it again, it works. I've tried changing the LocationManager to location = locationManager .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); but it doesn't work.
Basically, I am doing the following:
public class GPSTracker implements LocationListener {
public static final int LOCATION_SETTINGS_REQUEST_CODE = 109;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60;
private static boolean isGpsStatusChanged = false;
private static AlertDialog.Builder alertDialog;
private LocationManager locationManager;
private boolean canGetLocation = false;
private Location location;
private double latitude;
private double longitude;
private NewLocationListener newLocationListener;
public interface NewLocationListener {
void onLocationChanges(Location location);
}
public GPSTracker() {
}
#SuppressWarnings("all")
public Location getLocation() {
try {
locationManager = (LocationManager) BaseApplication.getContext().getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPSTracker", "Network");
if (locationManager != null) {
Log.d("GPSTracker", "Network - location manager != null");
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Log.d("GPSTracker", "Network - last known location != null");
latitude = location.getLatitude();
longitude = location.getLongitude();
newLocationListener.onLocationChanges(location);
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPSTracker", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d("LastKnownLocation", "Location: " + locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
newLocationListener.onLocationChanges(location);
}
}
}
}
}
} catch (Exception e) {
Log.e(Constants.TAG, e.getMessage(), e);
}
return location;
}
public static boolean isGpsEnabled() {
LocationManager locationManager = (LocationManager) BaseApplication.getContext().getSystemService(Context
.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
#SuppressWarnings("all")
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void setNewLocationListener(NewLocationListener newLocationListener) {
this.newLocationListener = newLocationListener;
}
public void removeLocationListener() {
this.newLocationListener = null;
}
#SuppressWarnings("InlinedApi")
public void showSettingsAlert(final Context context) {
if (alertDialog != null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
alertDialog = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
alertDialog = new AlertDialog.Builder(context);
}
alertDialog.setTitle(R.string.gps_window_title);
alertDialog.setMessage(R.string.gps_window_message);
alertDialog.setPositiveButton(R.string.gps_window_button_positive, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
((Activity) context).startActivityForResult(intent, LOCATION_SETTINGS_REQUEST_CODE);
}
});
alertDialog.setNegativeButton(R.string.gps_window_button_negative, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show().setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
alertDialog = null;
}
});
}
#Override
public void onLocationChanged(Location location) {
if (newLocationListener != null) {
newLocationListener.onLocationChanges(location);
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public static boolean isGpsStatusChanged() {
return isGpsStatusChanged;
}
public static void setIsGpsStatusChanged(boolean isGpsStatusChanged) {
GPSTracker.isGpsStatusChanged = isGpsStatusChanged;
}
}
And then in my fragment:
private void statGPSLocationTracking() {
Log.d(Constants.TAG, "start gps update");
// check if GPS enabled
gpsTracker = new GPSTracker();
gpsTracker.setNewLocationListener(newLocationListener);
gpsTracker.getLocation();
if (!gpsTracker.canGetLocation()) {
Log.d(Constants.TAG, "GPS not enabled, show enable dialog");
// stop the gps tracker(removes the location update listeners)
gpsTracker.stopUsingGPS();
// ask user to enable location
gpsTracker.showSettingsAlert(getActivity());
}
}
private GPSTracker.NewLocationListener newLocationListener = new GPSTracker.NewLocationListener() {
#Override
public void onLocationChanges(Location loc) {
if (getActivity() == null) {
return;
}
// GPS location determined, load the web page and pass the coordinates in the header location section
Log.d(Constants.TAG, "Location listener onLocationChanges: " + loc);
infoManager.setCurrentLocation(loc);
checkRadioInfoNeeded();
// stop the gps tracker(removes the location update listeners)
gpsTracker.stopUsingGPS();
}
};
Without this location permission android.Manifest.permission.ACCESS_FINE_LOCATION the getLastKnownLocation() will always return null.
getLastKnownLocation() does not return location immediately, it takes some time.
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60;
this to
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;
private static final long MIN_TIME_BW_UPDATES = 5 * 1000;
Also remove your code which gets location from network. Go to Google maps and see if maps is able to detect your current location. If your map is able to get location then your code too should get location place breakpoints and see where the real issue is.

I did everything, but again giving the latitude and longitude of 0.0

Class for getting Location
public class TrackGPS extends Service implements LocationListener {
private final Context mContext;
boolean checkGPS = false;
boolean checkNetwork = false;
boolean canGetLocation = false;
Location loc;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
public TrackGPS(Context mContext) {
this.mContext = mContext;
getLocation();
}
private Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
checkGPS = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
checkNetwork = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!checkGPS && !checkNetwork) {
Toast.makeText(mContext, "No Service Provider Available", Toast.LENGTH_SHORT).show();
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (checkNetwork) {
Toast.makeText(mContext, "Network", Toast.LENGTH_SHORT).show();
try {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (loc != null) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
}
catch(SecurityException e){
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (checkGPS) {
Toast.makeText(mContext,"GPS",Toast.LENGTH_SHORT).show();
if (loc == null) {
try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
}
} catch (SecurityException e) {
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return loc;
}
public double getLongitude() {
if (loc != null) {
longitude = loc.getLongitude();
}
return longitude;
}
public double getLatitude() {
if (loc != null) {
latitude = loc.getLatitude();
}
return latitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS Not Enabled");
alertDialog.setMessage("Do you wants to turn On GPS");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(TrackGPS.this);
}
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle ) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
Code for Main Activity is
private static final int REQUEST_CODE_PERMISSION = 1;
String mPermission = Manifest.permission.ACCESS_FINE_LOCATION
private Button b_get;
private TrackGPS gps;
double longitude;
double latitude;
//----------------------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_get = (Button)findViewById(R.id.locationBtn);
b_get.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gps = new TrackGPS(MainActivity.this);
if(gps.canGetLocation()){
longitude = gps.getLongitude();
latitude = gps .getLatitude();
Toast.makeText(getApplicationContext(),"Longitude:"+Double.toString(longitude)+"\nLatitude:"+Double.toString(latitude),Toast.LENGTH_SHORT).show();
}
else
{
gps.showSettingsAlert();
}
}
});
if(Build.VERSION.SDK_INT>= 23) {
if (checkSelfPermission(mPermission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{mPermission,
},
REQUEST_CODE_PERMISSION);
return ;
}
else
{
}
}
protected void onDestroy() { super.onDestroy() gps.stopUsingGPS();
}
Add Required permissions in your manifest file
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

GPS and Wifi are not enabled but isProviderEnabled is returning always true

In this case I use GPS,Network as a Provider, I try walking inside the building but seem like it doesn't find the location.. So, Why isProviderEnabled return true? Anyway, What is the way that i should implement ?
This is my code :
public class GPSTraker extends Service implements LocationListener {
private final Context context ;
boolean isGPSEnabled = false;
boolean isNetworkEnabled= false;
boolean canGetLocation = false;
Location location;
double latitude ;
double longitude ;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES=1000*60 ;
private LocationManager locationManager;
public GPSTraker(Context context){
this.context=context;
getLocation();
}
public Location getLocation(){
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
//No providers
} else {
this.canGetLocation = true;
/* if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
locationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
this
);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
}*/
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
locationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
this
);
Log.d("GPS", "GPS");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
}
}
}
}catch (Exception e){
e.printStackTrace();
Log.d("Exeception getLocation",e.toString());
}
return location;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation(){
return this.canGetLocation;
}
public void showSettingAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS is setting");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
alertDialog.show();
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTraker.this);
}
}
}
Manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">
</uses-permission>
It returns true because it's enabled. You don't ask if it's able calculate your position. To get updates when your location is changed use onLocationChanged().

Sample GPS tracker with asynctask : error "provider doesn't exisit: null"

Im looking for a very sample example show longitude and latitude trackers with asynctask.
I find this code https://stackoverflow.com/a/6788457 but he doesn't work :/ i catch this strangely exception "provider doesn't exisit: null" in :
public boolean startService() {
try {
// this.locatorService= new
// Intent(FastMainActivity.this,LocatorService.class);
// startService(this.locatorService);
FetchCordinates fetchCordinates = new FetchCordinates();
fetchCordinates.execute();
return true;
} catch (Exception error) {
Log.i("exception", error.getMessage());
return false;
}
}
After search, I see this post (https://stackoverflow.com/a/13851305/2137454) who explain I must may be use this line :
List<String> providers = locationManager.getAllProviders();
But I don't understand where and how use this tips in my gpstracker class... Someone can help me ?
Here the complete gpstracker class :
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location = null;
double latitude = 0;
double longitude = 0;
double altitude = 0;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000* 60 * 1;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context.getApplicationContext();
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Log.i("Je tombe...","...ici !");
} else {
this.canGetLocation = true;
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
altitude = location.getAltitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS() {
if (locationManager != null) {
Log.i("STOPUSINGGPS", "EFFECTIF");
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if (location != null) {
Log.i("LATITUDE", "EFFECTIF");
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
if (location != null) {
Log.i("LONGITUDE", "EFFECTIF");
longitude = location.getLongitude();
}
return longitude;
}
public double getAltitude() {
if (location != null) {
Log.i("ALTITUDE", "EFFECTIF");
altitude = location.getAltitude();
}
return altitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
edit : my gps is turned ON

Why current location of android phone don't change when i change location?

I get location of android phone as:
android.location.Location locationA;
LocationManager locationManager;
Criteria cri = new Criteria();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String tower = locationManager.getBestProvider(cri, false);
locationA = locationManager.getLastKnownLocation(tower);
if (locationA != null) {
// lat = (double) (locationA.getLatitude() * 1E6);
// longi = (double) (locationA.getLongitude() * 1E6);
double lat = locationA.getLatitude();
double longi = locationA.getLongitude();
TextView txt = (TextView) findViewById(R.id.textView1);
String td = String.valueOf(lat) + "," + String.valueOf(longi);
txt.setText(td);
}
Why current location of android phone don't change when i change location and get again current location?
check the time of your location using locationA.getTime(). if it was not up to date wait for a new location and then stop.
private static Location currentLocation;
private static Location prevLocation;
public void yourMethod()
{
locationManager.requestLocationUpdates(provider, MIN_TIME_REQUEST,
MIN_DISTANCE, locationListener);
}
private static LocationListener locationListener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
gotLocation(location);
}
};
private static void gotLocation(Location location) {
prevLocation = currentLocation == null ?
null : new Location(currentLocation);
currentLocation = location;
if (isLocationNew()) {
// do something
locationManager.removeUpdates(locationListener);
}
}
private static boolean isLocationNew() {
if (currentLocation == null) {
return false;
} else if (prevLocation == null) {
return false;
} else if (currentLocation.getTime() == prevLocation.getTime()) {
return false;
} else {
return true;
}
}

Categories

Resources