Reputation: 49
Firstly sorry for asking so many questions here the past couple of weeks, I'm new to android studio and finding it tough to figure out a lot of the core concepts by myself. In regards to the question, I have a project set up so whatever you type into the "1.2, -1.2" ect parameters you will find you the distance between two places and your answer will be displayed as a toast. However, I want the latitudes and longitueds to be variables.
Button button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Double distance = 1.0;
int val = 1;
Toast.makeText(getApplicationContext(), "You are " + String.valueOf(distance
(1.2, -1.2, 1.3, -2.4, "K")) + "kilometers away from the flag", Toast.LENGTH_LONG).show(); }
});
I have the following two methods and I want the values LatLng and currentLatitude and currentLongitude doubles for the parameters above.
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(53.3835,6.5996)).title("Marker"));
}
and
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
//mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title("Current Location"));
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
Any insight on how to do it would be much appreciated. From what I've read I know I'll need to split the LatLng variable into strings but thats all I can think of so far
Upvotes: 1
Views: 4089
Reputation: 999
If those 2 methods are in the same class then just make those 2 variables attributes of that class, else you will need to pass them to the corresponding classes. I will demonstrate in a simpler example than your code :
public class MyClass(){
private int lat;
private int longt;
//constructors ,setters and getters
public void method1(){
//affecting those attributes with values
lat = 1;
longt = 2;
}
public void method2(){
//simply access the attributes
System.out.println("lat "+lat+" longt "+longt);
}
}
Reading the comments , I think I need to further explain to you that when a variable is declared inside a method, it is local to that method, and therefor it will be "destroyed" (garbage collected or w.e) when that method is done doing it's job. But when a variable is declared outside a method, like the class's attributes, you can still refer to it whenever you need to, until the instance of that class is "destroyed".
Upvotes: 2