How to use the variable of one method in another method?

1

I want to use the previously assigned latitude and longitude variables in the onLocationChanged (Location location) method and use them in the method Method ()

Method where I want to get latitude and longitude

@Override
public void onLocationChanged(Location location) {
    Log.d("onLocationChanged", "entered");

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    //Place current location marker
    latitude = location.getLatitude();//aqui estamos obteniendo la latitud
    longitude = location.getLongitude();//aqui estamos obteniendo la longitud
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));//esto define el color del marcador
    mCurrLocationMarker = mMap.addMarker(markerOptions);//esto añade un marcador en nuestra posocion

    //move map camera
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    Toast.makeText(MapsActivity.this,"Your Current Location", Toast.LENGTH_LONG).show();

    Log.d("onLocationChanged", String.format("latitude:%.3f longitude:%.3f",latitude,longitude));

    //stop location updates
    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);// LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        Log.d("onLocationChanged", "Removing Location Updates");
    }
    Log.d("onLocationChanged", "Exit");

}

Method where I want to use them

 public void Method(){
        String Restaurant = "restaurant";
                //mMap.clear();
                String url = getUrl(latitude, longitude, Restaurant);
                Object[] DataTransfer = new Object[2];
                DataTransfer[0] = mMap;
                DataTransfer[1] = url;
                Log.d("onClick", url);
                GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
                getNearbyPlacesData.execute(DataTransfer);
                Toast.makeText(MapsActivity.this,"Nearby Restaurants", Toast.LENGTH_LONG).show();
    }
    
asked by Yop 02.05.2018 в 17:51
source

2 answers

0

If you are inside a class, it would be more convenient to declare the properties latitude and longitude as members of the class

public class Myclass{

        public latitude;
        public longitude;

        @Override
    public void onLocationChanged(Location location) {...}

        public void Method(){...}

        }

then you assign them a value in the onLocationChanged () event

   //Nótese el uso de 'THIS' para acceder a las propiedades de la clase

    this.latitude = location.getLatitude();//aqui estamos obteniendo la latitud
    this.longitude = location.getLongitude();//aqui estamos obteniendo la longitud

and finally, you access these same properties in your method

 public void Method(){
        String Restaurant = "restaurant";
                //mMap.clear();
                String url = getUrl(this.latitude, this.longitude, Restaurant);
                Object[] DataTransfer = new Object[2];
                DataTransfer[0] = mMap;
                DataTransfer[1] = url;
                Log.d("onClick", url);
                GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
                getNearbyPlacesData.execute(DataTransfer);
                Toast.makeText(MapsActivity.this,"Nearby Restaurants", Toast.LENGTH_LONG).show();
    }
    
answered by 02.05.2018 в 19:42
0

in your method tell you that you will receive variables

public void Method(double lat, double lon){
    String Restaurant = "restaurant";
    String url = getUrl(lat, lon, Restaurant);
    ......mode code
}

and simply where you go to call your Method you pass the data as follows Method(latitude,longitude); but of course the variables latitude y longitude must go to the beginning to be used in all clase

    
answered by 02.05.2018 в 19:54