I have a class GetMyLocation that gets the current location of the user. Now I'm trying to call an object of this class from another class, ShowMap2. But I am unable to send the updated location to the new class.
I haven't included the other overrides for GetMyLocation here.
What I have:
public class ShowMap2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showmap);
GetMyLocation myLocation = new GetMyLocation();
myLat = myLocation.GetCurrentLat();
myLon = myLocation.GetCurrentLon();
}
This is the class that gets the location:
public class GetMyLocation extends Activity implements LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
Location location;
public static double myLat, myLon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
this.myLat = location.getLatitude();
this.myLon = location.getLongitude();
}
public double GetCurrentLat(){
return this.myLat;
}
public double GetCurrentLon(){
return this.myLon;
}
The above code returns 0.0 for both lat and lon.
I have tried every possible way I could think of, but I always end up get 0.0. I know the class itself gets my current lat and lon, but I am not able to pass it correctly. Could some please help me out here?
Thanks.
PS:
I found this thread, and tried it accordingly, but to no avail. Still zeros.
How to access this variable from an Android onCreate method?
The only difference, in the question in the link, the class is static. When I make my class static, it says: Modifier 'static' not allowed here.
My updated code:
public class GetMyLocation extends Activity implements LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
Location location;
static double myLat, myLon;
double GetCurrentLat(){
return myLat;
}
double GetCurrentLon(){
return myLon;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
this.myLat = location.getLongitude();
this.myLon = location.getLongitude();
}
EDIT: Updated Code that works:
public class GetMyLocation implements LocationListener {
//public class GetMyLocation extends Activity implements LocationListener {
private static final LatLng HAMBURG = new LatLng(43.48484521,-80.5274279);
private GoogleMap map;
protected LocationManager locationManager;
protected LocationListener locationListener;
Location location;
private static double myLat, myLon;
GetMyLocation(Activity activity){
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
this.myLat = location.getLatitude();
this.myLon = location.getLongitude();
}
double GetCurrentLat(){
return this.myLat;
}
double GetCurrentLon(){
return this.myLon;
}
getting the location values from ShowMap2 using:
GetMyLocation myLocation = new GetMyLocation(this);
GetMyLocation myLocation = new GetMyLocation();does not call methods on it `onCreate(Bundle savedInstanceState);' You need to learn how to create a new Activity