Problem with the use of variables

0

I have a question, maybe it's silly but I've been trying several ways and nothing else I can not. I want to use the variable lo in the send method to another activity but it does not leave me, it is assumed that in onLocationChanged after all the procedure the variable is stored in the string value of the distance calculation and that value I need to send it to another layout, but this is not the case, in fact when trying to write the variable in the i.putExtra it does not appear. I leave an image to see if it is more understandable.

@Override
public void onLocationChanged(Location loc ) {
    Double lati =  loc.getLatitude();
    Double loni =  loc.getLongitude();
    double d = Double.parseDouble(ubica);
    double distance = SphericalUtil.computeDistanceBetween(new LatLng(loni,lati), new LatLng(d,d));
    String lo = Double.toString(distance);
}

@Override
public void onInfoWindowClick(Marker marker) {
    Toast.makeText(this, "Información Detallada",
            Toast.LENGTH_LONG).show();
    // num1(marker.getSnippet());
    for (MarkerInfo markerInfo : list) {
        if (marker.getTitle().contentEquals(markerInfo.name)) {
            String Abc;
            Abc = marker.getTitle();

            Intent i = new Intent(this, detalle.class);
            i.putExtra("firstName", Abc);
            i.putExtra("lastName", markerInfo.population);
            i.putExtra("img", markerInfo.imageURL);
            i.putExtra("website",markerInfo.web);
            i.putExtra("ubica",markerInfo.ubica);
            i.putExtra("distan", lo );
            startActivity(i);

            break;

    
asked by Xavier Espinosa 15.08.2017 в 07:24
source

1 answer

1

The variable lo is being declared and initialized within the onLocationChanged method, therefore you can not access it from the onInfoWindowClick method.

To access lo from both methods, you must declare it at the class level (outside the onLocationChanged method) and then you can use it in any of your methods.

The code is as follows (ignoring the parts that are not relevant with ... )

private String lo;

@Override
public void onLocationChanged(Location loc) {
    ...
    lo = Double.toString(distance);
}

@Override
public void onInfoWindowClick(Marker marker) {
    ...
    i.putExtra("distan", lo);
    ...
    
answered by 15.08.2017 / 07:40
source