I creating an app to GPS tracking.
Unfortunetlly the app doesn't show actual location (lat,long). It always shows lat:50.059644 long:19.9206669. But I want to get and show the actual location of user(me).
It's my main activity:
Button btnShowLocation;
GPSTracker gps;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
btnShowLocation = (Button) findViewById(R.id.btn1);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
gps = new GPSTracker(About.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
gps.showSettingsAlert();
}
}
});
}
And this is my GPS Tracker:
public class GPSTracker extends Service implements LocationListener{
private final Context mContext;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
Location location;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try{
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isGPSEnabled) {
System.out.println("disabled");
//no network provider is enabled
} else {
this.canGetLocation = true;
if(isGPSEnabled){
System.out.println("gps wlaczony");
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) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
} catch(Exception e){
e.printStackTrace();
}
return location;
}