As stated in the question, I would like to save lat and longitude as a double so I can use them in my program.
I can use setOnMyLocationChange listener, and from there create an object from the UserLocation class I have created which stores the lat and long.
I can print it out within the listener method using UserLocation.getLat() but I can not store the variable to use it elsewhere in the program. I will post the code which I think is relevant.
I have UserLocation l stored as global variable
UserLocation l;
Here I am getting a null object reference for l.getLat() even though I can reference the variable in the listener.
else if (city.equals("myLocation")) {
map.setMyLocationEnabled(true);
Location myLocation = map.getMyLocation();
map.setOnMyLocationChangeListener(this);
// map.setOnMyLocationChangeListener(null);
lat = l.getLat();
lon = l.getLng();
Toast.makeText(this, lat+" is latMy in map activity ", Toast.LENGTH_LONG).show();
}
Here I can print out the values just fine
@Override
public void onMyLocationChange(Location location) {
latMy = location.getLatitude();
lngMy = location.getLongitude();
l = new UserLocation();
l.setLat(latMy);
l.setLng(lngMy);
Toast.makeText(this, latMy+" is latMy in LOCATION CHANGE ", Toast.LENGTH_LONG).show();
}
Here is my UserLocation class
package com.example.its.citymap;
public class UserLocation {
private double lat;
private double lng;
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
}
I have searched and tried a lot but can not find a solid answer to this.
Thank you